Skip to content

Commit 03337b6

Browse files
committed
Add solution to 2025-12-10
1 parent c7cb303 commit 03337b6

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

2025/day10/solutions.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import numpy as np
2+
from scipy.optimize import linprog
3+
from collections import deque
4+
5+
6+
with open("input") as f:
7+
ls = f.read().strip().split("\n")
8+
9+
10+
def solve1(l):
11+
goal, *buttons, _ = l.split(" ")
12+
goal = set(i for i, x in enumerate(goal[1:-1]) if x == "#")
13+
14+
moves = []
15+
for b in buttons:
16+
b = b[1:-1]
17+
b = {int(x) for x in b.split(",")}
18+
moves.append(b)
19+
20+
# BFS
21+
q = deque()
22+
q.append((set(), 0))
23+
seen = set()
24+
while q:
25+
curr, steps = q.popleft()
26+
if curr == goal:
27+
return steps
28+
for m in moves:
29+
newset = curr ^ m
30+
if newset in seen:
31+
continue
32+
seen.add(frozenset(newset))
33+
q.append((newset, steps + 1))
34+
35+
36+
print(sum(solve1(l) for l in ls))
37+
38+
39+
# Part 2
40+
def solve2(l):
41+
_, *buttons, counters = l.split(" ")
42+
goal = tuple(map(int, counters[1:-1].split(",")))
43+
moves = []
44+
for b in buttons:
45+
b = b[1:-1]
46+
b = {int(x) for x in b.split(",")}
47+
moves.append(b)
48+
49+
c = [1 for _ in moves]
50+
A_eq = []
51+
b_eq = []
52+
for i in range(len(goal)):
53+
A_eq.append([1 if i in move else 0 for move in moves])
54+
b_eq.append(goal[i])
55+
A_eq = np.array(A_eq)
56+
b_eq = np.array(b_eq)
57+
res = linprog(
58+
c, A_eq=A_eq, b_eq=b_eq, bounds=(0, None), method="highs", integrality=True
59+
)
60+
# print(res)
61+
return res.fun
62+
63+
64+
print(sum(solve2(l) for l in ls))

0 commit comments

Comments
 (0)