36 lines
1 KiB
Python
36 lines
1 KiB
Python
from typing import final
|
|
import numpy as np
|
|
|
|
from scipy.optimize import LinearConstraint, milp
|
|
|
|
with open(r'day10/input.txt', 'r') as input:
|
|
lines = input.read().split('\n')[:-1]
|
|
|
|
joltage = []
|
|
buttons = []
|
|
|
|
for line in lines:
|
|
split_line = line.split(' ')
|
|
buttons.append(list(map(lambda x: set(map(int,(x[1:-1]).split(','))),split_line[1:-1])))
|
|
joltage.append(tuple(map(lambda x:int(x),split_line[-1][1:-1].split(','))))
|
|
|
|
finalsum = 0
|
|
for i,buttonlist in enumerate(buttons):
|
|
c = np.array([1]*len(buttonlist))
|
|
A_array = []
|
|
BL_array = []
|
|
for j in range(len(joltage[i])):
|
|
array = []
|
|
for button in buttonlist:
|
|
array.append(1 if j in button else 0)
|
|
A_array.append(array)
|
|
BL_array.append(joltage[i][j])
|
|
A = np.array(A_array)
|
|
b_l = np.array(BL_array)
|
|
b_u = np.array(BL_array)
|
|
constraints = LinearConstraint(A,b_l,b_u)
|
|
integrality = np.ones_like(c)
|
|
res = milp(c=c, constraints=constraints, integrality=integrality)
|
|
print(res.x)
|
|
finalsum += sum(res.x)
|
|
print(finalsum)
|