Last active
December 3, 2024 07:01
-
-
Save object/6475f60b8ab8e210d8f8ae11d09b8ea1 to your computer and use it in GitHub Desktop.
Advent of Code 2024, day 03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
with open("./data/input03.txt") as inputFile: | |
input = inputFile.read() | |
def evaluate(expr): | |
m = re.findall(r'\d{1,3}', expr) | |
return int(m[0]) * int(m[1]) | |
# Part 1 | |
res = 0 | |
for m in re.findall(r'mul\(\d{1,3},\d{1,3}\)', input): | |
res += evaluate(m) | |
print(res) | |
# Part 2 | |
res = 0 | |
enabled = True | |
for m in re.findall(r"(mul\(\d{1,3},\d{1,3}\))|(don't\(\))|(do\(\))", input): | |
if len(m[0]) > 0 and enabled: | |
res += evaluate(m[0]) | |
elif len(m[1]) > 0: | |
enabled = False | |
elif len(m[2]) > 0: | |
enabled = True | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment