From 1c7035d43331fc4360e0285a880cd1b86ca79022 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Fri, 13 Dec 2024 05:41:12 +0000 Subject: [PATCH] day 13 cleanup --- day13/sol.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/day13/sol.py b/day13/sol.py index 3e81a73..7957da3 100644 --- a/day13/sol.py +++ b/day13/sol.py @@ -22,10 +22,14 @@ import numpy def solve(file): total = 0 total2 = 0 - for record in parse(file): + 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 @@ -38,34 +42,50 @@ def solve(file): print("yes", k1, k2) total += 3*k1 + k2 + for record in records: + ax, ay, bx, by, px, py = record - m00 = ax - m01 = bx - m10 = ay - m11 = by - v1 = px+ 10000000000000 - v2 = py+ 10000000000000 + # Part 2 + # + # Can't use numpy because the values are too large for floats + # So here's an ad-hoc solver + + px += 10000000000000 + py += 10000000000000 + + 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 + # 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: + if v1*ax + v2*bx == px and v1*ay + v2*by == py: print("yes2", v1, v2) total2 += 3*v1 + v2