aoc2024/d13/part2.py

32 lines
773 B
Python
Raw Permalink Normal View History

2024-12-16 13:32:04 +00:00
import sys
from aoc import nums
import sympy as sym
from sympy.core.numbers import Integer
x,y = sym.symbols('x,y')
def solve(a,b,p, part2=False):
(xa, ya) = a
(xb, yb) = b
(xp, yp) = p
T = 10_000_000_000_000 if part2 else 0
eq1 = sym.Eq(x*xa+y*xb, xp+T)
eq2 = sym.Eq(x*ya+y*yb, yp+T)
result = sym.solve([eq1,eq2],(x,y))
if type(result[x]) is not Integer or type(result[y]) is not Integer:
return 0
elif not part2 and (result[x]>100 or result[y]>100):
return 0
else:
return result[x]*3 + result[y]
S1 = S2 = 0
for i in sys.stdin.read().strip().split('\n\n'):
[a,b,p] = i.split("\n")
a = nums(a)
b = nums(b)
p = nums(p)
S1 += solve(a,b,p)
S2 += solve(a,b,p,part2=True)
print(S1, S2)