2024 day 3 minor optimization

master
kwout 1 month ago
parent d1f87266e1
commit 91f8845a23
  1. 12
      2024/3.py

@ -5,13 +5,8 @@ lines = [l for l in open("input.txt", 'r').read().splitlines()]
summa, summa2, do = 0, 0, True summa, summa2, do = 0, 0, True
for l in lines: for l in lines:
x = [i.span() for i in re.finditer(r"mul\([0-9]{1,3},[0-9]{1,3}\)", l)]
x2 = [i.span() for i in re.finditer(r"mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don't\(\)", l)] x2 = [i.span() for i in re.finditer(r"mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don't\(\)", l)]
for start, end in x:
tup = l[start:end].split(',')
summa += int(tup[0][4:]) * int(tup[1][:-1])
for start,end in x2: for start,end in x2:
if l[start:end] == 'do()': if l[start:end] == 'do()':
do = True do = True
@ -19,7 +14,12 @@ for l in lines:
do = False do = False
elif do: elif do:
tup = l[start:end].split(',') tup = l[start:end].split(',')
summa2 += int(tup[0][4:]) * int(tup[1][:-1]) prod = int(tup[0][4:]) * int(tup[1][:-1])
summa2 += prod
summa += prod
else :
tup = l[start:end].split(',')
summa += int(tup[0][4:]) * int(tup[1][:-1])
print(summa) print(summa)
print(summa2) print(summa2)
Loading…
Cancel
Save