Skip to content

Commit

Permalink
AOC 2024 03
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
shenanigansd authored Dec 5, 2024
1 parent 9ac922f commit e73cd1d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
34 changes: 34 additions & 0 deletions events/advent_of_code/2024/03/python/aoc_2024_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import re
from math import prod
from pathlib import Path


def part1(text: str) -> int:
return sum(int(x) * int(y) for x, y in re.findall(r"mul\((\d{1,3}),(\d{1,3})\)", text, re.MULTILINE))


def part2(text: str) -> int:
do_indexes = [match.start(0) for match in re.finditer(r"do\(\)", text)]
dont_indexes = [match.start(0) for match in re.finditer(r"don't\(\)", text)]
map_ = [True] * len(text)
in_do = True
for counter in range(len(map_)):
if counter in do_indexes:
in_do = True
elif counter in dont_indexes:
in_do = False
else:
pass
map_[counter] = in_do

return sum(
prod(map(int, match.groups()))
for match in re.finditer(r"mul\((\d{1,3}),(\d{1,3})\)", text, re.MULTILINE)
if map_[match.start(0)]
)


if __name__ == "__main__":
text = Path("../input.txt").read_text(encoding="locale")
print(part1(text))
print(part2(text))
9 changes: 9 additions & 0 deletions events/advent_of_code/2024/03/python/test_aoc_2024_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from aoc_2024_03 import part1, part2


def test_part1() -> None:
assert part1("xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))") == 161


def test_part2() -> None:
assert part2("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))") == 48

0 comments on commit e73cd1d

Please sign in to comment.