Guest User

Untitled

a guest
Dec 10th, 2025
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. # Input: lines like "[<part1>] (<part2>) ... {<part3>}"
  2. #        part1 = string of . #
  3. #        part2, part3 = comma-separated list of numbers
  4. # Output: each part2 button increases elements of that line's part3 (0-indexed)
  5. #         find fewest # presses to get each line from all 0 to part3
  6. #         ignore part1 for this half
  7.  
  8. import numpy
  9. from scipy.optimize import LinearConstraint
  10. from scipy.optimize import milp
  11.  
  12. def minimum_presses(part2_list, part3_list):
  13.  
  14.   # https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.milp.html
  15.  
  16.   # minimize total number of presses
  17.  
  18.   c_coefficients = []
  19.   for part2 in part2_list:
  20.     c_coefficients.append(1)
  21.   c = numpy.array(c_coefficients)
  22.  
  23.   # effect of a combination of presses on part3
  24.  
  25.   f_list = []
  26.   for part3_index in range(0, len(part3_list)):
  27.     f = []
  28.     for part2 in part2_list:
  29.       f.append(1 if part3_index in part2 else 0)
  30.     f_list.append(f)
  31.   a = numpy.array(f_list)
  32.  
  33.   # desired effect
  34.  
  35.   b_u = numpy.array(part3_list)
  36.   b_l = b_u
  37.  
  38.   constraints = LinearConstraint(a, b_l, b_u)
  39.  
  40.   integrality = numpy.ones_like(c) # array of 1's with same shape as c
  41.  
  42.   res = milp(c=c, constraints=constraints, integrality=integrality)
  43.  
  44.   return int(res.fun)
  45.  
  46. def string_without_ends(s):
  47.   return s[1:len(s)-1]
  48.  
  49. total = 0
  50.  
  51. file = open("10_input.txt", "r")
  52. for line in file:
  53.   line = line.replace("\n", "")
  54.   chunks = line.split(" ")
  55.   part1 = string_without_ends(chunks.pop(0))
  56.   part3 = string_without_ends(chunks.pop())
  57.   part3_list = []
  58.   for num_str in part3.split(","):
  59.     part3_list.append(int(num_str))
  60.   part2_list = []
  61.   for chunk in chunks:
  62.     part2_str = string_without_ends(chunk)
  63.     part2 = []
  64.     for num_str in part2_str.split(","):
  65.       part2.append(int(num_str))
  66.     part2_list.append(part2)
  67.   total += minimum_presses(part2_list, part3_list)
  68.  
  69. print (total)
Advertisement
Add Comment
Please, Sign In to add comment