Advent of Code: Day 3

 Link to problem: https://adventofcode.com/2024/day/3

Oh no... regex. I hate regex. Perplexity is great at regex.

import re
pattern = r"mul\((\d{1,3}),\s*(\d{1,3})\)"
matches = re.findall(pattern, input)
print(matches)

Thanks Perplexity!

sum = 0
for a,b in matches:
product = int(a) * int(b)
sum += product
print(sum)

That's problem 1 done.

Now... for problem 2:

pattern = r"mul\(\d{1,3},\s?\d{1,3}\)|do\(\)|don't\(\)"
matches = re.findall(pattern, input)
print(matches)

Thanks again Perplexity!

do = True
sum = 0
for i in matches:
if i == 'do()':
do = True
continue
if i == "don't()":
do = False
continue
if do:
a,b = i.lstrip('mul(').rstrip(')').split(',')
product = int(a) * int(b)
sum += product
print(sum)

This one felt like cheating... I'll come back to do this in some terrible language or just do the string processing manually.

Answers : 156388521, 75920122

Comments