diff --git a/day16.py b/day16.py new file mode 100644 index 0000000..41a0c3f --- /dev/null +++ b/day16.py @@ -0,0 +1,72 @@ +from collections import deque +from tools.aoc import AOCDay +from tools.coordinate import Coordinate +from tools.grid import Grid +from typing import Any + + +def walk(grid: Grid, start: tuple[Coordinate, tuple[int, int]]) -> int: + visited = set() + q = deque() + q.append(start) + while q: + cur = q.popleft() + if cur in visited: + continue + pos, direction = cur + if not grid.isWithinBoundaries(pos): + continue + visited.add(cur) + + tile = grid.get(pos) + if not tile or (tile == '-' and dir in [(0, 1), (0, -1)]) or (tile == '|' and dir in [(1, 0), (-1, 0)]): + q.append((pos + direction, direction)) + elif tile == '|': + q.append((pos + (0, 1), (0, 1))) + q.append((pos + (0, -1), (0, -1))) + elif tile == '-': + q.append((pos + (1, 0), (1, 0))) + q.append((pos + (-1, 0), (-1, 0))) + elif tile == '\\': + q.append((pos + (direction[1], direction[0]), (direction[1], direction[0]))) + elif tile == '/': + q.append((pos + (-direction[1], -direction[0]), (-direction[1], -direction[0]))) + else: + assert False, tile + + return len(set(x[0] for x in visited)) + + +class Day(AOCDay): + inputs = [ + [ + (46, "input16_test"), + (7074, "input16"), + ], + [ + (51, "input16_test"), + (7530, "input16"), + ] + ] + + def part1(self) -> Any: + grid = Grid.from_data(self.getInput(), translate={'.': False}) + return walk(grid, start=(Coordinate(0, 0), (1, 0))) + + def part2(self) -> Any: + grid = Grid.from_data(self.getInput(), translate={'.': False}) + max_tiles = 0 + for x in grid.rangeX(): + max_tiles = max(max_tiles, walk(grid, start=(Coordinate(x, 0), (0, 1)))) + max_tiles = max(max_tiles, walk(grid, start=(Coordinate(x, grid.maxY), (0, -1)))) + + for y in grid.rangeY(): + max_tiles = max(max_tiles, walk(grid, start=(Coordinate(0, y), (1, 0)))) + max_tiles = max(max_tiles, walk(grid, start=(Coordinate(grid.maxX, -y), (-1, 0)))) + + return max_tiles + + +if __name__ == '__main__': + day = Day(2023, 16) + day.run(verbose=True) diff --git a/inputs/input16 b/inputs/input16 new file mode 100644 index 0000000..b8e87c8 --- /dev/null +++ b/inputs/input16 @@ -0,0 +1,110 @@ +\........../.......-........|....\.......-..\/....|................\........../.|..././........\.........../.. +...............\..\................/............\................./................../.........\..|....|/|-... +.../..........................\.....|.............-.......././.......-.|..../|.....-\........-....../......... +|..........................-..........-......./............................-..|................-.............. +.....|......../...../.......-.\..............................-........|............|/.../../........./........ +..\...|..-......\.........-\.||........|./................\....\........-..\..................../..\....-...|. +.|...........|............-........./.-.................|.|....../...............|..../..-..-...\...........|. +..............\............................................................./......................./\....-... +./........|...\.\..|..-....\............................|........./.................|/...............-........ +\....\./.............../................../....|......./........../.\......./........-....\......../....../|.. +./......--..........\..|.--........-...../......./........\.....|/..................|//...........|.........-. +.......|.....\-.\..-......-.........|.....................-/\.....-..........|.............-....\.....\.-..... +..--...\....-..|..................................-......-......\......-................\...../..-/....--..... +....-.........................\.\............|...............|.....-...\....|-................................ +.................-..|.\..\....-..........|...../.....-...-.-../.............../................-.............. +-..-......|..............................\.....\........|...../........\.................../.................. +..............-.....-.|..........|.\..................|........|...........\.\...|........-................... +..\............/...\\........../...-..\.../.........\..|................../......\..-.../....................\ +..................|...\......................./...|......../..............-./...\........\......./|...\..-..-. +.................................................-................|.........................................\. +......-...............................|./............./...........................-\/..../......\..........\.. +......\....|..........................\.-|..........................|....................\.....\/...-......... +..\..-|...|.....-...../......./....|...............\................|..|.......\...........................-.. +..\......-.......\.........\................|...........|....../...../..........-./...-|..................../. +................./.......\...|.....\....-................./......../..\...........-...|.../|................\. +..|..\./.........\.\.......................-......|....-..../.../................-.......\../.../...../....... +........../........./..|..-....................\\............\........................-......-......|......... +.../-....\..\..........\....\\......................................./........\...-.................\......... +................................../.................|..\.-..|................|/....-.........................| +...-.................||..../....................-..........................................|.-................ +..//.\..........................|.....|-.............\.\......-....\.\.......\............-..............\-... +.......|.|..-........./..|........-.....|........................-...................-......\................- +..-..|..........|....-.............|.|/...../../..................\...\................../......|....|........ +........\..././.............../.............../....-../.....|....\................|/.../.....|.|-..|.......... +......|..........\...-./........-...............................\..........|.............|..\..|......./.....| +.............................-................................................|................/......\.../..- +......-........\..........-..../...............\............/.....\......|........................-.|.../..... +...\...............-....-........-............-...../....|.\.../.......\..-....................|........|..... +.............................-...\....|...|.......\............................-............\................. +....................|...-........-.........-..\................/....................../.|.......-............. +......\.........................../....-....|...........................................\...-.........|......\ +.............\../\..............|\.\..........|\.../..................../\...................|\.......\....... +/.................../.\\.........................../...........-...-..........-..-............................ +............\......../........-.........../........./.................-................./...........|......... +.........\.......|.......\........./.....................\..............\\....../...|-....\..|..-.....|....... +.|....-.............\.........\.\../.......-...........................\./..........................--\......\ +........./..../................./.................\............................................./../.\........ +.....-..............-.-...\/..|....-........./.....................\................./.\..........-...|....|.. +........|...|.-.......|.......-............\.../....|...\............../............-......................... +........................|.....\........................./\/.........................|......................... +....\..././..........|.....\......................-................./.....|...............................|-.. +.../.........../.-......\..../.................-...\.....-......\.....\..............//.|..............-..-... +....../............./-/.\...........\...\..|/........................-...../......\........-./\..........||... +.................................../............|....................../..|.....\......\............/......... +.....|....\...|.-./............................./..-....|.\.......................-../.-..-.........\......... +..........\...|../.......-|............-........../...../..........-......-../.-......|..../.-.../............ +.-.........-............|/.......-........|...................-.........-........../.-........\..........|/... +.........\.........\....../....|...\\......-..........-................\................................-..... +............-...../......|...//........\-....|......../........./..................-................../....--. +...............|.................................|.../...\....................\................|...\.|........ +..............\.........\.........\....|.-.......\.\........-.........../...|.....|..............\.......\.... +........-.-.....|.../......-\..|.....-.........|.-................\......./....\...-.....\..|/...............| +....|.....................-...................\......\......-.......................-...../........./....\.... +..|...|.......\./..-..............\.............................................\../....|..................... +..................-../........|.....-\.../................................................./..\...|......-...- +.....\......\........\.....................|...|../......../....../.\........./............................... +............../........../...........\..\/..|.\...|..........-................................\-.............. +/...............\...|......\............/|...................|....././...\............................-.|..... +...........|................/-......\.....-.........-.............../......./............../|................. +..-...........|..\.............\....|/...............|..../..............\...............|./......|........./. +.....\\..|......\....\.......-.................\.-......................|........./...|.........|...|...-..... +-...../....|....../............./.......|/../\.............../...../-\.../-.......|........../..............\\ +..........\....................../.................|...--../.................................................. +.\.................../......|......-.............................//......|.../|.|....\.............\.......... +.\-...........-.........|..................................../........|......../...-............\....-...-.... +......\......|................-...../....|........-..\.....-........//................../.....|.........-..... +.....-..........-.|/.........-....................../......|.|./........................................./\\|. +...\................|.........../..-....|..-..........|........../....-....../......./.....-..\.....\\..../... +.....\.....-..................-...-..../....|....|.................|...\./.............../.......|.\/...../... +.||......../........../.-...|...........\.................\........-........|........................-...|.... +........./......\......|...............\.\.................................................\.................| +..........................|....................|..../-...............|....|......../....-..................|.. +.-........\.|......../...|...................|.........|............\.....|.......\........./................. +................-........................\...........|................../................-.............\...... +.......\..........|.............../...|.-......................../....\.............-.......\./............... +..........\...........................................-........|.......\........./...-........\............... +../......./...\|...............-........\................./...\...................../.....-.-....-...........- +.\../......../....-\................................|.....-..|...............................-................ +..........................\.....|................/.....\......../.............|..............\......./-..|.... +|.........-.|............./......-.................|.......|...\/...........|...........................\\.... +-..................|.........................-.............-.....|..../.........\...-/.....|............/\.... +.........\.\....\...........|....-.../..............|.....|-.......\.............-......./......././.......\.. +..../.....-...........................................\.../..\.|.......................-..//....-......|...... +./-..................\..\....................................\......./.\....|................................. +...../....................................-....-/........................-/.........-.\...|......./..-.\...... +.....|.............-.......|-..-.................................|-...........................\.|.....|....... +.........\-.............\.............../....-....|....-.|..........\............|.\...|.....-................ +............/.......-....|./....|../..../......./..\.................-..................|..................... +../....-........./.....\...............\......-....|...\........../.....|........................\............ +...../.\.............\.........................-..|.......|......-......|................./......./-.......... +...\..........\.......................\.-..././.........|..\..................\...../...|.......|............- +........./............-..................................|.....................-.............................. +........................|.....\.........\..............-..\..........||...../.\\./....../..................... +.................-.............\.../...........\...\......--................\.....\.............\....-........ +.../-..........\.\............-.......-.............\..\.\...../........|...-..-..|..........\................ +\.|..\......\-......./.........||..\.........................................................-...../.......... +....|..........................................\......-..........\.../...\-........|.......................... +.......-.......\..//./....|...............\.\..-...\....|....................-......\......\..-......../...... +/..../../....-../....-....-......................\........./..........-/..\......./..\-.../..........|........ +.|....\\........./....\.............-......--\...........|.........|..\...................../........./.\..... diff --git a/inputs/input16_test b/inputs/input16_test new file mode 100644 index 0000000..c78b2e7 --- /dev/null +++ b/inputs/input16_test @@ -0,0 +1,10 @@ +.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|.... \ No newline at end of file