You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
879 B
32 lines
879 B
#!/usr/bin/env python3
|
|
|
|
import sys, re
|
|
|
|
|
|
if __name__ == '__main__':
|
|
instr = '\n'.join([line.strip('\n') for line in open(sys.argv[1])])
|
|
res1, res2 = 0, 0
|
|
do = True
|
|
dos = []
|
|
prev = 0
|
|
do = re.compile(r'(don\'t|do)\(\)')
|
|
for d in do.finditer(instr):
|
|
if d.group() == 'don\'t()' and do:
|
|
do = False
|
|
dos.append((prev, d.start()))
|
|
elif d.group() == 'do()' and not do:
|
|
do = True
|
|
prev = d.start()
|
|
if do: dos.append((prev, len(instr)))
|
|
|
|
mul = re.compile(r'mul\((?P<d1>\d{1,3}),(?P<d2>\d{1,3})\)', re.DOTALL)
|
|
for m in mul.finditer(instr):
|
|
res1 += int(m['d1']) * int(m['d2'])
|
|
if sum([m.start() in range(*r) for r in dos]): res2 += int(m['d1']) * int(m['d2'])
|
|
|
|
# challenge 1
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
print(f"challenge 2:\n{res2}")
|
|
|
|
|