-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.py
executable file
·116 lines (110 loc) · 3.9 KB
/
day18.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
109
110
111
112
113
114
115
116
#!/usr/bin/python3
#TODO range(1000) fixen met while loop om het fool proof t maken --> KNOWN BUG maar geen tijd meer.
def split(word):
return [char for char in word]
resultpart1 = 0
with open('day18.dat') as f:
for i in f:
result = []
i = split(i.strip().replace(" ",""))
count = 0
len_check = len(i)
while 1:
if len(i) == 1:
break
lent = 0
for jj in range(1000):
j = jj - lent
if j >= len(i):
break
if i[j] == "+":
if i[j-1].isnumeric() and i[j+1].isnumeric():
i[j] = str(int(i[j-1]) + int(i[j+1]))
del i[j-1]
del i[j]
lent += 2
j = jj - lent
if j >= len(i):
break
if i[j] == "*":
if i[j-1].isnumeric() and i[j+1].isnumeric():
i[j] = str(int(i[j-1]) * int(i[j+1]))
del i[j-1]
del i[j]
lent += 2
j = jj - lent
if j >= len(i):
break
if i[j] == "(":
if i[j+2] == ")":
del i[j]
del i[j+1]
lent += 2
j = jj - lent
resultpart1 += int(i[0])
print(f"part1: {resultpart1}")
resultpart2 = 0
with open('day18.dat') as f:
for i in f:
result = []
i = split(i.strip().replace(" ",""))
count = 0
len_check = len(i)
plutormultiply = True
while 1:
if len(i) == 1:
break
lent = 0
for jj in range(1000):
oldplotmultiply = plutormultiply
j = jj - lent
if j >= len(i):
break
if(plutormultiply):
if i[j] == "+":
flag = True
for c in i[j:]:
if c == ")":
flag = True
break
if c == "(":
flag = False
break
if flag:
if i[j-1].isnumeric() and i[j+1].isnumeric():
i[j] = str(int(i[j-1]) + int(i[j+1]))
del i[j-1]
del i[j]
lent += 2
j = jj - lent
if j >= len(i):
break
else:
if i[j] == "*":
flag = True
for c in i[j:]:
if c == ")":
flag = True
break
if c == "(":
flag = False
break
if flag:
if i[j-1].isnumeric() and i[j+1].isnumeric():
i[j] = str(int(i[j-1]) * int(i[j+1]))
del i[j-1]
del i[j]
lent += 2
j = jj - lent
if j >= len(i):
break
if i[j] == "(":
if i[j+2] == ")":
del i[j]
del i[j+1]
lent += 2
plutormultiply = False
break
plutormultiply = not plutormultiply
resultpart2 += int(i[0])
print(f"part2: {resultpart2}")