main
magical 2024-12-13 05:31:25 +00:00
parent 54c84304aa
commit 4000af8bf3
3 changed files with 1372 additions and 0 deletions

1279
day13/input 100644

File diff suppressed because it is too large Load Diff

15
day13/sample1.in 100644
View File

@ -0,0 +1,15 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279

78
day13/sol.py 100644
View File

@ -0,0 +1,78 @@
def parse(file):
while True:
a = file.readline()
if not a:
break
b = file.readline()
pr = file.readline()
blank = file.readline()
ax, ay = parse_numbers(a)
bx, by = parse_numbers(b)
px, py = parse_numbers(pr)
yield ax,ay, bx,by, px,py
import re
def parse_numbers(s):
return [int(x) for x in re.findall(r"\d+", s)]
from fractions import Fraction as F
import numpy
def solve(file):
total = 0
total2 = 0
for record in parse(file):
#print(record)
ax, ay, bx, by, px, py = record
# 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)
total += 3*k1 + k2
m00 = ax
m01 = bx
m10 = ay
m11 = by
v1 = px+ 10000000000000
v2 = py+ 10000000000000
c = F(m10,m00)
m10 -= c*m00
m11 -= c*m01
v2 -= c*v1
c = F(1,m11)
v2 *= c
m11 *= c
c = F(m01,m11)
m01 -= c*m11
v1 -= c*v2
c = F(1,m00)
v1 *= c
m00 *= c
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)
solve(open("sample1.in"))
solve(open("input"))