Solve 2024:10 p1-2 "Hoof It"
A bug in a draft version of the code accidentically gave the answer to part 2. That's a first! :D
This commit is contained in:
parent
1b6502df7a
commit
0a812572db
1 changed files with 41 additions and 0 deletions
41
2024-python/output/day_10.py
Normal file
41
2024-python/output/day_10.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
from collections import defaultdict, deque
|
||||||
|
|
||||||
|
from output import D, matrix
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
grid, H, W = matrix(data)
|
||||||
|
starts = [
|
||||||
|
(r, c, int(grid[r][c]), (r, c))
|
||||||
|
for r, y in enumerate(grid)
|
||||||
|
for c, x in enumerate(y)
|
||||||
|
if x == "0"
|
||||||
|
]
|
||||||
|
queue = deque(starts)
|
||||||
|
scores = defaultdict(set)
|
||||||
|
ratings = defaultdict(int)
|
||||||
|
while queue:
|
||||||
|
r, c, value, start = queue.popleft()
|
||||||
|
if value == 9:
|
||||||
|
scores[start].add((r, c))
|
||||||
|
ratings[start] += 1
|
||||||
|
continue
|
||||||
|
for dy, dx in D:
|
||||||
|
if not (0 <= (qr := r + dy) < H and 0 <= (qc := c + dx) < W):
|
||||||
|
continue
|
||||||
|
if int(grid[qr][qc]) - value != 1:
|
||||||
|
continue
|
||||||
|
queue.append((qr, qc, int(grid[qr][qc]), start))
|
||||||
|
scores_sum = sum(len(t) for t in scores.values())
|
||||||
|
ratings_sum = sum(ratings.values())
|
||||||
|
return scores_sum, ratings_sum
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/10.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
Loading…
Add table
Reference in a new issue