Implement 2024 day 10

This commit is contained in:
2024-12-10 09:38:31 +01:00
parent d49fc8e769
commit 3e709cc863
3 changed files with 100 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
from collections import deque
import numpy
from . import SeparateRunner
class DayRunner(SeparateRunner):
@classmethod
def part1(cls, input: str) -> int:
grid = numpy.array(
[[int(v) for v in line] for line in input.strip().split("\n")]
)
width, height = grid.shape
start_x, start_y = numpy.nonzero(grid == 0)
todo = []
reachable = 0
for sx, sy in zip(start_x, start_y):
todo.append((sx, sy))
ways = numpy.zeros_like(grid, dtype=bool)
def enqueue(x: int, y: int, val: int) -> None:
if grid[x, y] == val + 1:
if not ways[x, y]:
todo.append((x, y))
ways[x, y] += True
while todo:
x, y = todo.pop()
val = grid[x, y]
if val == 9:
reachable += 1
continue
if x > 0:
enqueue(x - 1, y, val)
if y > 0:
enqueue(x, y - 1, val)
if x < width - 1:
enqueue(x + 1, y, val)
if y < height - 1:
enqueue(x, y + 1, val)
return reachable
@classmethod
def part2(cls, input: str) -> int:
grid = numpy.array(
[[int(v) for v in line] for line in input.strip().split("\n")]
)
ways = numpy.zeros_like(grid)
width, height = grid.shape
start_x, start_y = numpy.nonzero(grid == 9)
ways[grid == 9] = 1
todo = deque((x, y) for x, y in zip(start_x, start_y))
def enqueue(x: int, y: int, val: int, cur: int) -> None:
if grid[x, y] == val - 1:
if ways[x, y] == 0:
todo.append((x, y))
ways[x, y] += cur
while todo:
x, y = todo.popleft()
val = grid[x, y]
cur = ways[x, y]
if x > 0:
enqueue(x - 1, y, val, cur)
if y > 0:
enqueue(x, y - 1, val, cur)
if x < width - 1:
enqueue(x + 1, y, val, cur)
if y < height - 1:
enqueue(x, y + 1, val, cur)
return ways[grid == 0].sum()

View File

@@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732

7
2024/tests/test_day10.py Normal file
View File

@@ -0,0 +1,7 @@
from aoc.days.day10 import DayRunner
from . import get_data
def test_sample_part1() -> None:
assert DayRunner.part1(get_data(10)) == 36