mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 05:10:32 +01:00
Implement 2024 day 3
This commit is contained in:
25
2024/src/aoc/days/day3.py
Normal file
25
2024/src/aoc/days/day3.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import re
|
||||
|
||||
from . import SeparateRunner
|
||||
|
||||
|
||||
class DayRunner(SeparateRunner):
|
||||
@classmethod
|
||||
def part1(cls, data: str) -> int:
|
||||
return sum(int(a) * int(b) for a, b in re.findall(r"mul\((\d+),(\d+)\)", data))
|
||||
|
||||
@classmethod
|
||||
def part2(cls, data: str) -> int:
|
||||
do = True
|
||||
total = 0
|
||||
for op, a, b in re.findall(r"(don't\(\)|do\(\)|mul\((\d+),(\d+)\))", data):
|
||||
match op:
|
||||
case "do()":
|
||||
do = True
|
||||
case "don't()":
|
||||
do = False
|
||||
case _:
|
||||
if do:
|
||||
total += int(a) * int(b)
|
||||
|
||||
return total
|
||||
Reference in New Issue
Block a user