-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: GitHub <[email protected]>
- Loading branch information
1 parent
9ac922f
commit e73cd1d
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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)) |
This file contains 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
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 |