Skip to content

Commit 110bd54

Browse files
committed
2024/13: Attempt to use scipi linear algebra solver
1 parent ac6c406 commit 110bd54

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

2024/d13.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/python
22
"""Advent of Code, Day 13: Claw Contraption."""
33
import math
4+
import numpy as np
5+
import scipy.linalg
46
import z3
57

68
from lib import aoc
@@ -79,18 +81,24 @@ def solver(self, puzzle_input: list[list[int]], part_one: bool) -> int | str:
7981
if solver.check() != z3.sat:
8082
continue
8183

82-
cost = solver.model()[cost].as_long()
84+
arr_ab = np.array([[chunks[0][0], chunks[1][0]], [chunks[0][1], chunks[1][1]]])
85+
arr_dest = np.array(chunks[2])
86+
got = scipy.linalg.solve(arr_ab, arr_dest)
87+
assert np.allclose(got, np.round(got), atol=0.01)
88+
got = np.round(got).astype(int)
89+
cost = round(got[0]) * 3 + round(got[1])
8390
tokens += cost
8491

85-
b = (l * t - m * s) / (n * k - m * l)
86-
a = (s - b * l) / k
87-
got = 3 * a + b
88-
if a != round(a) or b != round(b):
89-
print(f"Rounding doesn't work. {a=}, {b=}")
90-
if got == cost:
91-
print("Yes")
92-
else:
93-
print("No")
92+
if False:
93+
b = (l * t - m * s) / (n * k - m * l)
94+
a = (s - b * l) / k
95+
got = 3 * a + b
96+
if a != round(a) or b != round(b):
97+
print(f"Rounding doesn't work. {a=}, {b=}")
98+
if got == cost:
99+
print("Yes")
100+
else:
101+
print("No")
94102

95103
return tokens
96104

0 commit comments

Comments
 (0)