2025 #5

Open
aey wants to merge 16 commits from 2025 into main
Showing only changes of commit a585a687c9 - Show all commits

View file

@ -1,4 +1,6 @@
import re import re
import functools
from math import gcd, inf
from pprint import pprint from pprint import pprint
from collections import deque, Counter, defaultdict from collections import deque, Counter, defaultdict
from heapq import heappop, heappush from heapq import heappop, heappush
@ -10,21 +12,49 @@ from output import matrix, D, DD, ADJ, ints, sints, mhd, mdbg, vdbg, cw, ccw, bk
def solve(data): def solve(data):
r = r"^\[(.+)\] (.+) \{(.+)\}$" r = r"^\[(.+)\] (.+) \{(.+)\}$"
p1 = 0 p1 = 0
p2 = 0
for line in data.splitlines(): for line in data.splitlines():
il, b, _j = re.findall(r, line)[0] il, b, j = re.findall(r, line)[0]
B = [set(ints(s)) for s in b.split()] B = [set(ints(s)) for s in b.split()]
E = set([i for i, s in enumerate(il) if s == "#"]) E = set([i for i, s in enumerate(il) if s == "#"])
Q = [(b, set(), 0) for b in B] j = tuple(ints(j))
while Q:
b, lit, p = Q.pop(0) p = 0
if lit == E: L = set()
p1 += p while L != E:
break p += 1
for nb in B: for bp in combinations(B, p):
if nb == b: L = set()
for b in bp:
L = L ^ b
if L == E:
break
if L == E:
break
p1 += p
@functools.cache
def _press(T):
if not any(T):
return 0
L = set(i for i, s in enumerate(T) if s % 2 == 1)
R = inf
for p in [bb for ii, bb in enumerate(B) if ii in L]:
tt = list(T)
for b in p:
for i in b:
tt[i] -= 1
if any(j < 0 for j in tt):
continue continue
Q.append((nb, lit ^ b, p + 1)) Th = tuple(j // 2 for j in tt)
p2 = None n = _press(Th)
if n is None:
continue
R = min(R, len(p) + 2 * n)
return R
p2 = _press(j)
return p1, p2 return p1, p2
@ -42,17 +72,17 @@ if __name__ == "__main__":
# uncomment to use AoC provided puzzle input # uncomment to use AoC provided puzzle input
with open("./input/10.txt", "r") as f: with open("./input/10.txt", "r") as f:
inp = f.read().strip() in_p = f.read().strip()
# uncomment to do initial data processing shared by part 1-2 # uncomment to do initial data processing shared by part 1-2
p1, p2 = solve(inp) p1, p2 = solve(inp)
print(p1) print(p1)
os.system(f"echo {p1} | wl-copy") os.system(f"echo {p1} | wl-copy")
# print(p2) print(p2)
# os.system(f"echo {p2} | wl-copy") os.system(f"echo {p2} | wl-copy")
# uncomment and replace 0 with actual output to refactor code # uncomment and replace 0 with actual output to refactor code
# and ensure nonbreaking changes # and ensure nonbreaking changes
# assert p1 == 0 # assert p1 == 466
# assert p2 == 0 # assert p2 == 17214