diff options
author | Alexandre Jesus <adbjesus@gmail.com> | 2023-12-09 22:40:15 +0000 |
---|---|---|
committer | Alexandre Jesus <adbjesus@gmail.com> | 2023-12-09 22:40:15 +0000 |
commit | 35d1a045681b7132b039d0da1412d1ac3c18bff0 (patch) | |
tree | 31b968c408cbcba974ca418ab9664b035d8f61f9 /day02.lisp | |
download | aoc2023-35d1a045681b7132b039d0da1412d1ac3c18bff0.tar.gz aoc2023-35d1a045681b7132b039d0da1412d1ac3c18bff0.zip |
First 6 days
Diffstat (limited to 'day02.lisp')
-rw-r--r-- | day02.lisp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/day02.lisp b/day02.lisp new file mode 100644 index 0000000..c007a9f --- /dev/null +++ b/day02.lisp @@ -0,0 +1,58 @@ +(defun input-lines (f) + (with-open-file (s f) + (loop for line = (read-line s nil 'eof) + until (eq line 'eof) + collect line))) + +(defun check-set (string red green blue) + (multiple-value-bind (n begin) + (parse-integer string :junk-allowed t) + (cond ((search "red" string :start2 begin) (<= n red)) + ((search "green" string :start2 begin) (<= n green)) + ((search "blue" string :start2 begin) (<= n blue))))) + +(defun split-by-char (char string) + (loop for i = 0 then (1+ j) + as j = (position char string :start i) + collect (subseq string i j) + while j)) + +(defun check-round (string red green blue) + (let ((sets (split-by-char #\, string))) + (not (find-if-not (lambda (set) (check-set set red green blue)) sets)))) + +(defun check-game (string red green blue) + (let ((pstring (nth 1 (split-by-char #\: string)))) + (let ((rounds (split-by-char #\; pstring))) + (not (find-if-not (lambda (set) (check-round set red green blue)) rounds))))) + +(defun solve1 (f) + (loop for line in (input-lines f) + as gid = (parse-integer line :junk-allowed t :start 5) + when (check-game line 12 13 14) + sum gid)) + +(defun cube-count (string color) + (multiple-value-bind (n begin) + (parse-integer string :junk-allowed t) + (if (search color string :start2 begin) n 0))) + +(defun game-max-cubes (line color) + (loop for i = (1+ (position #\: line)) then (1+ j) + as j = (position-if (lambda (c) (or (equal c #\,) (equal c #\;))) line :start i) + as count = (cube-count (subseq line i j) color) + maximize count + while j)) + +(defun solve2 (f) + (loop for line in (input-lines f) + as r = (game-max-cubes line "red") + as g = (game-max-cubes line "green") + as b = (game-max-cubes line "blue") + sum (* r g b))) + +(print (solve1 "data/02/example.txt")) +(print (solve1 "data/02/input.txt")) +(print (solve2 "data/02/example.txt")) +(print (solve2 "data/02/input.txt")) + |