Guest User

Untitled

a guest
Dec 10th, 2025
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. # Gonna use this to solve LP
  2. import re
  3. from pulp import LpMinimize, LpProblem, LpVariable, value, lpSum
  4.  
  5.  
  6.  
  7. def solve_machine(buttons, joltage):
  8. prob = LpProblem('Machine Problem', LpMinimize)
  9. variables = [LpVariable(f"x_{i}", lowBound=0, cat="Integer") for i in range(len(buttons))]
  10. prob += lpSum(variables[i] for i in range(len(variables))), "Objective"
  11. for i in range(len(joltage)):
  12. currVars = []
  13. for j in range(len(buttons)):
  14. if(i in buttons[j]):
  15. currVars.append(variables[j])
  16. prob += lpSum(currVars[i] for i in range(len(currVars))) == joltage[i], f"Constraint_{i}"
  17. # print(currVars, ",", joltage[i])
  18. prob.solve()
  19. ans = 0
  20. for v in variables:
  21. ans += v.value()
  22. # print(buttons, joltage)
  23. return ans
  24.  
  25. def main():
  26. ans = 0
  27. with open('../data/day10.txt', 'r') as f:
  28. for line in f.readlines():
  29. button_matches = re.findall(r'\((.*?)\)', line)
  30. buttons = [list(map(int, b.split(','))) for b in button_matches]
  31.  
  32. voltage_match = re.search(r'\{(.*?)\}', line)
  33. voltages = list(map(int, voltage_match.group(1).split(','))) if voltage_match else []
  34. ans += solve_machine(buttons, voltages)
  35. print(ans)
  36.  
  37.  
  38. if __name__ == '__main__':
  39. main()
Advertisement
Add Comment
Please, Sign In to add comment