solved day19

This commit is contained in:
Peter Tillemans 2024-12-19 14:20:50 +01:00
parent ae69d78d64
commit 57f4a063d6
2 changed files with 22 additions and 28 deletions

View file

@ -1,4 +1,3 @@
(defpackage :aoc/2024/19 (defpackage :aoc/2024/19
(:use :cl :aoc :alexandria :trivia :lla) (:use :cl :aoc :alexandria :trivia :lla)
(:export (:export
@ -54,7 +53,7 @@ bbrgwb"))
(terminates nil)) (terminates nil))
(loop (loop
for towel in towels for towel in towels
if (emptyp towel if (emptyp towel)
do (setf terminates t) do (setf terminates t)
else else
do (setf (gethash (char towel 0) towel-tree) do (setf (gethash (char towel 0) towel-tree)
@ -66,16 +65,16 @@ bbrgwb"))
((< (length a) (length b)) t) ((< (length a) (length b)) t)
((> (length a) (length b)) nil) ((> (length a) (length b)) nil)
(t (string-lessp a b))) (t (string-lessp a b)))
)))) )))
(loop (loop
for k being the hash-keys in towel-tree for k being the hash-keys in towel-tree
for v being the hash-values in towel-tree for v being the hash-values in towel-tree
do (setf (gethash k towel-tree) (towel-tree v (concatenate 'string word (list k)))) do (setf (gethash k towel-tree) (towel-tree v (concatenate 'string word (list k))))
) )
(make-node (make-node
:name word :name word
:terminates terminates :terminates terminates
:next towel-tree))) :next towel-tree))))
(defun show-tree (tree &optional (depth 0)) (defun show-tree (tree &optional (depth 0))
(loop (loop
@ -154,11 +153,10 @@ bbrgwb"))
collect pattern)) collect pattern))
(defun possible-patterns (towels pattern) (defun possible-patterns (towels pattern)
(multiple-value-bind (is-valid found) (multiple-value-bind (n found)
(gethash pattern *possible-patterns*) (gethash pattern *possible-patterns*)
(format t "~A ~A ~A~%" pattern is-valid found)
(if found (if found
(if is-valid 1 0) n
(setf (gethash pattern *possible-patterns*) (setf (gethash pattern *possible-patterns*)
(if (emptyp pattern) (if (emptyp pattern)
1 1
@ -175,7 +173,11 @@ bbrgwb"))
(format nil "~A" (length (valid-patterns onsen)))) (format nil "~A" (length (valid-patterns onsen))))
(defun part2 (data) (defun part2 (data)
(length data)) (reset-patterns)
(format nil "~A"
(loop for p in (onsen-patterns data)
for n = (possible-patterns (onsen-towels data) p)
sum n)))
(defun solve-day () (defun solve-day ()
(format t "part1: ~A~%" (part1 input-data)) (format t "part1: ~A~%" (part1 input-data))

View file

@ -64,21 +64,13 @@
) )
(define-test test-bar
:parent suite-2024-19
)
(define-test+run test-part1 (define-test+run test-part1
:parent suite-2024-19 :parent suite-2024-19
(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
(is equal nil (part2 sample-data))) (is equal "16" (part2 sample-data)))
(defun run-tests () (defun run-tests ()
(test 'suite-2024-19)) (test 'suite-2024-19))