advent-of-code-2023

My solutions to AoC 2023
git clone git://git.entf.net/advent-of-code-2023
Log | Files | Refs

commit 4d8a994d50d75ba86c7743d03f2975dec4a1a2f3
parent 2b2d4b999710dc9b7dd4dda4d89a5f6be7a9d463
Author: Lukas Henkel <lh@entf.net>
Date:   Sat, 16 Dec 2023 06:47:23 +0100

Day 16 task 1

Diffstat:
Ainput/16.txt | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day-16.lisp | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
At/day-16.lisp | 18++++++++++++++++++
3 files changed, 192 insertions(+), 0 deletions(-)

diff --git a/input/16.txt b/input/16.txt @@ -0,0 +1,110 @@ +\|.......|........|...../........................\.\.......\...-../..........\.........|...\......|........... +.|.../............|.....|\..../....-.\...........-............./......................|.-.-...........\.....-. +...............|........-....-...../..../|./.............\..|..................\..........|\.........../.\.... +....................|\..................\..................\/....................-....../................\.|.. +......././-......................................|.....|./.............................../.................... +.......................\.....|............-...\..|......-./...\.........../.............|..-......-...../..... +.....................-.......\.......|............./........\.......|........../............/................. +..................-................-./....................|....|...................-..................\....... +|.......././.................................|.....-............................-.........|......../../....... +.........../.......-........|................/......-....|.......\/..|..\...|..............-....\....|...-.... +............./-......./.....-./....\........./..|...............|..-......................-................... +......-\.\..|/......-.|.........-.......-..\|.............................\.-.............\...............\... +......\..../................\.........\..|...............-.......|..-|.....|-..........................-...... +.....\....../../..........\......-...........\.......-.\........./...........|\............-.............\.... +........\....................|........./...................|.........-.........\.............................. +.........-|.............\-.............................-..............|.....\../......|/...................... +..................../.......\/......./................../...../.......................|......|./.-......-|.... +..................../.........|..-.-./....../.|-..\..|........|......-.................\...\........./...\.... +....-...-....../...............................................|....................\..-..............-..|.-.\ +..........\..\.\....................-...|......................\......|......-.................\....|....../.. +\................../.....-./.......-...........\..../....../....|.........|........................|.......... +../......\.|..............................|../......................-.\..........\...............|..........|. +.|..............................-...-........|...-........|........../............\...../.\......-....|....... +............./...................\.\.........|...|../..........-.......\........../.-.-.....-..........-...... +..........................-.|\......|......./|.............../..../......--.....|.........|........../........ +/-....|/...-|..........\......................./..........-.....-.............\....../................\....... +............-../............\...............................-.........\...\...........|-....|................. +..............................|........-.........-..................|...........................-............. +........./...............//.........................................-..........|............|..../............ +...\..||../........................./................|......./.......|....|.....|............|................ +........./...................\-./.......\...../............../..........-|...................|..../.......|... +...../................/..-............../....................|/......................................-.|...... +\|........................................\...........-......../...\.|......|..\../........|.......\.......... +..|/......\....-...............||......//..................\\..-./...-....................|................... +./....../................\../.\.....\.........\.\\\..........................-..\..-............./-........... +........|..........-.....|..\..../....../.................../.............\...-.\...........\...........-..... +..........\...|/..|......|\/./...........-.|.................-.|....|....................../.................. +-.......................-.|.............-../......./.../...-|..|.........../../..\............\../..........|. +................./.........\..........-.........../..|.......//..............|..................|.....\....... +.............-../......\./.../.|.|.....-..|.....|...\.........|.....-/..................-................|.... +|.-\..../...........-.....|....................//........\............./....../........./../\..\....-......... +......\.|...........|..............|............/....-../.....\..................../...................|...... +../|....\....................................-.....--...|....../........-...................|...\.......\..... +..........|../..-.........-...........|....................../............/.......................|.|......... +........./..................\............................\..............|..................................\.. +...-|...........-......\|\...........\...\.........|..../......../..../..../..-|||................-........... +..|....-\..\......./...........\\.................................|......-./.............../........|..--..... +.................................../|...........-.......-...|..-............-........../...................... +.......|.............|.......................................................\...............\................ +........\.......|...........|............................\-......................................|............ +......|..\.-../......................|.-.../.....-..|.-.................................-.......\......./.|... +--\...../..........-...........\...................../.....|/............\..............-............./....... +......|-...............\........................................../.-\..|.....-........|..\./../.-..........|. +.........................//.....-...................|...|..-.\....|................|......\..-............/... +./.........-...-....................../......\\......||..........|....................--.................../.. +....-........\........................../..........|........./.........................\.\....|.......-|...... +.|\/...............|...-.|....|../..|...|...../......\..........|.............|........\.....|................ +...../.-\.-................|......../....-..\......|/.......\.......................\.......................|. +......................|..........-..../..../.\../................../......................|..............\|... +........../-\......|............................../....-......................................./.............. +./..........|.....-.............-..-...|../........\.-...........|.........................-................-. +.-....-................|..................-...-...........-........|.|..........-.-........................... +...|...............|.................\.......|.............../................\....|.............|.........\.. +..\..............|.............\..................\.......................|..........-........................ +../........./............\.\../.........................\../.....-....|....................-............../... +...\................\......../......................./\....\..-........\........./..\......................... +.........../..|.......-.........-|..............................................\...............-./\||...../.. +...............\........-..|..........||......................../..........-...../........................\... +...........\.....................-................................................................|..........| +./.........-.......-................................\..........||..|.|..\.........\.../..||......-.......\.... +\......\.........-..\......\...................-..|.....\.....\....................|..........-......-........ +.............|..........\..........-..................\.\..........|.....................|.............-/..... +........-................|......./..-...............\......|-...\.....\.............|./..\\../....-.../....... +..................\..||.............-........|............/.........\../...\..|/.....\.........\..........|... +..|../..................\..............................|...........................................-.......... +\..................../.|.......\...|-............../......\.............././..-...................|........... +......./..............................\..................|................\............./....-.....-.......... +..............|.........../...............................\.................\..|........-.......-.\.....|..... +.................|.....\........................................-.\.\..\...................//.....\./......... +....../..........|..-............/..........\............../.............|.................................... +.............|..........-.|..\..........-....\...........................|.....................\.\....-....... +..........\............../........-........|.....-./...........................................\..........\... +..........|.-...|.|...............-...|.|..................-...-........|/.../.............................\.. +.............-....\.....................................................\........................|...........\ +..\|...........|....-.-|........................................|.............................\.....\.-|...... +...........//.|.............-......\.........................|............-|...............././.....|....|.... +.............../.\...............--........|........-....\..../.........-.....................-......../...-.. +.....|......\.........../............/..................../.........|...........-.......-.|...........-....... +.................-........./..........|...................../..........................|.|...........-.......| +.....|...-........|...................../.../...................................\.............\...|........... +.............\.......|........................./....|........./........\.........\.......................\.... +............................/-..................................../........-..............\...|.....\..|\..... +...-..././....................-.......|................./..........-.....|.|..............\..........-....\... +.....\\......\...............................-...-........-.|..../........\.\.....-.....................|..... +..............\.........\.\.|..........-......//./.../....|.............-.../..|.........................\.... +...................../...........\.........................\.-..|..........|........./|...............|/...\|. +..|.-./.............-.........././.|......../.......\...........-.........../..........\.....-.........-...... +.....|......\....-.........-.............|.......|-........|-...-../-..|.......-................-............. +.................|.........|....\................................/.-.....|........./..../..........-.......... +-....\......./......\.\..................-..|./......-...\.\...-.........................\.............|...... +......................./..............\/..........-................../............\.......\..\.......\....../. +.|...............//.................................../.......\.......\.\..........-\...|............-........ +.../......................|.....-.........../................-.../............../.........\.\.../........../.. +..................................|\..................-../........-./..\......./....|../.......|-............. +.....\..........|................/........\..........-......../\../..../.\-.|...\....|\.............\|..--.... +................./................................././|..../..........|............../............../......... +..............|......|.......\....|...|......|..................................\.......-....\/...........|... +......./\..................................-......./......\....\..-..................-................\......| +...........-.........../..............\............|....../....../.......\........./......../..\.........\...- +.-..................\............................/\...................|...........|....\....\--/............/. diff --git a/src/day-16.lisp b/src/day-16.lisp @@ -0,0 +1,64 @@ +(defpackage #:aoc/day-16 + (:use #:cl #:aoc/utils) + (:export #:day-16)) +(in-package #:aoc/day-16) + +(defconstant +pipe-char+ (code-char 124)) + +(defun next-directions (cell direction) + (eswitch (cell) + (#\/ (switch (direction) + (:right '(:up)) + (:down '(:left)) + (:up '(:right)) + (:left '(:down)))) + (#\\ (switch (direction) + (:right '(:down)) + (:down '(:right)) + (:up '(:left)) + (:left '(:up)))) + (#\- (switch (direction) + (:down '(:right :left)) + (:up '(:right :left)) + (t (list direction)))) + (+pipe-char+ + (switch (direction) + (:left '(:up :down)) + (:right '(:up :down)) + (t (list direction)))) + (#\. (list direction)))) + +(defun move-position (pos direction) + (eswitch (direction) + (:right (point+ pos (cons 1 0))) + (:down (point+ pos (cons 0 1))) + (:up (point+ pos (cons 0 -1))) + (:left (point+ pos (cons -1 0))))) + +(defun shoot-beam (map) + (loop with energizing-map = (make-hash-table :test 'equal) + with passing-map = (make-hash-table :test 'equal) + with todo = (list (list (cons 0 0) :right)) + with width = (input-map-width map) + with height = (input-map-height map) + for (pos direction) = (pop todo) + while pos + for cell = (map-cell map pos) + for directions = (next-directions cell direction) + do (setf (gethash pos energizing-map) + (1+ (or (gethash pos energizing-map) 0)) + (gethash (list pos direction) passing-map) + t) + do (loop for dir in directions + for new-pos = (move-position pos dir) + unless (or (< (point-x new-pos) 0) + (< (point-y new-pos) 0) + (>= (point-x new-pos) width) + (>= (point-y new-pos) height) + (gethash (list new-pos dir) passing-map)) + do (push (list new-pos dir) todo)) + finally (return (hash-table-count energizing-map)))) + +(defun day-16 (input) + (let ((map (make-map input))) + (shoot-beam map))) diff --git a/t/day-16.lisp b/t/day-16.lisp @@ -0,0 +1,18 @@ +(defpackage #:aoc-test/day-16 + (:use #:cl #:lisp-unit2)) +(in-package #:aoc-test/day-16) + +(define-test test-day-16 + () + (multiple-value-bind (task-1) + (aoc:run-day 16 ".|...\\.... +|.-.\\..... +.....|-... +........|. +.......... +.........\\ +..../.\\\\.. +.-.-/..|.. +.|....-|.\\ +..//.|....") + (assert= 46 task-1)))