-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.py
More file actions
70 lines (49 loc) · 1.78 KB
/
day13.py
File metadata and controls
70 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import re
import numpy as np
from scipy.optimize import linprog
from lib.input import get_input
from lib.map import Position
COST = [3, 1]
class Machine:
def __init__(self, A: Position, B: Position, prize: Position):
self.A = A
self.B = B
self.prize = prize
lines = get_input(2024, 13).split("\n\n")
machines: list[Machine] = []
for m in lines:
A = Position(*tuple(map(int, re.search(r"A: X\+(\d+), Y\+(\d+)", m).groups())))
B = Position(*tuple(map(int, re.search(r"B: X\+(\d+), Y\+(\d+)", m).groups())))
prize = Position(*tuple(map(int, re.search(r"Prize: X=(\d+), Y=(\d+)", m).groups())))
machines.append(Machine(A, B, prize))
def find_minimum_cost(machine: Machine, bounds=None):
A = np.array([[machine.A.x, machine.B.x], [machine.A.y, machine.B.y]])
b = np.array([machine.prize.x, machine.prize.y])
x0_bounds = (0, bounds)
x1_bounds = (0, bounds)
res = linprog(COST, A_eq=A, b_eq=b, bounds=[x0_bounds, x1_bounds], method="highs")
def check_op(a, b):
return (
machine.A.x * a + machine.B.x * b == machine.prize.x
and machine.A.y * a + machine.B.y * b == machine.prize.y
)
if not res.success:
raise ValueError("No solution found")
a, b = np.round(res.x).astype(int)
if not check_op(a, b):
raise ValueError("No solution found")
# [3 * a + b] constraint pre-calculated by the solver
return np.round(res.fun).astype(int)
def compute(bounds, mult):
res = 0
for machine in machines:
machine.prize += (mult, mult)
try:
res += find_minimum_cost(machine, bounds=bounds)
except ValueError:
pass
return res
def part1():
return compute(100, 0)
def part2():
return compute(None, 10000000000000)