From f5b144ce216a5442a736a1b01df6fde5b5184a12 Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Sat, 7 Dec 2024 09:54:28 +0100 Subject: [PATCH] solved day 7, fixed compilation errors preventing loading --- aoc.asd | 2 + src/2024/day05.lisp | 1 - src/2024/day06.lisp | 14 +--- src/2024/day07.lisp | 128 +++++++++++++++++++++++++++++++++++++ tests/2024/day05-test.lisp | 13 ++-- tests/2024/day07-test.lisp | 49 ++++++++++++++ 6 files changed, 188 insertions(+), 19 deletions(-) create mode 100644 src/2024/day07.lisp create mode 100644 tests/2024/day07-test.lisp diff --git a/aoc.asd b/aoc.asd index ef5766e..95f5555 100644 --- a/aoc.asd +++ b/aoc.asd @@ -30,6 +30,7 @@ (:file "2024/day04") (:file "2024/day05") (:file "2024/day06") + (:file "2024/day07") ))) :description "Advent of Code challenges and solutions." :long-description "Solutions for the AOC challenges." @@ -52,6 +53,7 @@ (:file "2024/day04-test") (:file "2024/day05-test") (:file "2024/day06-test") + (:file "2024/day07-test") ))) :description "Test system for aoc" :perform (test-op (op c) (symbol-call :parachute :test :aoc/tests))) diff --git a/src/2024/day05.lisp b/src/2024/day05.lisp index 9f1b31a..9dc63af 100644 --- a/src/2024/day05.lisp +++ b/src/2024/day05.lisp @@ -2,7 +2,6 @@ (:use :cl :aoc :alexandria :trivia) (:export #:sample-data - #:sample-data2 #:part1 #:part2 #:rule-okp diff --git a/src/2024/day06.lisp b/src/2024/day06.lisp index a549603..b919785 100644 --- a/src/2024/day06.lisp +++ b/src/2024/day06.lisp @@ -1,5 +1,3 @@ - ;(declaim (optimize (speed 3) (debug 0) (safety 0))) - (defpackage :aoc/2024/06 (:use :cl :aoc :alexandria :trivia) (:export @@ -12,24 +10,16 @@ (in-package :aoc/2024/06) -(defun list-of-strings-p (list) - "Return t if LIST is non nil and contains only strings." - (and (consp list) - (every #'stringp list))) - -(deftype list-of-strings () - `(satisfies list-of-strings-p)) -(declaim (ftype (function (list-of-strings) (simple-array standard-char (* *))))) + + (defun parse-input (lines) (let ((l (length lines))) (make-array (list l l) :initial-contents (mapcar (lambda (s) (coerce s 'list)) lines)))) -(declaim (type list-of-strings input-text)) (defparameter input-text (test-input 2024 6)) -(declaim (type (simple-array standard-char (* *)) input-data)) (defparameter input-data (parse-input (test-input 2024 6))) diff --git a/src/2024/day07.lisp b/src/2024/day07.lisp new file mode 100644 index 0000000..548de40 --- /dev/null +++ b/src/2024/day07.lisp @@ -0,0 +1,128 @@ +(defpackage :aoc/2024/07 + (:use :cl :aoc :alexandria :trivia) + (:export + #:sample-data + #:sample-data2 + #:part1 + #:part2 + #:parse-line + #:make-equation + #:equation-result + #:equation-operands + #:mul-branch + #:plus-branch + #:plus-branch + #:solvedp + #:valid-equation + #:concat-branch + )) + +(in-package :aoc/2024/07) + + + + +(defstruct equation result operands) + + +(defun parse-line (line) + "return an equation with the result and the operands in reverse order as in the text" + (let ((ns (mapcar #'parse-integer (ppcre:split ":? " line)))) + (make-equation :result (first ns) :operands (reverse (cdr ns))))) + + +(defun parse-input (lines) + (mapcar #'parse-line lines)) + +(defparameter input-text (test-input 2024 7)) +(defparameter input-data (parse-input input-text)) + +(defparameter sample-text (aoc:split-lines "190: 10 19 +3267: 81 40 27 +83: 17 5 +156: 15 6 +7290: 6 8 6 15 +161011: 16 10 13 +192: 17 8 14 +21037: 9 7 18 13 +292: 11 6 16 20")) +(defparameter sample-data + (parse-input sample-text)) + + +(defun mul-branch (equation) + (let ((r (equation-result equation)) + (os (equation-operands equation))) + (if (= (gcd r (first os)) (first os)) + (make-equation :result (/ r (first os)) :operands (cdr os)) + nil))) + +(defun plus-branch (equation) + (let ((r (equation-result equation)) + (os (equation-operands equation))) + (if (>= r(first os)) + (make-equation :result (- r (first os)) :operands (cdr os))))) + +(defun solvedp (equation) + (let ((r (equation-result equation)) + (os (equation-operands equation))) + (and + (= (length os) 1) + (= r (first os))))) + +(defun valid-equation (equation) + (if (equation-operands equation) + (let ((mb (mul-branch equation)) + (pb (plus-branch equation))) + (or + (solvedp equation) + (and mb (valid-equation mb)) + (and pb (valid-equation pb)))) + nil)) + + +(defun sum-valid-eqs (eqs) + (loop + for eq in eqs + if (valid-equation eq) + sum (equation-result eq))) + +(defun part1 (data) + (format nil "~A" (sum-valid-eqs data))) + +(defun wrapping-power-of-10 (x) + (expt 10 (ceiling (log (1+ x) 10)))) + +(defun concat-branch (equation) + (let* ((r (equation-result equation)) + (os (equation-operands equation)) + (o (first os)) + (wrap (wrapping-power-of-10 o))) + (and (= (mod r wrap) o) + (make-equation :result (floor (/ r wrap)) :operands (cdr os))))) + +(defun valid-equation-part2 (equation) + (if (equation-operands equation) + (let ((mb (mul-branch equation)) + (pb (plus-branch equation)) + (cb (concat-branch equation))) + (or + (solvedp equation) + (and cb (valid-equation-part2 cb)) + (and mb (valid-equation-part2 mb)) + (and pb (valid-equation-part2 pb)) + )) + nil)) + +(defun sum-valid-eqs-part2 (eqs) + (loop + for eq in eqs + if (valid-equation-part2 eq) + sum (equation-result eq))) + +(defun part2 (data) + (format nil "~A" (sum-valid-eqs-part2 data))) + +(defun solve-day () + (format t "part1: ~A~%" (part1 input-data)) + (format t "part2: ~A~%" (part2 input-data))) diff --git a/tests/2024/day05-test.lisp b/tests/2024/day05-test.lisp index ec68415..034f23c 100644 --- a/tests/2024/day05-test.lisp +++ b/tests/2024/day05-test.lisp @@ -9,15 +9,16 @@ (define-test test-middle-page :parent suite-2024-05 - (is = 61 (middle-page (75 47 61 53 29))) + (is = 61 (middle-page '(75 47 61 53 29))) ) (define-test test-order-pages - :parent suite-2025-05 - (is equalp (sort-with-rules rules '(75 97 47 61 53)) '(97 75 47 61 53)) - (is equalp (sort-with-rules rules '(61 13 29)) '(61 29 13)) - (is equalp (sort-with-rules rule '(97 13 75 29 47)) '(97 75 47 29 13))) + :parent suite-2024-05 + (let ((rules (first sample-data))) + (is equalp (sort-with-rules rules '(75 97 47 61 53)) '(97 75 47 61 53)) + (is equalp (sort-with-rules rules '(61 13 29)) '(61 29 13)) + (is equalp (sort-with-rules rules '(97 13 75 29 47)) '(97 75 47 29 13)))) (define-test+run test-part1 @@ -26,6 +27,6 @@ (define-test+run test-part2 :parent suite-2024-05 - (true (equalp nil (part2 sample-data2)))) + (true (equalp nil (part2 sample-data)))) diff --git a/tests/2024/day07-test.lisp b/tests/2024/day07-test.lisp new file mode 100644 index 0000000..e0df63f --- /dev/null +++ b/tests/2024/day07-test.lisp @@ -0,0 +1,49 @@ +(defpackage :aoc/2024/07/tests + (:use :cl :aoc :aoc/tests :aoc/2024/tests :parachute :aoc/2024/07)) + +(in-package :aoc/2024/07/tests) + +(define-test suite-2024-07 + ;:parent suite-2024 + ) + + + +(define-test+run test-part1 + :parent suite-2024-07 + (true (equalp "3749" (part1 sample-data)))) + +(define-test+run test-part2 + :parent suite-2024-07 + (true (equalp "11387" (part2 sample-data)))) + + +(define-test test-parse-line + :parent suite-2024-07 + (is equalp (make-equation :result 1 :operands '(3 2)) (parse-line "1: 2 3"))) + +(define-test test-mul-branch + :parent suite-2024-07 + (is equalp (make-equation :result 10 :operands '(10)) (mul-branch (first sample-data))) + (is equalp nil (mul-branch (third sample-data)))) + +(define-test test-plus-branch + :parent suite-2024-07 + (is equalp (make-equation :result 171 :operands '(10)) (plus-branch (first sample-data))) + (is equalp (make-equation :result 78 :operands '(17)) (plus-branch (third sample-data)))) + +(define-test test-solvedp + :parent suite-2024-07 + (false (solvedp (first sample-data))) + (true (solvedp (make-equation :result 10 :operands '(10))))) + +(define-test test-valid-equation + :parent suite-2024-07 + (true (valid-equation (first sample-data))) + (false (valid-equation (third sample-data))) + ) + +(define-test test-concat-branch + :parent suite-2024-07 + (is equalp (make-equation :result 15 :operands '(15)) (concat-branch (nth 3 sample-data))) + (is equalp nil (concat-branch (third sample-data))))