From 0a812572dbf90dead88511ce1a56d841b3fc1b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Tue, 10 Dec 2024 09:30:15 +0100 Subject: [PATCH] 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 --- 2024-python/output/day_10.py | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2024-python/output/day_10.py diff --git a/2024-python/output/day_10.py b/2024-python/output/day_10.py new file mode 100644 index 0000000..4cf9af9 --- /dev/null +++ b/2024-python/output/day_10.py @@ -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)