From a3d25c257f4f04aee6ad29c3d1d4b3d7c4a73b42 Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Thu, 12 Dec 2024 09:04:24 +0100 Subject: [PATCH] save ongoing solution --- src/2024/day12.lisp | 88 +++++++++++++++++++++++++++++++++++--- tests/2024/day12-test.lisp | 28 ++++++------ 2 files changed, 98 insertions(+), 18 deletions(-) diff --git a/src/2024/day12.lisp b/src/2024/day12.lisp index 7c6e964..426f477 100644 --- a/src/2024/day12.lisp +++ b/src/2024/day12.lisp @@ -6,24 +6,102 @@ #:sample-data2 #:part1 #:part2 + #:find-same-neighbors + #:get-plant + #:find-region + #:scan-regions )) (in-package :aoc/2024/12) (defun parse-line (line) - line) + (coerce line 'list)) (defun parse-input (lines) - (mapcar #'parse-line lines)) + (let ((garden (mapcar #'parse-line lines))) + (make-array (list (length (first garden)) (length garden)) + :initial-contents garden)) + ) -(defparameter input-text (first (test-input 2024 12))) +(defparameter input-text (test-input 2024 12)) (defparameter input-data (parse-input input-text)) -(defparameter sample-text (aoc:split-lines "")) +(defparameter sample-text (aoc:split-lines "RRRRIICCFF +RRRRIICCCF +VVRRRCCFFF +VVRCCCJFFF +VVVVCJJCFE +VVIVCCJJEE +VVIIICJJEE +MIIIIIJJEE +MIIISIJEEE +MMMISSJEEE +")) + + +(defun get-plant (plants y x) + (if (array-in-bounds-p plants y x) + (aref plants y x) + nil)) + + + +(defun find-same-neighbors (plants x y) + (let ((plant (get-plant plants y x))) + (loop + for dx in '(-1 0 1 0) + for dy in '(0 -1 0 1) + for tx = (+ x dx) + for ty = (+ y dy) + if (eq plant (get-plant plants ty tx)) + collect (cons tx ty) + ))) + +(defun find-region (plants x y) + (let ((seen (make-array (array-dimensions plants) :initial-element nil)) + (queue (list (cons x y)))) + (loop + for p = (first queue) + for x = (car p) + for y = (cdr p) + for neighbors = (find-same-neighbors plants x y) + if (not (aref seen y x)) + collect (cons x y) + do (progn + (setf (aref seen y x) t) + (setf queue (cdr queue))) + do (format t "x: ~A, y: ~A, queue: ~A, neighbors: ~A~%" x y queue neighbors) + do (loop for p in neighbors + unless (aref seen (cdr p) (car p)) + do (push p queue)) + + until (emptyp queue) + ))) + +(defun scan-regions (plants) + (let ((region-id 0) + (garden-map (make-array (array-dimensions plants) :initial-element -1))) + (loop + for y from 0 below (array-dimension plants 1) + do (loop + for x from 0 below (array-dimension plants 0) + do (format t "~A,") + if (< (aref garden-map y x) 0) + do (let ((id (incf region-id))) + (loop + for region in (find-region plants x y) + do (setf (aref garden-map y x) id))))) + garden-map)) + + +(defun fence-cost (plants) + (loop for region being the hash-values of (scan-garden plants) + sum (let ((info (cdr region))) (* (car info) (cdr info))))) + (defparameter sample-data - (parse-input sample-text)) + (parse-input sample-text)) (defun part1 (data) nil) diff --git a/tests/2024/day12-test.lisp b/tests/2024/day12-test.lisp index 9d89646..323a564 100644 --- a/tests/2024/day12-test.lisp +++ b/tests/2024/day12-test.lisp @@ -4,24 +4,26 @@ (in-package :aoc/2024/12/tests) (define-test suite-2024-12 - ;:parent suite-2024 - ) + ; + ;:parent suite-2024 + ;) + ) -(define-test test-foo - :parent suite-2024-12 - ) +(define-test test-find-same-neighbors + :parent suite-2024-12 + (is equal '((1 . 0) (0 . 1)) (find-same-neighbors sample-data 0 0)) + + (is equal '((2 . 2) (3 . 1) (4 . 2)) (find-same-neighbors sample-data 3 2)) + ) (define-test test-bar - :parent suite-2024-12 - ) - - + :parent suite-2024-12) (define-test+run test-part1 - :parent suite-2024-12 - (is equal nil (part1 sample-data))) + :parent suite-2024-12 + (is equal nil (part1 sample-data))) (define-test+run test-part2 - :parent suite-2024-12 - (is equal nil (part2 sample-data))) + :parent suite-2024-12 + (is equal nil (part2 sample-data)))