Solve 2025 day 11 pt 1-2
For p1 a BFS algorithm is fast enough. For pt 2, for the third day in a row, it did not work. I caved in and used recursion and memoisation for DFS instead, instantly fast as lightning. I finished by making the pt 2 code compatible with pt 1.
This commit is contained in:
parent
0723a8223f
commit
044df24990
1 changed files with 33 additions and 0 deletions
33
2025-python/output/day_11.py
Normal file
33
2025-python/output/day_11.py
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
import functools
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
G = defaultdict(set)
|
||||||
|
for line in data.splitlines():
|
||||||
|
f, *t = line.split()
|
||||||
|
G[f[:-1]] = set(t)
|
||||||
|
|
||||||
|
@functools.cache
|
||||||
|
def _traverse(k, fasttrack=True, fft=False, dac=False):
|
||||||
|
if k == "out":
|
||||||
|
return fasttrack or (fft and dac)
|
||||||
|
return sum(
|
||||||
|
_traverse(nk, fasttrack, fft or k == "fft", dac or k == "dac")
|
||||||
|
for nk in G[k]
|
||||||
|
)
|
||||||
|
|
||||||
|
return _traverse("you"), _traverse("svr", fasttrack=False)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/11.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
|
|
||||||
|
assert p1 == 649
|
||||||
|
assert p2 == 458948453421420
|
||||||
Loading…
Add table
Reference in a new issue