day 13 actually numpy is fine
parent
1c7035d433
commit
7cc13becde
59
day13/sol.py
59
day13/sol.py
|
@ -32,64 +32,31 @@ def solve(file):
|
|||
# Solve the linear equation:
|
||||
# k_a ax + k_b * bx = px
|
||||
# k_a ay + k_b * by = py
|
||||
|
||||
|
||||
M = numpy.array([[ax, bx], [ay, by]])
|
||||
V = numpy.array([px, py])
|
||||
|
||||
sol = numpy.linalg.solve(M,V)
|
||||
k1 = int(round(sol[0]))
|
||||
k2 = int(round(sol[1]))
|
||||
|
||||
if k1*ax + k2*bx == px and k1*ay + k2*by == py:
|
||||
print("yes", k1, k2)
|
||||
#print("yes", k1, k2)
|
||||
total += 3*k1 + k2
|
||||
|
||||
for record in records:
|
||||
ax, ay, bx, by, px, py = record
|
||||
|
||||
# Part 2
|
||||
#
|
||||
# Can't use numpy because the values are too large for floats
|
||||
# So here's an ad-hoc solver
|
||||
qx = px + 10000000000000
|
||||
qy = py + 10000000000000
|
||||
|
||||
px += 10000000000000
|
||||
py += 10000000000000
|
||||
V2 = numpy.array([qx, qy])
|
||||
sol = numpy.linalg.solve(M,V2)
|
||||
k1 = int(round(sol[0]))
|
||||
k2 = int(round(sol[1]))
|
||||
|
||||
m00, m01 = ax, bx
|
||||
m10, m11 = ay, by
|
||||
v1 = px
|
||||
v2 = py
|
||||
|
||||
# subtract c*eq1 from eq2 to cancel first coefficient
|
||||
c = F(m10,m00)
|
||||
m10 -= c*m00
|
||||
m11 -= c*m01
|
||||
v2 -= c*v1
|
||||
if k1*ax + k2*bx == qx and k1*ay + k2*by == qy:
|
||||
#print("yes2", k1, k2)
|
||||
total2 += 3*k1 + k2
|
||||
|
||||
# scale eq2
|
||||
c = F(1,m11)
|
||||
v2 *= c
|
||||
m11 *= c
|
||||
|
||||
# subtract c*eq2 from eq1 to cancel second coefficient
|
||||
c = F(m01,m11)
|
||||
m01 -= c*m11
|
||||
v1 -= c*v2
|
||||
|
||||
# scale eq1
|
||||
c = F(1,m00)
|
||||
v1 *= c
|
||||
m00 *= c
|
||||
|
||||
# we now have
|
||||
# 1*k_a + 0 = v1
|
||||
# 0 + 1*k_b = v2
|
||||
|
||||
print(m00, m01, m10, m11, v1, v2)
|
||||
if v1.denominator == 1 and v2.denominator == 1 and v1 > 0 and v2 > 0:
|
||||
if v1*ax + v2*bx == px and v1*ay + v2*by == py:
|
||||
print("yes2", v1, v2)
|
||||
total2 += 3*v1 + v2
|
||||
|
||||
|
||||
print(total)
|
||||
print(total2)
|
||||
|
||||
|
|
Loading…
Reference in New Issue