commit 7bd93562fa1827db036c3b25cfe787b809b12669 parent fbf3f987e153a05c6fed487dfd94c2ce5677b575 Author: Lukas Henkel <lh@entf.net> Date: Tue, 10 Dec 2024 06:24:32 +0100 Day 10 Diffstat:
A | input/10.txt | | | 46 | ++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/day-10.lisp | | | 52 | ++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | t/day-10.lisp | | | 64 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 162 insertions(+), 0 deletions(-)
diff --git a/input/10.txt b/input/10.txt @@ -0,0 +1,46 @@ +6543298761232134567654321234365430109789210765 +7630165450545023698743430143278121218654307876 +8781074305656712587412945056189054327610128965 +9692181212789803654307876567098769016501201234 +8543290325654908743216521098778978039432343210 +1012107812343219210127432101065432128765464569 +8703456901432101021858921098123498012346678978 +9698767876545832210961012567898567109656541066 +0543678989836943367878983456547010478765432127 +1234569834787894450123672561032123567345345698 +8705450125690765645674501870129854438936216787 +9612325678501234734983465985438763520127303456 +0549814789456789821567874376579654011298012367 +1236703210321098980432989210189503567885410198 +0145610123010123671301065123679012456956328765 +2125898016134564547219874034578654320167019954 +1034567667821078998765723410165743210298907823 +2101215454910899789054010567234894109347876910 +3432109323856789670123120198654301078456585410 +4569218012541008765434103296785212562345694321 +5678107873432210894120254589698943421034783210 +6760256976501345703091345678899874014345115432 +5801345488989856912782987010710762321236006781 +4912352397430767824653456123601051430347112390 +3983401256521956543241089234512340554398203454 +2876510342107867852130176541329429663294345965 +1089459656236238901021287430458218776187654874 +2012368789845121652012396521567309889076567876 +3001871656903030743189465432456456776985678945 +2100980347812349876676576321078944567034569432 +3231291210101656734589689234561323498123253211 +4945387667892109825678710105430212234510104500 +5876456108743276710265431016521001107601067601 +6700123219654985610176721037815410098432188932 +5217634508989414323789872348906321016501098543 +4398549607678501234567961059817892107890107676 +3210498714565430104498854367876765226981238985 +4782107123645621223421343458987014312876543214 +5693898034564789014530012121898123103987454103 +8764796543673298987645430010987035654036567812 +9855687234982187656786521027876546789145658901 +8544590127651012565497890038985589690237849878 +1234210098545671434387912147104676521056932569 +0110367653234780301275403456203465432345691410 +3223458910125691215676323569312394341210780323 +4334389871210101234981019878456787430101671410 diff --git a/src/day-10.lisp b/src/day-10.lisp @@ -0,0 +1,52 @@ +(defpackage #:aoc/day-10 + (:use #:cl #:aoc/utils) + (:export #:day-10)) +(in-package #:aoc/day-10) + +(defun find-trailheads (map) + (loop for y from 0 below (input-map-height map) + nconc (loop for x from 0 below (input-map-width map) + for point = (cons x y) + for cell = (map-cell map point) + when (char= cell #\0) + collect point))) + +(defparameter *trail-neighbours* + (list (cons 0 -1) + (cons -1 0) + (cons 1 0) + (cons 0 1))) + +(defun score (map trailhead) + (let ((map-width (input-map-width map)) + (map-height (input-map-height map)) + (points (make-hash-table :test #'equal)) + (rating 0)) + (labels ((%walk (pos) + (when (char= (map-cell map pos) #\9) + (setf (gethash pos points) t) + (incf rating) + (return-from %walk nil)) + (loop with height = (char-number (map-cell map pos)) + for np in (loop for nd in *trail-neighbours* + for np = (point+ pos nd) + when (point-in-bounds-p np map-width map-height) + collect np) + for nc = (map-cell map np) + when (and (not (char= nc #\.)) + (= (- (char-number nc) + height) + 1)) + do (%walk np)))) + (%walk trailhead) + (values (length (hash-table-keys points)) + rating)))) + +(defun day-10 (input) + (loop with map = (make-map input) + with trailheads = (find-trailheads map) + for trailhead in trailheads + for (score rating) = (multiple-value-list (score map trailhead)) + sum score into task-1 + sum rating into task-2 + finally (return (values task-1 task-2)))) diff --git a/t/day-10.lisp b/t/day-10.lisp @@ -0,0 +1,64 @@ +(defpackage #:aoc-test/day-10 + (:use #:cl #:lisp-unit2) + (:import-from #:aoc/day-10)) +(in-package #:aoc-test/day-10) + +(define-test test-day-10 + () + (assert= 2 (aoc:run-day 10 "...0... +...1... +...2... +6543456 +7.....7 +8.....8 +9.....9")) + (assert= 4 (aoc:run-day 10 "..90..9 +...1.98 +...2..7 +6543456 +765.987 +876.... +987....")) + (assert= 3 (aoc:run-day 10 "10..9.. +2...8.. +3...7.. +4567654 +...8..3 +...9..2 +.....01")) + (assert= 36 (aoc:run-day 10 "89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732")) + (assert= 3 (nth-value 1 (aoc:run-day 10 ".....0. +..4321. +..5..2. +..6543. +..7..4. +..8765. +..9...."))) + (assert= 13 (nth-value 1 (aoc:run-day 10 "..90..9 +...1.98 +...2..7 +6543456 +765.987 +876.... +987...."))) + (assert= 227 (nth-value 1 (aoc:run-day 10 "012345 +123456 +234567 +345678 +4.6789 +56789."))) + (assert= 81 (nth-value 1 (aoc:run-day 10 "89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732"))))