Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Input: lines like "[<part1>] (<part2>) ... {<part3>}"
- # part1 = string of . #
- # part2, part3 = comma-separated list of numbers
- # Output: each part2 button increases elements of that line's part3 (0-indexed)
- # find fewest # presses to get each line from all 0 to part3
- # ignore part1 for this half
- import numpy
- from scipy.optimize import LinearConstraint
- from scipy.optimize import milp
- def minimum_presses(part2_list, part3_list):
- # https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.milp.html
- # minimize total number of presses
- c_coefficients = []
- for part2 in part2_list:
- c_coefficients.append(1)
- c = numpy.array(c_coefficients)
- # effect of a combination of presses on part3
- f_list = []
- for part3_index in range(0, len(part3_list)):
- f = []
- for part2 in part2_list:
- f.append(1 if part3_index in part2 else 0)
- f_list.append(f)
- a = numpy.array(f_list)
- # desired effect
- b_u = numpy.array(part3_list)
- b_l = b_u
- constraints = LinearConstraint(a, b_l, b_u)
- integrality = numpy.ones_like(c) # array of 1's with same shape as c
- res = milp(c=c, constraints=constraints, integrality=integrality)
- return int(res.fun)
- def string_without_ends(s):
- return s[1:len(s)-1]
- total = 0
- file = open("10_input.txt", "r")
- for line in file:
- line = line.replace("\n", "")
- chunks = line.split(" ")
- part1 = string_without_ends(chunks.pop(0))
- part3 = string_without_ends(chunks.pop())
- part3_list = []
- for num_str in part3.split(","):
- part3_list.append(int(num_str))
- part2_list = []
- for chunk in chunks:
- part2_str = string_without_ends(chunk)
- part2 = []
- for num_str in part2_str.split(","):
- part2.append(int(num_str))
- part2_list.append(part2)
- total += minimum_presses(part2_list, part3_list)
- print (total)
Advertisement
Add Comment
Please, Sign In to add comment