-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
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,43 @@ | ||
from collections.abc import Iterable | ||
from itertools import pairwise | ||
from typing import TypeAlias | ||
|
||
from src.aoc.aoc2024 import YEAR, get_day | ||
from src.aoc.aoc_helper import Aoc | ||
|
||
Nums: TypeAlias = Iterable[int] | ||
|
||
|
||
Nums: TypeAlias = Iterable[int] | ||
|
||
|
||
def safe(nums: Nums, /) -> bool: | ||
match [b - a for a, b in pairwise(nums)]: | ||
case [*diffs] if all(d in {1, 2, 3} for d in diffs) or all( | ||
d in {-1, -2, -3} for d in diffs | ||
): | ||
return True | ||
case _: | ||
return False | ||
|
||
|
||
def part_a(txt: str) -> int: | ||
return sum(safe(map(int, nums)) for nums in map(str.split, txt.splitlines())) | ||
|
||
|
||
def part_b(txt: str) -> int: | ||
return sum( | ||
safe(nums := list(map(int, line.split()))) | ||
or any(safe(nums[:i] + nums[i + 1 :]) for i in range(len(nums))) | ||
for line in txt.splitlines() | ||
) | ||
|
||
|
||
def main(txt: str) -> None: | ||
print("part_a: ", part_a(txt)) | ||
print("part_b: ", part_b(txt)) | ||
|
||
|
||
if __name__ == "__main__": | ||
aoc = Aoc(day=get_day(), years=YEAR) | ||
aoc.run(main, submit=True, part="both", readme_update=True) |
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
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,20 @@ | ||
import pytest | ||
|
||
from src.aoc.aoc2024 import day_02 as d | ||
|
||
TEST_INPUT = """ | ||
7 6 4 2 1 | ||
1 2 7 8 9 | ||
9 7 6 2 1 | ||
1 3 2 4 5 | ||
8 6 4 4 1 | ||
1 3 6 7 9 | ||
""".strip() | ||
|
||
|
||
def test_a() -> None: | ||
assert d.part_a(TEST_INPUT) == 2 | ||
|
||
|
||
def test_b() -> None: | ||
assert d.part_b(TEST_INPUT) == 4 |