diff options
author | Oscar Najera <hi@oscarnajera.com> | 2023-12-04 18:37:20 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2023-12-04 18:37:20 +0100 |
commit | 6e2e487e8c87102c2761986cc8b7941290d0a2db (patch) | |
tree | 8b543600288d59faf8d7e6b6d9556b7d42fdf8f6 /AoC2023 | |
parent | b43f2a47e83aa7ca9c8144e3f144d2c69c7fc579 (diff) | |
download | scratch-6e2e487e8c87102c2761986cc8b7941290d0a2db.tar.gz scratch-6e2e487e8c87102c2761986cc8b7941290d0a2db.tar.bz2 scratch-6e2e487e8c87102c2761986cc8b7941290d0a2db.zip |
Solve day04 part2
Diffstat (limited to 'AoC2023')
-rw-r--r-- | AoC2023/day04/solver.lisp | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/AoC2023/day04/solver.lisp b/AoC2023/day04/solver.lisp index ec8f09d..2baed03 100644 --- a/AoC2023/day04/solver.lisp +++ b/AoC2023/day04/solver.lisp @@ -1,4 +1,4 @@ -(ql:quickload '(fiveam str transducers)) +(ql:quickload '(fiveam str arrows transducers)) (defpackage #:day04 (:use #:cl #:fiveam) @@ -28,10 +28,38 @@ Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11") (t:comp (t:filter-map #'matching-numbers) (t:map #'length) - (t:map (lambda (n) (expt 2 (1- n))))) + (t:map #'1-) + (t:map (lambda (n) (expt 2 n)))) #'+ lines)) +(defun solver1c (lines) + (arrows:->> + (mapcar #'matching-numbers lines) + (remove-if #'null) + (mapcar #'length) + (mapcar #'1-) + (mapcar (lambda (n) (expt 2 n))) + (apply #'+))) + +(defun solver2 (lines) + (let* ((line-count (length lines)) + (stash (make-hash-table :size line-count))) + (loop + for line in lines + for i from 0 + for matches = (length (matching-numbers line)) + do (incf (gethash i stash 0)) + (dotimes (j matches) + (when (< (+ i j 1) line-count) + (incf (gethash (+ i j 1) stash 0) + (gethash i stash)))) + sum (gethash i stash 0)))) + (test solutions (is (= 13 (solver1 (uiop:split-string eg-input :separator '(#\Newline))))) - (is (= 18619 (solver1 #p"input")))) + (is (= 13 (solver1c (uiop:split-string eg-input :separator '(#\Newline))))) + (is (= 18619 (solver1 #p"input"))) + (is (= 18619 (solver1c (uiop:read-file-lines "input")))) + (is (= 30 (solver2 (uiop:split-string eg-input :separator '(#\Newline))))) + (is (= 8063216 (solver2 (uiop:read-file-lines "input"))))) |