Compare commits
No commits in common. "7cc13becde944812ea92496b5a8d6ac6bb6e4c58" and "54c84304aa4eb58afd5dae4f77e44ce61d307f77" have entirely different histories.
7cc13becde
...
54c84304aa
1279
day13/input
1279
day13/input
File diff suppressed because it is too large
Load Diff
|
@ -1,15 +0,0 @@
|
||||||
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
|
|
65
day13/sol.py
65
day13/sol.py
|
@ -1,65 +0,0 @@
|
||||||
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
|
|
||||||
records = list(parse(file))
|
|
||||||
for record in records:
|
|
||||||
#print(record)
|
|
||||||
ax, ay, bx, by, px, py = record
|
|
||||||
|
|
||||||
# Part 1
|
|
||||||
|
|
||||||
# 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)
|
|
||||||
total += 3*k1 + k2
|
|
||||||
|
|
||||||
# Part 2
|
|
||||||
qx = px + 10000000000000
|
|
||||||
qy = py + 10000000000000
|
|
||||||
|
|
||||||
V2 = numpy.array([qx, qy])
|
|
||||||
sol = numpy.linalg.solve(M,V2)
|
|
||||||
k1 = int(round(sol[0]))
|
|
||||||
k2 = int(round(sol[1]))
|
|
||||||
|
|
||||||
if k1*ax + k2*bx == qx and k1*ay + k2*by == qy:
|
|
||||||
#print("yes2", k1, k2)
|
|
||||||
total2 += 3*k1 + k2
|
|
||||||
|
|
||||||
print(total)
|
|
||||||
print(total2)
|
|
||||||
|
|
||||||
|
|
||||||
solve(open("sample1.in"))
|
|
||||||
solve(open("input"))
|
|
Loading…
Reference in New Issue