From e74dd696dcea6de65e687aeddff33ffcfbea7f05 Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Fri, 13 Dec 2024 06:01:50 +0100 Subject: [PATCH] solve day12 start 13 --- aoc.asd | 2 + src/2024/day12.lisp | 36 ++++++++++++--- src/2024/day13.lisp | 42 +++++++++++++++++ tests/2024/day12-test.lisp | 95 ++++++++++++++++++++++++++++++++++++-- tests/2024/day13-test.lisp | 27 +++++++++++ 5 files changed, 191 insertions(+), 11 deletions(-) create mode 100644 src/2024/day13.lisp create mode 100644 tests/2024/day13-test.lisp diff --git a/aoc.asd b/aoc.asd index d60ddc2..afe12c3 100644 --- a/aoc.asd +++ b/aoc.asd @@ -37,6 +37,7 @@ (:file "2024/day10") (:file "2024/day11") (:file "2024/day12") + (:file "2024/day13") ))) :description "Advent of Code challenges and solutions." :long-description "Solutions for the AOC challenges." @@ -65,6 +66,7 @@ (:file "2024/day10-test") (:file "2024/day11-test") (:file "2024/day12-test") + (:file "2024/day13-test") ))) :description "Test system for aoc" :perform (test-op (op c) (symbol-call :parachute :test :aoc/tests))) diff --git a/src/2024/day12.lisp b/src/2024/day12.lisp index 37b4841..35d7a29 100644 --- a/src/2024/day12.lisp +++ b/src/2024/day12.lisp @@ -5,11 +5,14 @@ #:sample-data2 #:part1 #:part2 - #:top-side-p #:find-same-neighbors #:get-plant #:find-region #:scan-regions + #:top-side-p + #:left-side-p + #:right-side-p + #:bottom-side-p #:sides )) @@ -112,7 +115,7 @@ MMMISSJEEE for x from 0 below (array-dimension garden 0) do (let* ((id (aref garden y x)) (d (gethash id info (cons 0 0)))) - (setf (gethash id info) (cons (1+ (car d)) (+ (f garden x y) (cdr d)))))) + (setf (gethash id info) (cons (1+ (car d)) (+ (apply f (list garden x y)) (cdr d)))))) ) info)) @@ -144,13 +147,32 @@ MMMISSJEEE (lu (eq id (get-plant garden (1- y) (1- x)))) (up (eq id (get-plant garden (1- y) x))) (left (eq id (get-plant garden y (1- x))))) - (+ - (if (and (or lu (not left)) (not up)) 1 0) ; top side - (if (and (not left) (not lu)) 1 0) ; left side - ))) + (and (or lu (not up)) (not left)))) + +(defun right-side-p (garden x y) + (let* ((id (get-plant garden y x)) + (ru (eq id (get-plant garden (1- y) (1+ x)))) + (up (eq id (get-plant garden (1- y) x))) + (right (eq id (get-plant garden y (1+ x))))) + (and (or ru (not up)) (not right)))) + +(defun bottom-side-p (garden x y) + (let* ((id (get-plant garden y x)) + (lb (eq id (get-plant garden (1+ y) (1- x)))) + (bottom (eq id (get-plant garden (1+ y) x))) + (left (eq id (get-plant garden y (1- x))))) + (and (or lb (not left)) (not bottom)))) + +(defun sides (garden x y) + (+ + (if (top-side-p garden x y) 1 0) + (if (left-side-p garden x y) 1 0) + (if (right-side-p garden x y) 1 0) + (if (bottom-side-p garden x y) 1 0) + )) (defun part2 (data) - (length data)) + (format nil "~A" (calculate-costs (calculate-info (scan-regions data) #'si)))) (defun solve-day () (format t "part1: ~A~%" (part1 input-data)) diff --git a/src/2024/day13.lisp b/src/2024/day13.lisp new file mode 100644 index 0000000..1678c74 --- /dev/null +++ b/src/2024/day13.lisp @@ -0,0 +1,42 @@ + +(defpackage :aoc/2024/13 + (:use :cl :aoc :alexandria :trivia :lla) + (:export + #:sample-data + #:sample-data2 + #:part1 + #:part2 + )) + +(in-package :aoc/2024/13) + + +(defun parse-line (line) + line) + + +(defun parse-input (lines) + (mapcar #'parse-line lines)) + +(defparameter input-text (first (test-input 2024 13))) +(defparameter input-data (parse-input input-text)) + +(defparameter sample-text (aoc:split-lines "")) +(defparameter sample-data + (parse-input sample-text)) + +(defun part1 (data) + nil) + +(defun part2 (data) + nil) + +(defun solve-day () + (format t "part1: ~A~%" (part1 input-data)) + (format t "part2: ~A~%" (part2 input-data))) + +(defun submit () + (let ((p1 (part1 input-data)) + (p2 (part2 input-data))) + (if p1 (submit-part1 2024 13 p1)) + (if p2 (submit-part2 2024 13 p2)))) diff --git a/tests/2024/day12-test.lisp b/tests/2024/day12-test.lisp index 5f91fd2..6b1c81b 100644 --- a/tests/2024/day12-test.lisp +++ b/tests/2024/day12-test.lisp @@ -23,9 +23,9 @@ (define-test test-top-side-p :parent suite-2024-12 - (true (top-side-p (make-array '(3 3) :initial-contents '((0 0 0) - (0 0 0) - (0 0 0))) 1 1)) + (false (top-side-p (make-array '(3 3) :initial-contents '((0 0 0) + (0 0 0) + (0 0 0))) 1 1)) (true (top-side-p (make-array '(3 3) :initial-contents '((1 0 0) (1 1 1) (1 1 1))) 1 1)) @@ -37,7 +37,94 @@ (1 1 1))) 1 1)) (false (top-side-p (make-array '(3 3) :initial-contents '((0 1 0) (0 1 1) - (1 1 1))) 1 1))) + (1 1 1))) 1 1)) + (true (top-side-p (make-array '(3 3) :initial-contents '((0 1 0) + (1 1 1) + (1 1 1))) 1 0)) + (false (top-side-p (make-array '(3 3) :initial-contents '((1 1 0) + (1 1 1) + (1 1 1))) 1 0)) + ) + +(define-test test-bottom-side-p + :parent suite-2024-12 + (false (bottom-side-p (make-array '(3 3) :initial-contents '((0 0 0) + (0 0 0) + (0 0 0))) 1 1)) + (true (bottom-side-p (make-array '(3 3) :initial-contents '((1 1 1) + (1 1 1) + (1 0 0))) 1 1)) + (false (bottom-side-p (make-array '(3 3) :initial-contents '((1 1 1) + (1 1 1) + (0 0 0))) 1 1)) + (true (bottom-side-p (make-array '(3 3) :initial-contents '((0 0 0) + (0 1 1) + (0 0 0))) 1 1)) + (false (bottom-side-p (make-array '(3 3) :initial-contents '((0 1 1) + (0 1 1) + (0 1 0))) 1 1)) + (true (bottom-side-p (make-array '(3 3) :initial-contents '((0 1 0) + (1 1 1) + (0 1 0))) 1 2)) + (false (bottom-side-p (make-array '(3 3) :initial-contents '((1 1 0) + (1 1 1) + (1 1 1))) 1 2)) + ) + +(define-test test-left-side-p + :parent suite-2024-12 + (false (left-side-p (make-array '(3 3) :initial-contents '((0 0 0) + (0 0 0) + (0 0 0))) 1 1)) + (true (left-side-p (make-array '(3 3) :initial-contents '((1 1 1) + (0 1 1) + (0 1 1))) 1 1)) + (false (left-side-p (make-array '(3 3) :initial-contents '((0 1 1) + (0 1 1) + (1 1 1))) 1 1)) + (true (left-side-p (make-array '(3 3) :initial-contents '((0 0 1) + (0 1 1) + (1 1 1))) 1 1)) + (false (left-side-p (make-array '(3 3) :initial-contents '((0 1 0) + (1 1 1) + (0 1 1))) 1 1))) + +(define-test test-right-side-p + :parent suite-2024-12 + (false (right-side-p (make-array '(3 3) :initial-contents '((0 0 0) + (0 0 0) + (0 0 0))) 1 1)) + (true (right-side-p (make-array '(3 3) :initial-contents '((1 1 1) + (1 1 0) + (1 1 0))) 1 1)) + (false (right-side-p (make-array '(3 3) :initial-contents '((1 1 0) + (1 1 0) + (1 1 1))) 1 1)) + (true (right-side-p (make-array '(3 3) :initial-contents '((1 0 0) + (1 1 0) + (1 1 1))) 1 1)) + (false (right-side-p (make-array '(3 3) :initial-contents '((0 1 0) + (1 1 1) + (1 1 0))) 1 1))) + + + +(define-test test-sides + (is = 4 (sides (make-array '(3 3) :initial-contents '((0 0 0) + (0 1 0) + (0 0 0))) 1 1)) + (is = 1 (sides (make-array '(3 3) :initial-contents '((0 1 0) + (0 1 0) + (0 0 0))) 1 1)) + (is = 3 (sides (make-array '(3 3) :initial-contents '((0 0 0) + (0 1 0) + (0 1 0))) 1 1)) + (is = 1 (sides (make-array '(3 3) :initial-contents '((0 0 0) + (1 1 0) + (0 0 0))) 1 1)) + (is = 3 (sides (make-array '(3 3) :initial-contents '((0 0 0) + (0 1 1) + (0 0 0))) 1 1)) ) (define-test+run test-part1 :parent suite-2024-12 diff --git a/tests/2024/day13-test.lisp b/tests/2024/day13-test.lisp new file mode 100644 index 0000000..c606d8a --- /dev/null +++ b/tests/2024/day13-test.lisp @@ -0,0 +1,27 @@ +(defpackage :aoc/2024/13/tests + (:use :cl :aoc :aoc/tests :aoc/2024/tests :parachute :aoc/2024/13)) + +(in-package :aoc/2024/13/tests) + +(define-test suite-2024-13 + ;:parent suite-2024 + ) + +(define-test test-foo + :parent suite-2024-13 + ) + + +(define-test test-bar + :parent suite-2024-13 + ) + + + +(define-test+run test-part1 + :parent suite-2024-13 + (is equal nil (part1 sample-data))) + +(define-test+run test-part2 + :parent suite-2024-13 + (is equal nil (part2 sample-data)))