day 13, solve linear equations
This commit is contained in:
parent
ac5746a2c9
commit
558d1a4883
|
@ -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)
|
|
@ -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)
|
|
@ -0,0 +1,25 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
def nums(s:str) -> list[int]:
|
||||||
|
return list(map(int,re.findall(r'-?\d+',s)))
|
||||||
|
|
||||||
|
|
||||||
|
TESTS = [
|
||||||
|
("", []),
|
||||||
|
("9738: 7 89 52 75 8 1", [9738, 7, 89, 52, 75, 8, 1]),
|
||||||
|
("p=62,20 v=85,-14", [62, 20, 85, -14]),
|
||||||
|
("24 25 28 31 28", [24, 25, 28, 31, 28]),
|
||||||
|
("how():mul(422,702)'how()'", [422,702]),
|
||||||
|
("58692 56129", [58692, 56129]),
|
||||||
|
("Button A: X+16, Y+32", [16, 32]),
|
||||||
|
("46,51,67,25,72,77,13,96,36,76,52,23", [46,51,67,25,72,77,13,96,36,76,52,23]),
|
||||||
|
("47|53", [47,53]),
|
||||||
|
("..........M..........j.............y.....O........", []),
|
||||||
|
]
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
for (q,a) in TESTS:
|
||||||
|
assert nums(q) == a
|
||||||
|
a, b = nums("how():mul(422,702)'how()'")
|
||||||
|
assert a == 422
|
||||||
|
assert b == 702
|
Loading…
Reference in New Issue