|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# |
| 3 | +# Advent of Code 2024 Day 19 |
| 4 | +# |
| 5 | + |
| 6 | +import sys |
| 7 | + |
| 8 | +from aoc.common import InputData |
| 9 | +from aoc.common import SolutionBase |
| 10 | +from aoc.common import aoc_samples |
| 11 | + |
| 12 | +Input = tuple[set[str], list[str]] |
| 13 | +Output1 = int |
| 14 | +Output2 = int |
| 15 | + |
| 16 | + |
| 17 | +TEST = """\ |
| 18 | +r, wr, b, g, bwu, rb, gb, br |
| 19 | +
|
| 20 | +brwrr |
| 21 | +bggr |
| 22 | +gbbr |
| 23 | +rrbgbr |
| 24 | +ubwu |
| 25 | +bwurrg |
| 26 | +brgr |
| 27 | +bbrgwb |
| 28 | +""" |
| 29 | + |
| 30 | + |
| 31 | +class Solution(SolutionBase[Input, Output1, Output2]): |
| 32 | + def parse_input(self, input_data: InputData) -> Input: |
| 33 | + lines = list(input_data) |
| 34 | + return set(lines[0].split(", ")), lines[2:] |
| 35 | + |
| 36 | + def part_1(self, input: Input) -> Output1: |
| 37 | + towels, designs = input |
| 38 | + possible = set[str](towels) |
| 39 | + |
| 40 | + def find(w: str, pos: int) -> bool: |
| 41 | + if pos == len(w): |
| 42 | + return True |
| 43 | + pp = [p for p in possible if w[pos:].startswith(p)] |
| 44 | + for ppp in pp: |
| 45 | + possible.add(w[:pos + len(ppp)]) |
| 46 | + return any(find(w, pos + len(ppp)) for ppp in pp) |
| 47 | + |
| 48 | + ans = 0 |
| 49 | + for design in designs: |
| 50 | + if find(design, 0): |
| 51 | + ans += 1 |
| 52 | + return ans |
| 53 | + |
| 54 | + def part_2(self, input: Input) -> Output2: |
| 55 | + return 0 |
| 56 | + |
| 57 | + @aoc_samples( |
| 58 | + ( |
| 59 | + ("part_1", TEST, 6), |
| 60 | + # ("part_2", TEST, "TODO"), |
| 61 | + ) |
| 62 | + ) |
| 63 | + def samples(self) -> None: |
| 64 | + pass |
| 65 | + |
| 66 | + |
| 67 | +solution = Solution(2024, 19) |
| 68 | + |
| 69 | + |
| 70 | +def main() -> None: |
| 71 | + solution.run(sys.argv) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + main() |
0 commit comments