day 13, solve linear equations
This commit is contained in:
19
d13/part1.py
Normal file
19
d13/part1.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import sys
|
||||
from aoc import nums
|
||||
|
||||
MOVES = sorted([(i,j)
|
||||
for i in range(1,101)
|
||||
for j in range(1,101)
|
||||
], key=lambda x:3*x[0]+x[1])
|
||||
|
||||
S = 0
|
||||
for i in sys.stdin.read().strip().split('\n\n'):
|
||||
[a,b,p] = i.split("\n")
|
||||
(xa, ya) = nums(a)
|
||||
(xb, yb) = nums(b)
|
||||
(xp, yp) = nums(p)
|
||||
for (i,j) in MOVES:
|
||||
if xa*i+xb*j==xp and ya*i+yb*j==yp:
|
||||
S += 3*i+j
|
||||
break
|
||||
print(S)
|
31
d13/part2.py
Normal file
31
d13/part2.py
Normal file
@@ -0,0 +1,31 @@
|
||||
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)
|
Reference in New Issue
Block a user