save ongoing solution
This commit is contained in:
parent
ab4267bf46
commit
a3d25c257f
2 changed files with 98 additions and 18 deletions
|
@ -6,22 +6,100 @@
|
||||||
#:sample-data2
|
#:sample-data2
|
||||||
#:part1
|
#:part1
|
||||||
#:part2
|
#:part2
|
||||||
|
#:find-same-neighbors
|
||||||
|
#:get-plant
|
||||||
|
#:find-region
|
||||||
|
#:scan-regions
|
||||||
))
|
))
|
||||||
|
|
||||||
(in-package :aoc/2024/12)
|
(in-package :aoc/2024/12)
|
||||||
|
|
||||||
|
|
||||||
(defun parse-line (line)
|
(defun parse-line (line)
|
||||||
line)
|
(coerce line 'list))
|
||||||
|
|
||||||
|
|
||||||
(defun parse-input (lines)
|
(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 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
|
(defparameter sample-data
|
||||||
(parse-input sample-text))
|
(parse-input sample-text))
|
||||||
|
|
||||||
|
|
|
@ -4,19 +4,21 @@
|
||||||
(in-package :aoc/2024/12/tests)
|
(in-package :aoc/2024/12/tests)
|
||||||
|
|
||||||
(define-test suite-2024-12
|
(define-test suite-2024-12
|
||||||
|
;
|
||||||
;:parent suite-2024
|
;:parent suite-2024
|
||||||
|
;)
|
||||||
)
|
)
|
||||||
|
|
||||||
(define-test test-foo
|
(define-test test-find-same-neighbors
|
||||||
:parent suite-2024-12
|
: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
|
(define-test test-bar
|
||||||
:parent suite-2024-12
|
:parent suite-2024-12)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(define-test+run test-part1
|
(define-test+run test-part1
|
||||||
:parent suite-2024-12
|
:parent suite-2024-12
|
||||||
|
|
Loading…
Reference in a new issue