Skip to content
Snippets Groups Projects
Commit 0078bf02 authored by BoB's avatar BoB
Browse files

beating max :-0

parent a3c1c04d
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
```
#day13 - cleaned up code:
import re
def readfile(filename):
with open(filename, 'r') as file:
return [
[int(num) for num in re.findall(r'[\+=](\d+)', line)]
for line in file if line.strip()
]
numbers = readfile('input13.txt')
A, B, Prize = numbers[::3], numbers[1::3], numbers[2::3]
import numpy as np
extradist = 10000000000000 # part1: extradist = 0
totalcost = 0
for i in range(len(A)):
M = np.array([A[i], B[i]]).T # Transpose to get correct 2x2 matrix
p = np.array(Prize[i])[:, None] + extradist
x = np.linalg.solve(M, p)
x_round = np.rint(x).astype(int)
if np.allclose(M @ x_round - p, np.zeros_like(p)):
totalcost += int(3*x_round[0]+x_round[1])
print(f"total cost = {totalcost}")
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment