solved day 7, fixed compilation errors preventing loading

This commit is contained in:
Peter Tillemans 2024-12-07 09:54:28 +01:00
parent 8d52daa00c
commit f5b144ce21
6 changed files with 188 additions and 19 deletions

View file

@ -30,6 +30,7 @@
(:file "2024/day04") (:file "2024/day04")
(:file "2024/day05") (:file "2024/day05")
(:file "2024/day06") (:file "2024/day06")
(:file "2024/day07")
))) )))
:description "Advent of Code challenges and solutions." :description "Advent of Code challenges and solutions."
:long-description "Solutions for the AOC challenges." :long-description "Solutions for the AOC challenges."
@ -52,6 +53,7 @@
(:file "2024/day04-test") (:file "2024/day04-test")
(:file "2024/day05-test") (:file "2024/day05-test")
(:file "2024/day06-test") (:file "2024/day06-test")
(:file "2024/day07-test")
))) )))
:description "Test system for aoc" :description "Test system for aoc"
:perform (test-op (op c) (symbol-call :parachute :test :aoc/tests))) :perform (test-op (op c) (symbol-call :parachute :test :aoc/tests)))

View file

@ -2,7 +2,6 @@
(:use :cl :aoc :alexandria :trivia) (:use :cl :aoc :alexandria :trivia)
(:export (:export
#:sample-data #:sample-data
#:sample-data2
#:part1 #:part1
#:part2 #:part2
#:rule-okp #:rule-okp

View file

@ -1,5 +1,3 @@
;(declaim (optimize (speed 3) (debug 0) (safety 0)))
(defpackage :aoc/2024/06 (defpackage :aoc/2024/06
(:use :cl :aoc :alexandria :trivia) (:use :cl :aoc :alexandria :trivia)
(:export (:export
@ -12,24 +10,16 @@
(in-package :aoc/2024/06) (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) (defun parse-input (lines)
(let ((l (length lines))) (let ((l (length lines)))
(make-array (list l l) (make-array (list l l)
:initial-contents (mapcar (lambda (s) (coerce s 'list)) lines)))) :initial-contents (mapcar (lambda (s) (coerce s 'list)) lines))))
(declaim (type list-of-strings input-text))
(defparameter input-text (test-input 2024 6)) (defparameter input-text (test-input 2024 6))
(declaim (type (simple-array standard-char (* *)) input-data))
(defparameter input-data (defparameter input-data
(parse-input (test-input 2024 6))) (parse-input (test-input 2024 6)))

128
src/2024/day07.lisp Normal file
View file

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

View file

@ -9,15 +9,16 @@
(define-test test-middle-page (define-test test-middle-page
:parent suite-2024-05 :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 (define-test test-order-pages
:parent suite-2025-05 :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 '(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 '(61 13 29)) '(61 29 13))
(is equalp (sort-with-rules rule '(97 13 75 29 47)) '(97 75 47 29 13))) (is equalp (sort-with-rules rules '(97 13 75 29 47)) '(97 75 47 29 13))))
(define-test+run test-part1 (define-test+run test-part1
@ -26,6 +27,6 @@
(define-test+run test-part2 (define-test+run test-part2
:parent suite-2024-05 :parent suite-2024-05
(true (equalp nil (part2 sample-data2)))) (true (equalp nil (part2 sample-data))))

View file

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