|
| 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