refactored part 1 to share caches with part 2

This commit is contained in:
Peter Tillemans 2024-12-19 14:40:19 +01:00
parent 5926b5cd3f
commit 486314adbd
2 changed files with 13 additions and 65 deletions

View file

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

View file

@ -7,34 +7,6 @@
;: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
:parent suite-2024-19
(reset-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 ()