-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.py
75 lines (68 loc) · 2.15 KB
/
8.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3
import re
linereg = re.compile("(\w+) (inc|dec) (-?\d+) if (\w+) (\>|<|>=|<=|==|!=) (-?\d+)")
def parse(line):
match = linereg.match(line)
if match[2] == "inc":
modify = 0 + int(match[3])
else:
modify = 0 - int(match[3])
return {
"output": match[1],
"modify": modify,
"input": match[4],
"condition": match[5],
"value": int(match[6]),
}
def compute(data):
vars = {}
for line in data:
line = parse(line)
if line["output"] not in vars:
vars.update({line["output"]: 0})
if line["input"] not in vars:
vars.update({line["input"]: 0})
condition = False
if line["condition"] == "<":
if vars[line["input"]] < line["value"]:
condition = True
elif line["condition"] == ">":
if vars[line["input"]] > line["value"]:
condition = True
elif line["condition"] == ">=":
if vars[line["input"]] >= line["value"]:
condition = True
elif line["condition"] == "<=":
if vars[line["input"]] <= line["value"]:
condition = True
elif line["condition"] == "==":
if vars[line["input"]] == line["value"]:
condition = True
elif line["condition"] == "!=":
if vars[line["input"]] != line["value"]:
condition = True
else:
print(line)
if condition == True:
vars[line["output"]] += line["modify"]
yield vars[line["output"]]
return vars
def highest_ever(data):
maximum = 0
for iteration in compute(data):
if type(iteration) == int:
if iteration > maximum:
maximum = iteration
return maximum
def highest_end(data):
computer = compute(data)
while True:
try:
next(computer)
except StopIteration as end:
return max(end.value.values())
if __name__ == "__main__":
with open("8") as dfile:
data = dfile.readlines()
print("Solution 1:", highest_end(data))
print("Solution 2:", highest_ever(data))