48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
from pulp import LpProblem, LpVariable, LpMinimize, lpSum, PULP_CBC_CMD
|
|
"""
|
|
ok to clarify the solution here:
|
|
modelling this as ilp ->
|
|
variable for each button representing how many times it is pressed - minimize this!
|
|
constraints for each joltage thingy? idk - (buttons that increment it * number of times pressed) *must equal goal so it matches expected value*!
|
|
if button doesnt increment 0 * wtv is still 0 so doesnt matter
|
|
boom done city
|
|
"""
|
|
|
|
|
|
def solve(goal, buttons): # hence another thing i havent done in years
|
|
prob = LpProblem("Operation_Dumbass_Elves", LpMinimize)
|
|
x = [LpVariable(f"x_{i}", lowBound=0, cat='Integer') for i in range(len(buttons))]
|
|
|
|
prob += lpSum(x), "Button Presses"
|
|
|
|
for i in range(len(goal)):
|
|
constraint = []
|
|
for j in range(len(buttons)):
|
|
M_ij = buttons[j][i]
|
|
constraint.append(M_ij * x[j])
|
|
prob += lpSum(constraint) == goal[i], f"Constraint_{i}"
|
|
|
|
prob.solve(PULP_CBC_CMD(msg=0))
|
|
|
|
return (int(prob.objective.value()))
|
|
|
|
|
|
with open('input.txt', 'r') as file:
|
|
lines = file.readlines()
|
|
|
|
count = 0
|
|
|
|
for line in lines:
|
|
line = line.rstrip('\n').split()
|
|
goal = tuple(map(int, line[-1].strip('{}').split(',')))
|
|
buttons = []
|
|
for i in range(1, len(line) - 1):
|
|
incs = map(int, line[i].strip('()').split(','))
|
|
button = [0] * len(goal)
|
|
for inc in incs:
|
|
button[inc] += 1
|
|
buttons.append(tuple(button))
|
|
|
|
count += solve(goal, buttons)
|
|
|
|
print(count) |