working on part2

This commit is contained in:
Peter Tillemans 2024-12-12 14:22:21 +01:00
parent b3518e5195
commit 785f8f382a
2 changed files with 44 additions and 7 deletions

View file

@ -1,4 +1,3 @@
(defpackage :aoc/2024/12
(:use :cl :aoc :alexandria :trivia :lla)
(:export
@ -6,10 +5,12 @@
#:sample-data2
#:part1
#:part2
#:top-side-p
#:find-same-neighbors
#:get-plant
#:find-region
#:scan-regions
#:sides
))
(in-package :aoc/2024/12)
@ -46,8 +47,6 @@ MMMISSJEEE
(aref plants y x)
nil))
(defun find-same-neighbors (plants x y)
(let ((plant (get-plant plants y x)))
(loop
@ -105,7 +104,7 @@ MMMISSJEEE
(if (eq id (get-plant garden y (1+ x))) 1 0)
)))
(defun calculate-info (garden)
(defun calculate-info (garden f)
(let ((info (make-hash-table)))
(loop
for y from 0 below (array-dimension garden 1)
@ -113,7 +112,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)) (+ (circumference garden x y) (cdr d))))))
(setf (gethash id info) (cons (1+ (car d)) (+ (f garden x y) (cdr d))))))
)
info))
@ -129,7 +128,26 @@ MMMISSJEEE
(parse-input sample-text))
(defun part1 (data)
(format nil "~A" (calculate-costs (calculate-info (scan-regions data)))))
(format nil "~A" (calculate-costs (calculate-info (scan-regions data) #'circumference))))
(defun top-side-p (garden x y)
(let* ((id (get-plant garden y x))
(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)))))
(and (or lu (not left)) (not up))
))
(defun left-side-p (garden x y)
(let* ((id (get-plant garden y x))
(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
)))
(defun part2 (data)
(length data))

View file

@ -20,10 +20,29 @@
(define-test test-bar
:parent suite-2024-12)
(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))
(true (top-side-p (make-array '(3 3) :initial-contents '((1 0 0)
(1 1 1)
(1 1 1))) 1 1))
(false (top-side-p (make-array '(3 3) :initial-contents '((0 0 0)
(1 1 1)
(1 1 1))) 1 1))
(true (top-side-p (make-array '(3 3) :initial-contents '((0 0 0)
(0 1 1)
(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)))
(define-test+run test-part1
:parent suite-2024-12
(is equal "1930" (part1 sample-data)))
(define-test+run test-part2
:parent suite-2024-12
(is equal nil (part2 sample-data)))
(is equal "1206" (part2 sample-data)))