Skip to content

Commit 97b4079

Browse files
committed
AoC 2025 Day 5
1 parent 4faaf8c commit 97b4079

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- @BEGIN:ImplementationsTable:2025@ -->
99
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
1010
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
11-
| python3 | [](src/main/python/AoC2025_01.py) | [](src/main/python/AoC2025_02.py) | [](src/main/python/AoC2025_03.py) | [](src/main/python/AoC2025_04.py) | | | | | | | | |
11+
| python3 | [](src/main/python/AoC2025_01.py) | [](src/main/python/AoC2025_02.py) | [](src/main/python/AoC2025_03.py) | [](src/main/python/AoC2025_04.py) | [](src/main/python/AoC2025_05.py) | | | | | | | |
1212
| java | [](src/main/java/AoC2025_01.java) | [](src/main/java/AoC2025_02.java) | [](src/main/java/AoC2025_03.java) | [](src/main/java/AoC2025_04.java) | | | | | | | | |
1313
| bash | [](src/main/bash/AoC2025_01.sh) | [](src/main/bash/AoC2025_02.sh) | [](src/main/bash/AoC2025_03.sh) | [](src/main/bash/AoC2025_04.sh) | | | | | | | | |
1414
<!-- @END:ImplementationsTable:2025@ -->

src/main/python/AoC2025_05.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#! /usr/bin/env python3
2+
#
3+
# Advent of Code 2025 Day 5
4+
#
5+
6+
import sys
7+
8+
from aoc import my_aocd
9+
from aoc.common import InputData
10+
from aoc.common import SolutionBase
11+
from aoc.common import aoc_samples
12+
from aoc.range import RangeInclusive
13+
14+
Input = InputData
15+
Output1 = int
16+
Output2 = int
17+
18+
19+
TEST = """\
20+
3-5
21+
10-14
22+
16-20
23+
12-18
24+
25+
1
26+
5
27+
8
28+
11
29+
17
30+
32
31+
"""
32+
33+
34+
class Solution(SolutionBase[Input, Output1, Output2]):
35+
def parse_input(self, input_data: InputData) -> Input:
36+
return input_data
37+
38+
def part_1(self, inputs: Input) -> Output1:
39+
blocks = my_aocd.to_blocks(list(inputs))
40+
ranges = set[range]()
41+
for line in blocks[0]:
42+
lo, hi = map(int, line.split("-"))
43+
ranges.add(range(lo, hi + 1))
44+
ans = 0
45+
for line in blocks[1]:
46+
if any(int(line) in rng for rng in ranges):
47+
ans += 1
48+
return ans
49+
50+
def part_2(self, inputs: Input) -> Output2:
51+
blocks = my_aocd.to_blocks(list(inputs))
52+
ranges = set[RangeInclusive]()
53+
for line in blocks[0]:
54+
lo, hi = map(int, line.split("-"))
55+
ranges.add(RangeInclusive.between(lo, hi))
56+
merged = RangeInclusive.merge(ranges)
57+
ans = 0
58+
for rng in merged:
59+
ans += rng.len
60+
return ans
61+
62+
@aoc_samples(
63+
(
64+
("part_1", TEST, 3),
65+
("part_2", TEST, 14),
66+
)
67+
)
68+
def samples(self) -> None:
69+
pass
70+
71+
72+
solution = Solution(2025, 5)
73+
74+
75+
def main() -> None:
76+
solution.run(sys.argv)
77+
78+
79+
if __name__ == "__main__":
80+
main()

0 commit comments

Comments
 (0)