refactored part 1 to share caches with part 2
This commit is contained in:
parent
5926b5cd3f
commit
486314adbd
2 changed files with 13 additions and 65 deletions
|
@ -48,9 +48,7 @@ bbrgwb"))
|
||||||
(defvar *possible-cache* (make-hash-table :test #'equal))
|
(defvar *possible-cache* (make-hash-table :test #'equal))
|
||||||
(defun reset-patterns ()
|
(defun reset-patterns ()
|
||||||
(setf *pattern-cache* (make-hash-table :test #'equal))
|
(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)
|
(defun string-prefix-p (prefix string)
|
||||||
(and
|
(and
|
||||||
|
@ -67,32 +65,6 @@ bbrgwb"))
|
||||||
)))
|
)))
|
||||||
(setf (gethash pattern *pattern-cache*) remaining))))
|
(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)
|
(defun possible-patterns (towels pattern)
|
||||||
(multiple-value-bind (n found)
|
(multiple-value-bind (n found)
|
||||||
(gethash pattern *possible-cache*)
|
(gethash pattern *possible-cache*)
|
||||||
|
@ -110,17 +82,19 @@ bbrgwb"))
|
||||||
0))))
|
0))))
|
||||||
))
|
))
|
||||||
|
|
||||||
(defun part1 (onsen)
|
(defun possible-patterns-per-towel (onsen)
|
||||||
(format nil "~A" (length (valid-cache onsen))))
|
(loop for p in (onsen-patterns onsen)
|
||||||
|
for n = (possible-patterns (onsen-towels onsen) p)
|
||||||
|
collect n))
|
||||||
|
|
||||||
(defun part2 (data)
|
(defun part1 (onsen)
|
||||||
(reset-patterns)
|
(format nil "~A" (count-if-not #'zerop (possible-patterns-per-towel onsen))))
|
||||||
(format nil "~A"
|
|
||||||
(loop for p in (onsen-patterns data)
|
(defun part2 (onsen)
|
||||||
for n = (possible-patterns (onsen-towels data) p)
|
(format nil "~A" (reduce #'+ (possible-patterns-per-towel onsen))))
|
||||||
sum n)))
|
|
||||||
|
|
||||||
(defun solve-day ()
|
(defun solve-day ()
|
||||||
|
(reset-patterns)
|
||||||
(format t "part1: ~A~%" (part1 input-data))
|
(format t "part1: ~A~%" (part1 input-data))
|
||||||
(format t "part2: ~A~%" (part2 input-data)))
|
(format t "part2: ~A~%" (part2 input-data)))
|
||||||
|
|
||||||
|
|
|
@ -5,34 +5,6 @@
|
||||||
|
|
||||||
(define-test suite-2024-19
|
(define-test suite-2024-19
|
||||||
;:parent suite-2024
|
;: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
|
(define-test test-possible-patterns
|
||||||
|
@ -66,10 +38,12 @@
|
||||||
|
|
||||||
(define-test+run test-part1
|
(define-test+run test-part1
|
||||||
:parent suite-2024-19
|
:parent suite-2024-19
|
||||||
|
(reset-patterns)
|
||||||
(is equal "6" (part1 sample-data)))
|
(is equal "6" (part1 sample-data)))
|
||||||
|
|
||||||
(define-test+run test-part2
|
(define-test+run test-part2
|
||||||
:parent suite-2024-19
|
:parent suite-2024-19
|
||||||
|
(reset-patterns)
|
||||||
(is equal "16" (part2 sample-data)))
|
(is equal "16" (part2 sample-data)))
|
||||||
|
|
||||||
(defun run-tests ()
|
(defun run-tests ()
|
||||||
|
|
Loading…
Reference in a new issue