diff options
-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"))))) |