(1|2)+lisp files for puzzles 1 and 2

Includes code for both rounds, separated by comments.
This commit is contained in:
2023-06-26 07:16:31 +01:00
parent e118b9ad2a
commit 6761c53d16
2 changed files with 80 additions and 0 deletions

26
puzzle-1.lisp Normal file
View File

@@ -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)))