with open('data-2024-03.txt', 'r') as f:
inp = f.read().splitlines()Advent of Code 2024 Day 3
— Day 3: Mull It Over —
We need to multiply some numbers from corrupted computer memory.
Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
Regex, amirite?
import re
patt = r'mul\([0-9]+,[0-9]+\)'
mul_out = []
for i in inp:
mul_out += re.findall(patt, i)
# mul_out
def get_digi(z):
a,b = z.split(',')
c,d = a.split('(')
e = b[:-1]
return int(d) * int(e)
digi = [get_digi(x) for x in mul_out]
# digi
sum(digi)
# 174561379174561379
— Part Two —
There are some do() and some don't() instructions which turn on and turn off the mul() instructions.
Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
patt = r'mul\([0-9]+,[0-9]+\)|do\(\)|don\'t\(\)'
new_out = []
for i in inp:
new_out += re.findall(patt, i)
# new_out
nice_out = []
do_dont = True
for n in new_out:
if n == 'do()':
do_dont = True
elif n == "don't()":
do_dont = False
else:
if do_dont:
nice_out.append(n)
len(mul_out)
len(new_out)
len(nice_out)
# nice_out
digi = [get_digi(x) for x in nice_out]
# digi
sum(digi)
# 106921067106921067