Skip to content

Commit

Permalink
Merge pull request #78 from ephemient/py/day10
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 10, 2024
2 parents af80721 + b5cb28c commit fc9a2f5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Development occurs in language-specific directories:
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt)|[day7.py](py/aoc2024/day7.py)|[day7.rs](rs/src/day7.rs)|
|[Day8.hs](hs/src/Day8.hs)|[Day8.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt)|[day8.py](py/aoc2024/day8.py)|[day8.rs](rs/src/day8.rs)|
|[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day9.kt)|[day9.py](py/aoc2024/day9.py)|[day9.rs](rs/src/day9.rs)|
|[Day10.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.kt)|||
|[Day10.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.kt)|[day10.py](py/aoc2024/day10.py)||
68 changes: 68 additions & 0 deletions py/aoc2024/day10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Day 10: Hoof It
"""

from operator import add, or_
from typing import Callable

SAMPLE_INPUT = """
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
"""


def _parse(data: str) -> list[set[tuple[int, int]]]:
elevations = [set() for _ in range(10)]
for y, line in enumerate(data.splitlines()):
for x, char in enumerate(line):
if "0" <= char <= "9":
elevations[int(char)].add((y, x))
return elevations


def _adj(point: tuple[int, int]) -> list[tuple[int, int]]:
y, x = point
return [(y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)]


def _bfs[T](
elevations: list[set[tuple[int, int]]],
start: Callable[[tuple[int, int]], T],
plus: Callable[[T, T], T],
):
acc = {key: start(key) for key in elevations[0]}
for points in elevations[1:]:
tmp = {}
for key, value in acc.items():
for point in _adj(key):
if point in points:
tmp[point] = plus(value, tmp[point]) if point in tmp else value
acc = tmp
return acc


def part1(data: str) -> int:
"""
>>> part1(SAMPLE_INPUT)
36
"""
return sum(
len(value) for value in _bfs(_parse(data), lambda point: {point}, or_).values()
)


def part2(data: str) -> int:
"""
>>> part2(SAMPLE_INPUT)
81
"""
return sum(_bfs(_parse(data), lambda _: 1, add).values())


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ day6 = "aoc2024.day6:parts"
day7 = "aoc2024.day7:parts"
day8 = "aoc2024.day8:parts"
day9 = "aoc2024.day9:parts"
day10 = "aoc2024.day10:parts"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit fc9a2f5

Please sign in to comment.