Skip to content

Commit

Permalink
2024/13: Attempt to use scipi linear algebra solver
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacG committed Dec 16, 2024
1 parent ac6c406 commit 110bd54
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions 2024/d13.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 110bd54

Please sign in to comment.