diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-06-27 21:00:04 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-17 14:14:38 +0100 |
commit | 1041984da04df8d2968e3b45fd70beedd7b3ba8b (patch) | |
tree | 5d49d0c061589ddafb9fa5b9e8865343f2f82793 /2022/puzzle-1.lisp | |
parent | 3d8c64554157be723f555fc0dfb6652370e25316 (diff) | |
download | advent-of-code-1041984da04df8d2968e3b45fd70beedd7b3ba8b.tar.gz advent-of-code-1041984da04df8d2968e3b45fd70beedd7b3ba8b.tar.bz2 advent-of-code-1041984da04df8d2968e3b45fd70beedd7b3ba8b.zip |
(2022>README,1,5)+literate code for problem 1 rewrite,~minor changes to 5
Making README.org a literate document for my code to better describe
my process.
Diffstat (limited to '2022/puzzle-1.lisp')
-rw-r--r-- | 2022/puzzle-1.lisp | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/2022/puzzle-1.lisp b/2022/puzzle-1.lisp index 3a1510e..a53b599 100644 --- a/2022/puzzle-1.lisp +++ b/2022/puzzle-1.lisp @@ -1,26 +1,26 @@ (defvar input (uiop:read-file-string "2022/1-input")) + (defvar *sep (format nil "~%~%")) +(defun parse-entity (inp) + (with-input-from-string (s inp) + (loop for line = (read-line s nil) + while line + collect (parse-integer line)))) + (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))))))))) + (let* ((pos (search *sep input)) + (converted (parse-entity (subseq input 0 pos)))) + (if (null pos) + (list converted) + (cons converted + (get-lists (subseq input (+ pos 2))))))) -(defvar sums (sort (mapcar (lambda (lst) (reduce #'+ lst)) (get-lists input)) #'>)) +(defvar sums (sort (mapcar (lambda (lst) (reduce #'+ lst)) (get-lists input)) + #'>)) -;; First challenge -(format t "Top snacks: ~a" (car sums)) +(format t "Round 1: ~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))) +(destructuring-bind (first second third &rest _) sums + (format t "Round 2: ~a,~a,~a:>~a" first second third + (+ first second third))) |