-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.py
108 lines (78 loc) · 2.11 KB
/
day07.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import sys
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
equations = []
with open(inputfile,"r") as input:
for line in input:
eq = line.rstrip().split(":")
operands = [int(x) for x in eq[1].lstrip().split(" ")]
operands.reverse()
equations.append((int(eq[0]), operands))
def check(start):
tocheck = [start]
print(tocheck)
while len(tocheck) > 0:
eq = tocheck.pop(0)
if len(eq[1]) > 1:
if eq[0]/eq[1][0] == int(eq[0]/eq[1][0]):
newresult = int(eq[0]/eq[1][0])
tocheck.insert(0,(newresult,eq[1][1:]))
if eq[0] - eq[1][0] > 0:
tocheck.insert(1,(eq[0] - eq[1][0],eq[1][1:]))
else:
if eq[0] - eq[1][0] > 0:
tocheck.insert(1,(eq[0] - eq[1][0],eq[1][1:]))
else:
if eq[0]/eq[1][0] == int(eq[0]/eq[1][0]) == 1:
return True
if eq[0] - eq[1][0] == 0:
return True
return False
def check2(start):
tocheck = [start]
# print(tocheck)
while len(tocheck) > 0:
eq = tocheck.pop(0)
print(eq)
if len(eq[1]) > 1:
if eq[0]/eq[1][0] == int(eq[0]/eq[1][0]):
newresult = int(eq[0]/eq[1][0])
tocheck.insert(0,(newresult,eq[1][1:]))
if eq[0] - eq[1][0] > 0:
tocheck.insert(1,(eq[0] - eq[1][0],eq[1][1:]))
else:
if eq[0] - eq[1][0] > 0:
tocheck.insert(1,(eq[0] - eq[1][0],eq[1][1:]))
if str(eq[0]).endswith(str(eq[1][0])) and not eq[0] == eq[1][0]:
tocheck.insert(1,(int(str(eq[0])[:-len(str(eq[1][0]))]),eq[1][1:]))
else:
if eq[0]/eq[1][0] == int(eq[0]/eq[1][0]) == 1:
return True
if eq[0] - eq[1][0] == 0:
return True
return False
nope = []
total1 = 0
def run_part1():
global total1
print(equations)
for eq in equations:
if check(eq):
total1 += eq[0]
else:
nope.append(eq)
print(total1)
def run_part2():
global total1
total2 = 0
for eq in nope:
if check2(eq):
total2 += eq[0]
# print("OK" + str(eq))
print(total2)
print(total1+total2)
run_part1()
run_part2()