From 57f4a063d6bad93589ae9864d103d50f128e542d Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Thu, 19 Dec 2024 14:20:50 +0100 Subject: [PATCH] solved day19 --- src/2024/day19.lisp | 34 ++++++++++++++++++---------------- tests/2024/day19-test.lisp | 16 ++++------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/2024/day19.lisp b/src/2024/day19.lisp index e5ece2d..d4930cd 100644 --- a/src/2024/day19.lisp +++ b/src/2024/day19.lisp @@ -1,4 +1,3 @@ - (defpackage :aoc/2024/19 (:use :cl :aoc :alexandria :trivia :lla) (:export @@ -54,7 +53,7 @@ bbrgwb")) (terminates nil)) (loop for towel in towels - if (emptyp towel + if (emptyp towel) do (setf terminates t) else do (setf (gethash (char towel 0) towel-tree) @@ -66,16 +65,16 @@ bbrgwb")) ((< (length a) (length b)) t) ((> (length a) (length b)) nil) (t (string-lessp a b))) - )))) - (loop - for k being the hash-keys 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)))) - ) - (make-node - :name word - :terminates terminates - :next towel-tree))) + ))) + (loop + for k being the hash-keys 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)))) + ) + (make-node + :name word + :terminates terminates + :next towel-tree)))) (defun show-tree (tree &optional (depth 0)) (loop @@ -154,11 +153,10 @@ bbrgwb")) collect pattern)) (defun possible-patterns (towels pattern) - (multiple-value-bind (is-valid found) + (multiple-value-bind (n found) (gethash pattern *possible-patterns*) - (format t "~A ~A ~A~%" pattern is-valid found) (if found - (if is-valid 1 0) + n (setf (gethash pattern *possible-patterns*) (if (emptyp pattern) 1 @@ -175,7 +173,11 @@ bbrgwb")) (format nil "~A" (length (valid-patterns onsen)))) (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 () (format t "part1: ~A~%" (part1 input-data)) diff --git a/tests/2024/day19-test.lisp b/tests/2024/day19-test.lisp index f016c08..a1d4c19 100644 --- a/tests/2024/day19-test.lisp +++ b/tests/2024/day19-test.lisp @@ -64,21 +64,13 @@ ) - - -(define-test test-bar - :parent suite-2024-19 - ) - - - (define-test+run test-part1 - :parent suite-2024-19 - (is equal "6" (part1 sample-data))) + :parent suite-2024-19 + (is equal "6" (part1 sample-data))) (define-test+run test-part2 - :parent suite-2024-19 - (is equal nil (part2 sample-data))) + :parent suite-2024-19 + (is equal "16" (part2 sample-data))) (defun run-tests () (test 'suite-2024-19))