Skip to content

Commit 6e3275e

Browse files
committed
Add solution to 2024-12-13
1 parent 10f5399 commit 6e3275e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

2024/day13/solutions.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import re
2+
import z3
3+
4+
with open("input") as f:
5+
groups = [
6+
list(map(int, re.findall("\\d+", g))) for g in f.read().strip().split("\n\n")
7+
]
8+
9+
10+
def price(ax, ay, bx, by, tx, ty, offset=0):
11+
tx += offset
12+
ty += offset
13+
a = z3.Int("a")
14+
b = z3.Int("b")
15+
s = z3.Solver()
16+
s.add(a * ax + b * bx == tx)
17+
s.add(a * ay + b * by == ty)
18+
s.add(a >= 0)
19+
s.add(b >= 0)
20+
if s.check() == z3.sat:
21+
m = s.model()
22+
return 3 * m[a].as_long() + m[b].as_long()
23+
return 0
24+
25+
26+
# Part 1
27+
print(sum(price(*g) for g in groups))
28+
29+
# Part 2
30+
print(sum(price(*g, 10000000000000) for g in groups))

0 commit comments

Comments
 (0)