working on part2
This commit is contained in:
parent
b3518e5195
commit
785f8f382a
2 changed files with 44 additions and 7 deletions
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
(defpackage :aoc/2024/12
|
(defpackage :aoc/2024/12
|
||||||
(:use :cl :aoc :alexandria :trivia :lla)
|
(:use :cl :aoc :alexandria :trivia :lla)
|
||||||
(:export
|
(:export
|
||||||
|
@ -6,10 +5,12 @@
|
||||||
#:sample-data2
|
#:sample-data2
|
||||||
#:part1
|
#:part1
|
||||||
#:part2
|
#:part2
|
||||||
|
#:top-side-p
|
||||||
#:find-same-neighbors
|
#:find-same-neighbors
|
||||||
#:get-plant
|
#:get-plant
|
||||||
#:find-region
|
#:find-region
|
||||||
#:scan-regions
|
#:scan-regions
|
||||||
|
#:sides
|
||||||
))
|
))
|
||||||
|
|
||||||
(in-package :aoc/2024/12)
|
(in-package :aoc/2024/12)
|
||||||
|
@ -46,8 +47,6 @@ MMMISSJEEE
|
||||||
(aref plants y x)
|
(aref plants y x)
|
||||||
nil))
|
nil))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defun find-same-neighbors (plants x y)
|
(defun find-same-neighbors (plants x y)
|
||||||
(let ((plant (get-plant plants y x)))
|
(let ((plant (get-plant plants y x)))
|
||||||
(loop
|
(loop
|
||||||
|
@ -105,7 +104,7 @@ MMMISSJEEE
|
||||||
(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)
|
(defun calculate-info (garden f)
|
||||||
(let ((info (make-hash-table)))
|
(let ((info (make-hash-table)))
|
||||||
(loop
|
(loop
|
||||||
for y from 0 below (array-dimension garden 1)
|
for y from 0 below (array-dimension garden 1)
|
||||||
|
@ -113,7 +112,7 @@ MMMISSJEEE
|
||||||
for x from 0 below (array-dimension garden 0)
|
for x from 0 below (array-dimension garden 0)
|
||||||
do (let* ((id (aref garden y x))
|
do (let* ((id (aref garden y x))
|
||||||
(d (gethash id info (cons 0 0))))
|
(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))
|
info))
|
||||||
|
|
||||||
|
@ -129,7 +128,26 @@ MMMISSJEEE
|
||||||
(parse-input sample-text))
|
(parse-input sample-text))
|
||||||
|
|
||||||
(defun part1 (data)
|
(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)
|
(defun part2 (data)
|
||||||
(length data))
|
(length data))
|
||||||
|
|
|
@ -20,10 +20,29 @@
|
||||||
(define-test test-bar
|
(define-test test-bar
|
||||||
:parent suite-2024-12)
|
: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
|
(define-test+run test-part1
|
||||||
:parent suite-2024-12
|
:parent suite-2024-12
|
||||||
(is equal "1930" (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
|
||||||
(is equal nil (part2 sample-data)))
|
(is equal "1206" (part2 sample-data)))
|
||||||
|
|
Loading…
Reference in a new issue