From abaec4231fb3005879b9319d7feda0ffdce6a1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Thu, 12 Dec 2024 00:38:39 +0100 Subject: [PATCH] Solve 2016:24 p1-2 "Air Duct Spelunking" BFS baby. --- 2016-python2/output/day_24.py | 77 +++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 2016-python2/output/day_24.py diff --git a/2016-python2/output/day_24.py b/2016-python2/output/day_24.py new file mode 100644 index 0000000..d2c7645 --- /dev/null +++ b/2016-python2/output/day_24.py @@ -0,0 +1,77 @@ +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, ints, matrix, mdbg, mhd, vdbg + + +def solve(data): + grid, H, W = matrix(data) + dests = { + v: (y, x) for y, r in enumerate(grid) for x, v in enumerate(r) if v.isdigit() + } + S0 = dests["0"] + del dests["0"] + p1 = travel(dests, grid, H, W, S0) + p2 = travel(dests, grid, H, W, S0, goback=True) + return p1, p2 + + +def travel(dests, grid, H, W, S0, goback=False): + shortest = float("inf") + for goals in permutations(dests.items()): + goals = list(goals) + if goback: + goals += [("0", S0)] + t = 0 + S = S0 + for _, E in goals: + seen = set() + q = [(S, 0)] + while q: + pos, w = q.pop(0) + if pos == E: + t += w + break + if pos in seen: + continue + seen.add(pos) + y, x = pos + for dy, dx in D: + if not (0 <= dy + y < H and 0 <= dx + x < W): + continue + if grid[dy + y][dx + x] != "#": + q.append(((dy + y, dx + x), w + 1)) + S = E + shortest = min(shortest, t) + return shortest + + +if __name__ == "__main__": + # use dummy data + inp = """ + ########### + #0.1.....2# + #.#######.# + #4.......3# + ########### + """.strip() + + # uncomment to instead use stdin + # import sys; inp = sys.stdin.read().strip() + + # uncomment to use AoC provided puzzle input + with open("./input/24.txt", "r") as f: + inp = f.read().strip() + + # uncomment to do initial data processing shared by part 1-2 + p1, p2 = solve(inp) + + print(p1) + print(p2) + + # uncomment and replace 0 with actual output to refactor code + # and ensure nonbreaking changes + # assert p1 == 0 + # assert p2 == 0