solved day 12 part 1

This commit is contained in:
Peter Tillemans 2024-12-12 13:23:19 +01:00
parent a3d25c257f
commit b3518e5195
3 changed files with 35 additions and 12 deletions

View file

@ -39,7 +39,7 @@ MIIIIIJJEE
MIIISIJEEE MIIISIJEEE
MMMISSJEEE MMMISSJEEE
")) "))
(defparameter sample-data (parse-input sample-text))
(defun get-plant (plants y x) (defun get-plant (plants y x)
(if (array-in-bounds-p plants y x) (if (array-in-bounds-p plants y x)
@ -72,7 +72,6 @@ MMMISSJEEE
do (progn do (progn
(setf (aref seen y x) t) (setf (aref seen y x) t)
(setf queue (cdr queue))) (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 do (loop for p in neighbors
unless (aref seen (cdr p) (car p)) unless (aref seen (cdr p) (car p))
do (push p queue)) do (push p queue))
@ -87,27 +86,53 @@ MMMISSJEEE
for y from 0 below (array-dimension plants 1) for y from 0 below (array-dimension plants 1)
do (loop do (loop
for x from 0 below (array-dimension plants 0) for x from 0 below (array-dimension plants 0)
do (format t "~A,") for r = (aref garden-map y x)
if (< (aref garden-map y x) 0) if (< r 0)
do (let ((id (incf region-id))) do (let ((id (incf region-id)))
(loop (loop
for region in (find-region plants x y) for region in (find-region plants x y)
for x = (car region)
for y = (cdr region)
do (setf (aref garden-map y x) id))))) do (setf (aref garden-map y x) id)))))
garden-map)) garden-map))
(defun circumference (garden x y)
(let ((id (aref garden y x)))
(- 4
(if (eq id (get-plant garden (1- y) x)) 1 0)
(if (eq id (get-plant garden (1+ y) x)) 1 0)
(if (eq id (get-plant garden y (1- x))) 1 0)
(if (eq id (get-plant garden y (1+ x))) 1 0)
)))
(defun calculate-info (garden)
(let ((info (make-hash-table)))
(loop
for y from 0 below (array-dimension garden 1)
do (loop
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)) (+ (circumference garden x y) (cdr d))))))
)
info))
(defun calculate-costs (info)
(loop for k being the hash-keys in info
for v being the hash-values in info
sum (* (car v) (cdr v))))
(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))
(defun part1 (data) (defun part1 (data)
nil) (format nil "~A" (calculate-costs (calculate-info (scan-regions data)))))
(defun part2 (data) (defun part2 (data)
nil) (length data))
(defun solve-day () (defun solve-day ()
(format t "part1: ~A~%" (part1 input-data)) (format t "part1: ~A~%" (part1 input-data))

View file

@ -13,7 +13,6 @@
(defparameter *aoc-url* "https://adventofcode.com") (defparameter *aoc-url* "https://adventofcode.com")
(defparameter *cookie-jar* (defparameter *cookie-jar*
(cl-cookie:make-cookie-jar (cl-cookie:make-cookie-jar
:cookies (list :cookies (list
@ -25,7 +24,6 @@
:domain ".adventofcode.com" :domain ".adventofcode.com"
:secure-p t)))) :secure-p t))))
(defun split-lines (s) (defun split-lines (s)
(cl-ppcre:split "\\n" s)) (cl-ppcre:split "\\n" s))

View file

@ -22,7 +22,7 @@
(define-test+run test-part1 (define-test+run test-part1
:parent suite-2024-12 :parent suite-2024-12
(is equal nil (part1 sample-data))) (is equal "1930" (part1 sample-data)))
(define-test+run test-part2 (define-test+run test-part2
:parent suite-2024-12 :parent suite-2024-12