can anyone help me fix ny numpy shit? silver actually works, but I think my rounding or something on gold is getting fucked up.
import re
import numpy as np
def Day13(data):
silver = 0; gold = 0
data = data.strip().splitlines()
games = []
for i in range(0, len(data), 4):
if not data[i]: continue
a = tuple(map(int,re.findall(r"(\d+)", data[i+0])))
b = tuple(map(int,re.findall(r"(\d+)", data[i+1])))
goal = tuple(map(int,re.findall(r"(\d+)", data[i+2])))
games.append((a, b, goal))
for ((x1, y1), (x2,y2), (gx,gy)) in games:
a = np.array([[x1, x2], [y1, y2]])
b = np.array([gx, gy])
c = np.array([gx+10000000000000, gy+10000000000000])
try:
result = np.linalg.solve(a, b)
a_presses = np.round(result[0])
b_presses = np.round(result[1])
if np.isclose(result[0], a_presses) \
and np.isclose(result[1], b_presses):
silver += int((a_presses * 3) + (b_presses * 1))
except np.linalg.LinAlgError:
continue
try:
result = np.linalg.solve(a, c)
a_presses = np.round(result[0])
b_presses = np.round(result[1])
if np.isclose(result[0], a_presses) \
and np.isclose(result[1], b_presses):
gold += int((a_presses * 3) + (b_presses * 1))
except np.linalg.LinAlgError:
continue
return (silver, gold)
data = """
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400\n
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176\n
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450\n
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
"""
print(Day13(data))
with open("2024_13.txt") as f:
data = f.read()
Day13(data)
# gold -- too high
# 10000000000000