From 110bd54c0e383f9a4365abd599a60a39a6f21ca4 Mon Sep 17 00:00:00 2001 From: Isaac Good Date: Mon, 16 Dec 2024 11:40:53 -0800 Subject: [PATCH] 2024/13: Attempt to use scipi linear algebra solver --- 2024/d13.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/2024/d13.py b/2024/d13.py index 0e40b78..ec191b0 100755 --- a/2024/d13.py +++ b/2024/d13.py @@ -1,6 +1,8 @@ #!/bin/python """Advent of Code, Day 13: Claw Contraption.""" import math +import numpy as np +import scipy.linalg import z3 from lib import aoc @@ -79,18 +81,24 @@ def solver(self, puzzle_input: list[list[int]], part_one: bool) -> int | str: if solver.check() != z3.sat: continue - cost = solver.model()[cost].as_long() + arr_ab = np.array([[chunks[0][0], chunks[1][0]], [chunks[0][1], chunks[1][1]]]) + arr_dest = np.array(chunks[2]) + got = scipy.linalg.solve(arr_ab, arr_dest) + assert np.allclose(got, np.round(got), atol=0.01) + got = np.round(got).astype(int) + cost = round(got[0]) * 3 + round(got[1]) tokens += cost - b = (l * t - m * s) / (n * k - m * l) - a = (s - b * l) / k - got = 3 * a + b - if a != round(a) or b != round(b): - print(f"Rounding doesn't work. {a=}, {b=}") - if got == cost: - print("Yes") - else: - print("No") + if False: + b = (l * t - m * s) / (n * k - m * l) + a = (s - b * l) / k + got = 3 * a + b + if a != round(a) or b != round(b): + print(f"Rounding doesn't work. {a=}, {b=}") + if got == cost: + print("Yes") + else: + print("No") return tokens