|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import time
|
| 4 | +from functools import cache |
| 5 | +from itertools import product |
4 | 6 | from pathlib import Path
|
5 | 7 |
|
6 | 8 | DIR = Path(__file__).parent
|
7 | 9 | FILE = DIR.parents[3] / "inputs" / f"{DIR.stem}.txt"
|
8 | 10 |
|
| 11 | +TRAILHEAD = "0" |
| 12 | +END_OF_TRAIL = "9" |
9 | 13 |
|
10 |
| -def part1(contents: str): |
11 |
| - return "Not done yet!" |
12 | 14 |
|
| 15 | +def num_trails(contents: list[str], x: int, y: int, unique: bool = False) -> int: |
| 16 | + @cache |
| 17 | + def _find_all_trail_ends(x: int, y: int) -> set[tuple[int, int]]: |
| 18 | + nonlocal contents |
| 19 | + curr = contents[x][y] |
| 20 | + if curr == END_OF_TRAIL: |
| 21 | + return {(x, y)} |
13 | 22 |
|
14 |
| -def part2(contents: str): |
15 |
| - return "Not done yet!" |
| 23 | + target = str(int(curr) + 1) |
| 24 | + trailheads = set() |
| 25 | + for dx, dy in [ |
| 26 | + [-1, 0], |
| 27 | + [0, 1], |
| 28 | + [1, 0], |
| 29 | + [0, -1], |
| 30 | + ]: |
| 31 | + newx, newy = x + dx, y + dy |
| 32 | + if not 0 <= newx < len(contents) or not 0 <= newy < len(contents[0]): |
| 33 | + # new point is outside bounds of the contents, ignore |
| 34 | + continue |
| 35 | + if contents[newx][newy] == target: |
| 36 | + trailheads |= _find_all_trail_ends(x=newx, y=newy) |
| 37 | + return trailheads |
| 38 | + |
| 39 | + @cache |
| 40 | + def _find_num_unique_trails(x: int, y: int) -> int: |
| 41 | + # Oddly enough, this was the way I initially tried to solve Part 1 |
| 42 | + # due to my misunderstanding things. I had to then go back and return a set |
| 43 | + # of those trail end coordinates, so as to have a unique set of trail ends, |
| 44 | + # which I could then count with len. |
| 45 | + # After finding that Part 2 was, indeed, this version, |
| 46 | + # I just re-built what I did at first and re-implemented with a flag. |
| 47 | + nonlocal contents |
| 48 | + curr = contents[x][y] |
| 49 | + if curr == END_OF_TRAIL: |
| 50 | + return 1 |
| 51 | + |
| 52 | + target = str(int(curr) + 1) |
| 53 | + trails = 0 |
| 54 | + for dx, dy in [ |
| 55 | + [-1, 0], |
| 56 | + [0, 1], |
| 57 | + [1, 0], |
| 58 | + [0, -1], |
| 59 | + ]: |
| 60 | + newx, newy = x + dx, y + dy |
| 61 | + if not 0 <= newx < len(contents) or not 0 <= newy < len(contents[0]): |
| 62 | + # new point is outside bounds of the contents, ignore |
| 63 | + continue |
| 64 | + if contents[newx][newy] == target: |
| 65 | + trails += _find_num_unique_trails(x=newx, y=newy) |
| 66 | + return trails |
| 67 | + |
| 68 | + if unique: |
| 69 | + # Part 2 |
| 70 | + return _find_num_unique_trails(x=x, y=y) |
| 71 | + |
| 72 | + # Part 1 |
| 73 | + trailhead_set = _find_all_trail_ends(x=x, y=y) |
| 74 | + return len(trailhead_set) |
| 75 | + |
| 76 | + |
| 77 | +def part1(contents: list[str]) -> int: |
| 78 | + total = 0 |
| 79 | + for x, line in enumerate(contents): |
| 80 | + for y, char in enumerate(line): |
| 81 | + if char == TRAILHEAD: |
| 82 | + num = num_trails(contents=contents, x=x, y=y) |
| 83 | + total += num |
| 84 | + return total |
| 85 | + |
| 86 | + |
| 87 | +def part2(contents: list[str]) -> int: |
| 88 | + total = 0 |
| 89 | + for x, line in enumerate(contents): |
| 90 | + for y, char in enumerate(line): |
| 91 | + if char == TRAILHEAD: |
| 92 | + num = num_trails(contents=contents, x=x, y=y, unique=True) |
| 93 | + total += num |
| 94 | + return total |
16 | 95 |
|
17 | 96 |
|
18 | 97 | def main():
|
19 |
| - contents = FILE.read_text() |
| 98 | + contents = FILE.read_text().strip().split("\n") |
20 | 99 |
|
21 | 100 | _start1 = time.perf_counter()
|
22 | 101 | result1 = part1(contents)
|
|
0 commit comments