mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 00:11:57 +00:00
chore: create new project structure and aoc.py runner script
This commit is contained in:
39
adventofcode/2024/day3/day3.py
Normal file
39
adventofcode/2024/day3/day3.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import re
|
||||
|
||||
|
||||
def main(content):
|
||||
operations = re.findall(r"(?:mul\((\d+),(\d+)\))|(do\(\))|(don't\(\))", content)
|
||||
|
||||
# filter only mul instructions for part1, format: ('498', '303', '', '')
|
||||
mul_operations = [x for x in operations if x[0].isnumeric()]
|
||||
total = sum(int(a) * int(b) for a, b, *_rest in mul_operations)
|
||||
print("Part 1: ", total)
|
||||
|
||||
do_mul = True
|
||||
total = 0
|
||||
for op in operations:
|
||||
token = "".join(op)
|
||||
print(token)
|
||||
if token.startswith("don't"):
|
||||
do_mul = False
|
||||
print("disable_mul")
|
||||
elif token.startswith("do"):
|
||||
do_mul = True
|
||||
elif token.isnumeric():
|
||||
if do_mul:
|
||||
a, b, *_rest = op
|
||||
total += int(a) * int(b)
|
||||
else:
|
||||
raise RuntimeError(f"Invalid token {token}")
|
||||
print("Part 2: ", total)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
import sys
|
||||
infile = sys.argv[1]
|
||||
|
||||
with open(infile) as f:
|
||||
content = f.read()
|
||||
main(content)
|
||||
|
Reference in New Issue
Block a user