diff --git a/src/2024/day19.lisp b/src/2024/day19.lisp index fe30e34..0ff0b9a 100644 --- a/src/2024/day19.lisp +++ b/src/2024/day19.lisp @@ -48,9 +48,7 @@ bbrgwb")) (defvar *possible-cache* (make-hash-table :test #'equal)) (defun reset-patterns () (setf *pattern-cache* (make-hash-table :test #'equal)) - (setf *valid-cache* (make-hash-table :test #'equal)) - (setf *possible-cache* (make-hash-table :test #'equal)) - ) + (setf *possible-cache* (make-hash-table :test #'equal))) (defun string-prefix-p (prefix string) (and @@ -67,32 +65,6 @@ bbrgwb")) ))) (setf (gethash pattern *pattern-cache*) remaining)))) -(defun valid-pattern-p (towels pattern) - (multiple-value-bind (is-valid found) - (gethash pattern *valid-cache*) - (if found - is-valid - (setf (gethash pattern *valid-cache*) - (if (emptyp pattern) - t - (if-let (ps (remaining-patterns towels pattern)) - (loop - for p in ps - for pok = (valid-pattern-p towels p) - thereis pok - ))))) - )) - -(defun towels-to-regex (onsen) - (format nil "^(~{~A~^|~})*$" (onsen-towels onsen))) - -(defun valid-cache (onsen) - (reset-patterns) - (loop - for pattern in (onsen-patterns onsen) - if (valid-pattern-p (onsen-towels onsen) pattern) - collect pattern)) - (defun possible-patterns (towels pattern) (multiple-value-bind (n found) (gethash pattern *possible-cache*) @@ -110,17 +82,19 @@ bbrgwb")) 0)))) )) -(defun part1 (onsen) - (format nil "~A" (length (valid-cache onsen)))) +(defun possible-patterns-per-towel (onsen) + (loop for p in (onsen-patterns onsen) + for n = (possible-patterns (onsen-towels onsen) p) + collect n)) -(defun part2 (data) - (reset-patterns) - (format nil "~A" - (loop for p in (onsen-patterns data) - for n = (possible-patterns (onsen-towels data) p) - sum n))) +(defun part1 (onsen) + (format nil "~A" (count-if-not #'zerop (possible-patterns-per-towel onsen)))) + +(defun part2 (onsen) + (format nil "~A" (reduce #'+ (possible-patterns-per-towel onsen)))) (defun solve-day () + (reset-patterns) (format t "part1: ~A~%" (part1 input-data)) (format t "part2: ~A~%" (part2 input-data))) diff --git a/tests/2024/day19-test.lisp b/tests/2024/day19-test.lisp index a1d4c19..aeb7803 100644 --- a/tests/2024/day19-test.lisp +++ b/tests/2024/day19-test.lisp @@ -5,34 +5,6 @@ (define-test suite-2024-19 ;:parent suite-2024 - ) - -(define-test test-valid-pattern-p - :parent suite-2024-19 - (let ((tt (onsen-towels sample-data))) - (false (valid-pattern-p tt "z")) - (true (valid-pattern-p tt "r")) - (true (valid-pattern-p tt "rb")) - (false (valid-pattern-p tt "bw")) - (true (valid-pattern-p tt "gbb")) - - ;;brwrr can be made with a br towel, then a wr towel, and then finally an r towel. - (true (valid-pattern-p tt "brwrr")) - ;;bggr can be made with a b towel, two g towels, and then an r towel. - (true (valid-pattern-p tt "bggr")) - ;; gbbr can be made with a gb towel and then a br towel. - (true (valid-pattern-p tt "gbbr")) - ;; rrbgbr can be made with r, rb, g, and br. - (true (valid-pattern-p tt "rrbgbr")) - ;; ubwu is impossible. - (false (valid-pattern-p tt "ubwu")) - ;; bwurrg can be made with bwu, r, r, and g. - (true (valid-pattern-p tt "bwurrg")) - ;; brgr can be made with br, g, and r. - (true (valid-pattern-p tt "brgr")) - ;; bbrgwb is impossible. - (false (valid-pattern-p tt "bbrgwb"))) - ) (define-test test-possible-patterns @@ -66,10 +38,12 @@ (define-test+run test-part1 :parent suite-2024-19 + (reset-patterns) (is equal "6" (part1 sample-data))) (define-test+run test-part2 :parent suite-2024-19 + (reset-patterns) (is equal "16" (part2 sample-data))) (defun run-tests ()