-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
25 lines (22 loc) · 812 Bytes
/
day3.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
import re
def cleanseMemory(corruptedMem):
matchedNums = re.findall(mulPattern, corruptedMem)
return sum((int(firstEl) * int(secondEl)) for firstEl, secondEl in matchedNums)
def advC(corruptedMem):
mul_enabled = True
totale = 0
segmenti = re.split(r"(\bdo\(\)|\bdon\'t\(\))", corruptedMem)
for segmento in segmenti:
#reintegra la parentesi che si è "mangiata" la funzione split
segmento += ")"
if 'do()' in segmento:
mul_enabled = True
elif 'don\'t()' in segmento:
mul_enabled = False
if mul_enabled:
totale += cleanseMemory(segmento)
return totale
with open("input.txt") as F:
corruptedData = F.read()
mulPattern = r"mul\((\d{1,3}),(\d{1,3})\)"
print(advC(corruptedData))