From 19aee1363a29519078e6fd641c8dc126798b0256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Thu, 12 Dec 2024 22:53:34 +0100 Subject: [PATCH] Solve 2024:12 p1 "Garden Groups" Loooong time overdue. First drafts of the code kept track of every id separately in a defaultdict. This was not sustainable since the code examples had some recurring ids, and after managing to support the test cases the code was eay off for the real puzzle input. In the end, the whole code was deleted and replaced by a BFS solution that expands the current regions until it finds no more areas. Got the trick to count all perimiters in an else clause from a leaderboard placed youtuber. --- 2024-python/output/day_12.py | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 2024-python/output/day_12.py diff --git a/2024-python/output/day_12.py b/2024-python/output/day_12.py new file mode 100644 index 0000000..712e3e1 --- /dev/null +++ b/2024-python/output/day_12.py @@ -0,0 +1,86 @@ +import re +from collections import Counter, defaultdict, deque +from heapq import heappop, heappush +from itertools import chain, combinations, compress, permutations + +from output import ADJ, DD, D, ccw, cw, ints, matrix, mdbg, mhd, vdbg + + +def solve(data): + grid, H, W = matrix(data) + p1 = 0 + seen = set() + for r, row in enumerate(grid): + for c, col in enumerate(row): + if (r, c) in seen: + continue + areas = 0 + perimeters = 0 + q = deque([(r, c)]) + while q: + rc = q.popleft() + if rc in seen: + continue + seen.add(rc) + areas += 1 + y, x = rc + for dy, dx in D: + if (0 <= y + dy < H and 0 <= x + dx