From 785f8f382a2c5d41ee520d4739af79b147926de2 Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Thu, 12 Dec 2024 14:22:21 +0100 Subject: [PATCH] working on part2 --- src/2024/day12.lisp | 30 ++++++++++++++++++++++++------ tests/2024/day12-test.lisp | 21 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/2024/day12.lisp b/src/2024/day12.lisp index a425e26..37b4841 100644 --- a/src/2024/day12.lisp +++ b/src/2024/day12.lisp @@ -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)) diff --git a/tests/2024/day12-test.lisp b/tests/2024/day12-test.lisp index 0ed5b0d..5f91fd2 100644 --- a/tests/2024/day12-test.lisp +++ b/tests/2024/day12-test.lisp @@ -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)))