diff --git a/src/2024/day12.lisp b/src/2024/day12.lisp index 426f477..a425e26 100644 --- a/src/2024/day12.lisp +++ b/src/2024/day12.lisp @@ -39,7 +39,7 @@ MIIIIIJJEE MIIISIJEEE MMMISSJEEE ")) - +(defparameter sample-data (parse-input sample-text)) (defun get-plant (plants y x) (if (array-in-bounds-p plants y x) @@ -72,7 +72,6 @@ MMMISSJEEE 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)) @@ -87,27 +86,53 @@ MMMISSJEEE 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) + for r = (aref garden-map y x) + if (< r 0) do (let ((id (incf region-id))) (loop for region in (find-region plants x y) + for x = (car region) + for y = (cdr region) do (setf (aref garden-map y x) id))))) 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 (parse-input sample-text)) (defun part1 (data) - nil) + (format nil "~A" (calculate-costs (calculate-info (scan-regions data))))) (defun part2 (data) - nil) + (length data)) (defun solve-day () (format t "part1: ~A~%" (part1 input-data)) diff --git a/src/main.lisp b/src/main.lisp index 542f333..3e9c0cc 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -13,7 +13,6 @@ (defparameter *aoc-url* "https://adventofcode.com") - (defparameter *cookie-jar* (cl-cookie:make-cookie-jar :cookies (list @@ -25,7 +24,6 @@ :domain ".adventofcode.com" :secure-p t)))) - (defun split-lines (s) (cl-ppcre:split "\\n" s)) diff --git a/tests/2024/day12-test.lisp b/tests/2024/day12-test.lisp index 323a564..0ed5b0d 100644 --- a/tests/2024/day12-test.lisp +++ b/tests/2024/day12-test.lisp @@ -22,7 +22,7 @@ (define-test+run test-part1 :parent suite-2024-12 - (is equal nil (part1 sample-data))) + (is equal "1930" (part1 sample-data))) (define-test+run test-part2 :parent suite-2024-12