|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# |
| 3 | +# Advent of Code 2025 Day 12 |
| 4 | +# |
| 5 | + |
| 6 | +import sys |
| 7 | +from dataclasses import dataclass |
| 8 | +from math import prod |
| 9 | +from typing import Self |
| 10 | + |
| 11 | +from aoc import my_aocd |
| 12 | +from aoc.common import InputData |
| 13 | +from aoc.common import SolutionBase |
| 14 | +from aoc.common import aoc_samples |
| 15 | + |
| 16 | +TEST = """\ |
| 17 | +0: |
| 18 | +### |
| 19 | +##. |
| 20 | +##. |
| 21 | +
|
| 22 | +1: |
| 23 | +### |
| 24 | +##. |
| 25 | +.## |
| 26 | +
|
| 27 | +2: |
| 28 | +.## |
| 29 | +### |
| 30 | +##. |
| 31 | +
|
| 32 | +3: |
| 33 | +##. |
| 34 | +### |
| 35 | +##. |
| 36 | +
|
| 37 | +4: |
| 38 | +### |
| 39 | +#.. |
| 40 | +### |
| 41 | +
|
| 42 | +5: |
| 43 | +### |
| 44 | +.#. |
| 45 | +### |
| 46 | +
|
| 47 | +4x4: 0 0 0 0 2 0 |
| 48 | +12x5: 1 0 1 0 2 2 |
| 49 | +12x5: 1 0 1 0 3 2 |
| 50 | +""" |
| 51 | + |
| 52 | + |
| 53 | +@dataclass(frozen=True) |
| 54 | +class Shape: |
| 55 | + area: int |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def from_input(cls, lines: list[str]) -> Self: |
| 59 | + return cls(len(lines) * max(len(line) for line in lines)) |
| 60 | + |
| 61 | + |
| 62 | +@dataclass(frozen=True) |
| 63 | +class Region: |
| 64 | + area: int |
| 65 | + quantities: dict[int, int] |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def from_input(cls, string: str) -> Self: |
| 69 | + sa, sq = string.split(": ") |
| 70 | + area = prod(int(x) for x in sa.split("x")) |
| 71 | + quantities = {i: int(q) for i, q in enumerate(sq.split())} |
| 72 | + return cls(area, quantities) |
| 73 | + |
| 74 | + |
| 75 | +Input = tuple[list[Region], dict[int, Shape]] |
| 76 | +Output1 = int |
| 77 | +Output2 = str |
| 78 | + |
| 79 | + |
| 80 | +class Solution(SolutionBase[Input, Output1, Output2]): |
| 81 | + def parse_input(self, input_data: InputData) -> Input: |
| 82 | + blocks = my_aocd.to_blocks(input_data) |
| 83 | + shapes = { |
| 84 | + int(block[0][:-1]): Shape.from_input(block[1:]) |
| 85 | + for block in blocks[:-1] |
| 86 | + } |
| 87 | + regions = [Region.from_input(line) for line in blocks[-1]] |
| 88 | + return regions, shapes |
| 89 | + |
| 90 | + def part_1(self, inputs: Input) -> Output1: |
| 91 | + regions, shapes = inputs |
| 92 | + return sum( |
| 93 | + sum( |
| 94 | + quantity * shapes[idx].area |
| 95 | + for idx, quantity in region.quantities.items() |
| 96 | + ) |
| 97 | + <= region.area |
| 98 | + for region in regions |
| 99 | + ) |
| 100 | + |
| 101 | + def part_2(self, _: Input) -> Output2: |
| 102 | + return "🎄" |
| 103 | + |
| 104 | + @aoc_samples(()) |
| 105 | + def samples(self) -> None: |
| 106 | + pass |
| 107 | + |
| 108 | + |
| 109 | +solution = Solution(2025, 12) |
| 110 | + |
| 111 | + |
| 112 | +def main() -> None: |
| 113 | + solution.run(sys.argv) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments