diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-06-26 07:16:31 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-06-26 07:16:31 +0100 |
commit | f8a0d0f1acc50711e470999434f7f8f504188b2e (patch) | |
tree | fde092685093fe73d936e51b02f6dc1c4a8d1f45 | |
download | advent-of-code-f8a0d0f1acc50711e470999434f7f8f504188b2e.tar.gz advent-of-code-f8a0d0f1acc50711e470999434f7f8f504188b2e.tar.bz2 advent-of-code-f8a0d0f1acc50711e470999434f7f8f504188b2e.zip |
(1|2)+lisp files for puzzles 1 and 2
Includes code for both rounds, separated by comments.
-rw-r--r-- | puzzle-1.lisp | 26 | ||||
-rw-r--r-- | puzzle-2.lisp | 54 |
2 files changed, 80 insertions, 0 deletions
diff --git a/puzzle-1.lisp b/puzzle-1.lisp new file mode 100644 index 0000000..45d1921 --- /dev/null +++ b/puzzle-1.lisp @@ -0,0 +1,26 @@ +(defvar input (uiop:read-file-string "puzzle-1-input.txt")) +(defvar *sep (format nil "~%~%")) + +(defun get-lists (input) + (let ((pos (search *sep input))) + (with-input-from-string (s (subseq input 0 pos)) + (let ((converted + (loop + for line = (read-line s nil nil) + while line + collect (parse-integer line)))) + (if (null pos) + (list converted) + (cons converted + (get-lists (subseq input (+ pos 2))))))))) + +(defvar sums (sort (mapcar (lambda (lst) (reduce #'+ lst)) (get-lists input)) #'>)) + +;; First challenge +(format t "Top snacks: ~a" (car sums)) + +;; Second challenge +(let ((first (car sums)) + (second (car (cdr sums))) + (third (car (cdr (cdr sums))))) + (format t "~a,~a,~a:>~a" first second third (+ first second third))) diff --git a/puzzle-2.lisp b/puzzle-2.lisp new file mode 100644 index 0000000..377b6ba --- /dev/null +++ b/puzzle-2.lisp @@ -0,0 +1,54 @@ +(defvar input (uiop:read-file-string "puzzle-2-input.txt")) +;; Each newline represents a new round, which we should parse on the go + +(defun sensible-convert-input (str) + (cond + ((or (string= str "X") (string= str "A")) 0) + ((or (string= str "Y") (string= str "B")) 1) + ((or (string= str "Z") (string= str "C")) 2))) + +;; Round 1 +(defvar rounds + (with-input-from-string (stream input) + (loop + for strategy = (read-line stream nil) + until (null strategy) + collect + (let ((opponent (subseq strategy 0 1)) + (yours (subseq strategy 2 3))) + (list (sensible-convert-input opponent) (sensible-convert-input yours)))))) + +(loop + for round in rounds + until (null round) + sum + (destructuring-bind (opp you) round + (+ + 1 you ;; base score + (cond ; outcome score + ((eq you opp) 3) + ((eq (mod (+ 1 opp) 3) you) 6) + (t 0))))) + +;; Round 2. + +;; We can still use the same rounds data as previously, just +;; reinterpret it in when doing the sum. + +(defun get-correct-choice (opponent outcome) + (case outcome + (0 (mod (- opponent 1) 3)) + (1 opp) + (2 (mod (+ 1 opponent) 3)) + (t 0))) + +(loop for round in rounds + sum + (destructuring-bind (opp you) round + (let ((choice (get-correct-choice opp you))) + (+ 1 choice + (case you ;; outcome -> score + (0 0) + (1 3) + (2 6) + (t 0)))))) |