summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormhsn <mail@mhsn.net>2025-12-23 19:15:05 +0000
committermhsn <mail@mhsn.net>2025-12-23 19:46:25 +0000
commit2c4eec0e929ba6533b7107b795067297a47db785 (patch)
treecf9b83ab2c8995be9a3205920009642cde496dcd
parenta9ab1f136aad824fb57f374f381e6b1986cdc42a (diff)
downloadaoc-2c4eec0e929ba6533b7107b795067297a47db785.tar.gz
aoc-2c4eec0e929ba6533b7107b795067297a47db785.zip
25-07 python p2
-rwxr-xr-x2025/07/python.py30
1 files changed, 17 insertions, 13 deletions
diff --git a/2025/07/python.py b/2025/07/python.py
index 48a8f2a..639c368 100755
--- a/2025/07/python.py
+++ b/2025/07/python.py
@@ -1,27 +1,31 @@
#!/usr/bin/env python3
from fileinput import input
-from functools import partial, reduce
+from functools import cache
lines = [line.strip() for line in input()]
+start = lines[0].index("S")
-bs = {lines[0].index("S")}
silver = 0
+bs = {start}
+for line in lines:
+ for b in bs.copy():
+ if line[b] == "^":
+ silver += 1
+ bs -= {b}
+ bs |= {b - 1, b + 1}
-def split(t: int, c: int) -> set[int]:
- global silver
- if lines[t][c] == "^":
- silver += 1
- return {c - 1, c + 1}
- return {c}
+@cache
+def tls(r: int, c: int) -> int:
+ if r == len(lines):
+ return 1
+ if lines[r][c] == "^":
+ return tls(r, c - 1) + tls(r, c + 1)
+ return tls(r + 1, c)
-for t, _ in enumerate(lines):
- bs = reduce(set.union, map(partial(split, t), bs))
-
-
-gold = 0
+gold = tls(0, start)
print("silver:", silver)
print("gold:", gold)