aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-17 14:41:21 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-17 14:42:01 +0100
commit9c1e58152495199eacc44419013f7b9d03914f32 (patch)
treedb37bed87851a4fc2e42ec246bd42074256cfa2b
parent0761c00b1ea2b88ac2f79ae6b936b5d0d344e4a4 (diff)
downloadadvent-of-code-9c1e58152495199eacc44419013f7b9d03914f32.tar.gz
advent-of-code-9c1e58152495199eacc44419013f7b9d03914f32.tar.bz2
advent-of-code-9c1e58152495199eacc44419013f7b9d03914f32.zip
Current implementation of puzzle-7
Not complete, dunno what to do for parsing and handling CD commands.
-rw-r--r--2022/puzzle-7.lisp47
1 files changed, 47 insertions, 0 deletions
diff --git a/2022/puzzle-7.lisp b/2022/puzzle-7.lisp
new file mode 100644
index 0000000..3a4f353
--- /dev/null
+++ b/2022/puzzle-7.lisp
@@ -0,0 +1,47 @@
+(load "lib")
+(defparameter input (uiop:read-file-string "7-input"))
+(defparameter lines (get-lines input)) ;; first line is ALWAYS "$ cd /"
+
+(defun tokenise-lines (lines)
+ "Split each LINE in LINES by space"
+ (mapcar (lambda (line)
+ (mapcar #'clist-to-string
+ (split-completely (string-to-clist line) #\Space)))
+ lines))
+
+(defun token-cmdp (token)
+ "Checks if TOKEN is a command (by the first member)"
+ (string= (car token) "$"))
+
+(defun till-next-cmd (tokens)
+ "Iterates over TOKENS till a command is found, returning the remaining
+tokens (including command)."
+ (loop
+ for token-set on tokens
+ if (token-cmdp (car token-set))
+ return token-set))
+
+(defun parse-ls (tokens)
+ "Converts the following TOKENS till a command (via TOKEN-CMDP) into a
+directory structure i.e. an alist of string names by their content."
+ (loop for token in tokens
+ until (token-cmdp token)
+ collect
+ (let ((size (car token))
+ (name (cadr token)))
+ (if (string= size "dir")
+ `(,name . nil)
+ `(,name . ,(parse-integer size))))))
+
+(defun parse-tokens (tokens)
+ (if (token-cmdp (car tokens))
+ (cond
+ ((string= (cadar tokens) "ls")
+ (values
+ (till-next-cmd (cdr tokens))
+ (parse-ls (cdr tokens))))
+ ((string= (cadar tokens) "cd")
+ ;; TODO: Actually figure out what I should do here
+ (values
+ (cdr tokens)
+ nil)))))