aboutsummaryrefslogtreecommitdiff
path: root/2022/Day07/part-one.cl
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2022-12-07 18:22:21 +0200
committerSyndamia <kamen@syndamia.com>2022-12-07 18:22:21 +0200
commitf63f48184eda1ea426e702f2288c2e02f7584323 (patch)
tree94fe6cc00fdd8723d6c61dfb177c9a7fd59e1fef /2022/Day07/part-one.cl
parentab60ec374d8952843264c9ebd3cd8f2dfca3a8a3 (diff)
downloadadvent-of-code-f63f48184eda1ea426e702f2288c2e02f7584323.tar
advent-of-code-f63f48184eda1ea426e702f2288c2e02f7584323.tar.gz
advent-of-code-f63f48184eda1ea426e702f2288c2e02f7584323.zip
[2022/D07] Solved both tasks from today
Diffstat (limited to '2022/Day07/part-one.cl')
-rw-r--r--2022/Day07/part-one.cl47
1 files changed, 31 insertions, 16 deletions
diff --git a/2022/Day07/part-one.cl b/2022/Day07/part-one.cl
index e1ffecc..390cd24 100644
--- a/2022/Day07/part-one.cl
+++ b/2022/Day07/part-one.cl
@@ -1,4 +1,3 @@
-
;;; https://gitlab.com/Syndamia/senzill
(require :senzill)
(use-package :senzill.math)
@@ -6,28 +5,44 @@
(use-package :senzill.io)
(ask-for-stream (prog-input)
- (let* ((folders '("/" 0 0)) (current-folder 0))
+ ;;; Every 3 consecutive values (in FOLDERS) represent one folder, where
+ ;;; the first is it's name, second is size of all files inside and third is parent folder.
+ (let* ((folders '("/" 0 -1)) (current-folder 0) (temp-size 0))
(defun parse-cd (loc)
- (cond ((string= loc "/")
- (setf current-folder 0))
- ((string= loc "..")
- (setf current-folder (nth (+ 2 current-folder) folders))
- (T
- (setf current-folder (position-if (lambda (x) (and (stringp x) (string= x loc)))
- current-folder))))))
+ "Updates CURRENT-FOLDER to point to index of chosen folder"
+ (if (string= loc "..")
+ (setf current-folder (nth (+ 2 current-folder) folders))
+ (+= current-folder (position-if (lambda (x) (and (stringp x) (string= x loc)))
+ (nthcdr current-folder folders)
+ :from-end T)))) ; Dirty trick for not properly handling folders with same names in different locations
(defun parse-dir (name)
- (push-back name folders)
- (push-back 0 folders) ; sum of size of all files
+ "Adds a new folder inside folders"
+ (push-back name folders)
+ (push-back 0 folders)
(push-back current-folder folders))
+ ;;; Parses input lines
(doread-lines (inpt :read-line-options (prog-input NIL))
- (cond ((and (char= (char inpt 0) #\$) (char= (char inpt 2) #\c))
+ ;; "$ ls" isn't parsed, because we don't need to
+ (cond ;; if we have "$ cd"
+ ((and (char= (char inpt 0) #\$) (char= (char inpt 2) #\c))
(parse-cd (subseq inpt 5)))
+ ;; if we have "dir ..."
((char= (char inpt 0) #\d)
(parse-dir (subseq inpt 4)))
+ ;; if we have "<NUMBER> <FILE_NAME>"
((digit-char-p (char inpt 0))
- (+= (nth (+ 1 current-folder) folders) (parse-integer inpt :junk-allowed T))))
- (print current-folder)
- (force-output))
- (print folders)))
+ (setf temp-size (parse-integer inpt :junk-allowed T))
+ ;;; Adds the file size to the current folder and all parent folders
+ ;;; This could be extracted to the final summation, but this way it's simpler
+ (loop for fol = current-folder then (nth (+ 2 fol) folders)
+ until (= fol -1) do
+ (+= (nth (+ 1 fol) folders)
+ temp-size)))))
+
+ ;;; LREST is a tail of FOLDERS, where on each iteration it starts with the file size of each folder
+ (loop for lrest on (cdr folders) by #'cdddr
+ if (<= (car lrest) 100000)
+ sum (car lrest) into result
+ finally (print result))))