Skip to content

Commit 3578e19

Browse files
authored
AOC 2024 03
Signed-off-by: GitHub <noreply@github.com>
1 parent 9ac922f commit 3578e19

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import re
2+
from math import prod
3+
from pathlib import Path
4+
5+
6+
def part1(text: str) -> int:
7+
return sum(int(x) * int(y) for x, y in re.findall(r"mul\((\d{1,3}),(\d{1,3})\)", text, re.MULTILINE))
8+
9+
10+
def part2(text: str) -> int:
11+
do_indexes = [match.start(0) for match in re.finditer(r"do\(\)", text)]
12+
dont_indexes = [match.start(0) for match in re.finditer(r"don't\(\)", text)]
13+
map_ = [True] * len(text)
14+
in_do = True
15+
for counter in range(len(map_)):
16+
if counter in do_indexes:
17+
in_do = True
18+
elif counter in dont_indexes:
19+
in_do = False
20+
else:
21+
pass
22+
map_[counter] = in_do
23+
24+
return sum(
25+
prod(map(int, match.groups()))
26+
for match in re.finditer(r"mul\((\d{1,3}),(\d{1,3})\)", text, re.MULTILINE)
27+
if map_[match.start(0)]
28+
)
29+
30+
31+
if __name__ == "__main__":
32+
text = Path("../input.txt").read_text(encoding="locale")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from aoc_2024_03 import part1, part2
2+
3+
4+
def test_part1() -> None:
5+
assert part1("xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))") == 161
6+
7+
8+
def test_part2() -> None:
9+
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 commit comments

Comments
 (0)