Skip to content

Commit

Permalink
day 02
Browse files Browse the repository at this point in the history
  • Loading branch information
vsedov committed Dec 2, 2024
1 parent d11e7e6 commit 20315aa
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/aoc/aoc2024/day_02.py
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)
2 changes: 1 addition & 1 deletion src/aoc/aoc2024/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
| Day | Problem | Part A | Part B | Complete |
|-----|---------|---------|---------|----------|
| 01 | [Historian Hysteria](https://adventofcode.com/2024/day/1) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| 02 | [?](https://adventofcode.com/2024/day/2) | :x: | :x: | :x: |
| 02 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| 03 | [?](https://adventofcode.com/2024/day/3) | :x: | :x: | :x: |
| 04 | [?](https://adventofcode.com/2024/day/4) | :x: | :x: | :x: |
| 05 | [?](https://adventofcode.com/2024/day/5) | :x: | :x: | :x: |
Expand Down
20 changes: 20 additions & 0 deletions tests/aoc2024/2024_day_02_test.py
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

0 comments on commit 20315aa

Please sign in to comment.