-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.py
More file actions
50 lines (46 loc) · 1.55 KB
/
Copy pathday10.py
File metadata and controls
50 lines (46 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import re
from z3 import *
buttonSets = []
joltages = []
with open('101225', 'r') as f:
content = f.read()
lines = content.split("\n")
for each in lines:
matches = re.findall(r'\{([^\{\}]*)\}', each)
if matches:
joltages.append([int(i) for i in matches[0].split(",")])
# print(joltages)
button_matches = re.findall(r'\(([^\(\)]*)\)', each)
buttonSet = []
if button_matches:
for each in button_matches:
buttonSet.append([int(i) for i in each.split(",")])
buttonSets.append(buttonSet)
# print(buttonSet)
ans = 0
for i in range(len(joltages)):
buttonSet = buttonSets[i]
joltage = joltages[i]
length = len(buttonSet)
solver = Optimize()
vars = [Int(f'x{k}') for k in range(length)]
var_dict = {f'x{k}': vars[k] for k in range(length)}
for each in vars:
solver.add(each >= 0)
equations = ["" for _ in range(len(joltage))]
minimize = ["" for _ in range(len(joltage))]
for position, value in enumerate(joltage):
terms = []
for button_position, button in enumerate(buttonSet):
if (position in button):
terms.append(str(vars[button_position]))
equations[position] = " + ".join(terms) + " == " + str(value)
minimize[position] = " + ".join(terms)
print(equations[position])
[solver.add(eval(equation, var_dict)) for equation in equations]
solver.minimize(Sum(vars))
if solver.check() == sat:
model = solver.model()
for each in vars:
ans += model[each].as_long()
print(ans)