diff options
author | mhsn <mail@mhsn.net> | 2025-09-12 20:20:58 +0100 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2025-09-12 20:36:09 +0100 |
commit | 6b733982f9f240c1c97f1fa705bfbe4cd93c640e (patch) | |
tree | dad623203c8bb40f372e0ea14479c2f564efda79 /2024/10/python.py | |
parent | 9299c4ef655b88eb8b571864c939c78b27f77723 (diff) | |
download | aoc-6b733982f9f240c1c97f1fa705bfbe4cd93c640e.tar.gz aoc-6b733982f9f240c1c97f1fa705bfbe4cd93c640e.zip |
simplify python file structure
Diffstat (limited to '2024/10/python.py')
-rwxr-xr-x | 2024/10/python.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/2024/10/python.py b/2024/10/python.py new file mode 100755 index 0000000..88892b6 --- /dev/null +++ b/2024/10/python.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +from fileinput import input + +grid = [[int(c) for c in line.strip()] for line in input()] + +starts = { + (idx, idy) for idy, line in enumerate(grid) for idx, n in enumerate(line) if n == 0 +} + +# print(*grid, sep="\n") +# print(starts) + + +def score_trailhead(start, gold): + score = 0 + q = [start] + seen = set() + while q: + x, y = q.pop() + seen.add((x, y)) + + h = grid[y][x] + if h == 9: + score += 1 + continue + for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]: + nx, ny = x + dx, y + dy + if ((nx, ny) in seen and not gold) or not ( + 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) + ): + continue + + if grid[ny][nx] == h + 1: + q.append((nx, ny)) + return score + + +silver = sum(score_trailhead(start, False) for start in starts) +gold = sum(score_trailhead(start, True) for start in starts) + +print("silver:", silver) +print("gold:", gold) |