|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# |
| 3 | +# Advent of Code 2025 Day 4 |
| 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 | +from aoc.geometry import Direction |
| 12 | +from aoc.grid import Cell |
| 13 | + |
| 14 | +Input = InputData |
| 15 | +Output1 = int |
| 16 | +Output2 = int |
| 17 | + |
| 18 | + |
| 19 | +TEST = """\ |
| 20 | +..@@.@@@@. |
| 21 | +@@@.@.@.@@ |
| 22 | +@@@@@.@.@@ |
| 23 | +@.@@@@..@. |
| 24 | +@@.@@@@.@@ |
| 25 | +.@@@@@@@.@ |
| 26 | +.@.@.@.@@@ |
| 27 | +@.@@@.@@@@ |
| 28 | +.@@@@@@@@. |
| 29 | +@.@.@@@.@. |
| 30 | +""" |
| 31 | + |
| 32 | + |
| 33 | +class Solution(SolutionBase[Input, Output1, Output2]): |
| 34 | + def parse_input(self, input_data: InputData) -> Input: |
| 35 | + return input_data |
| 36 | + |
| 37 | + def part_1(self, inputs: Input) -> Output1: |
| 38 | + grid = list(inputs) |
| 39 | + ans = 0 |
| 40 | + for r, row in enumerate(grid): |
| 41 | + for c, ch in enumerate(row): |
| 42 | + if ch == "@": |
| 43 | + cnt = 0 |
| 44 | + cell = Cell(r, c) |
| 45 | + for d in Direction.octants(): |
| 46 | + n = cell.at(d) |
| 47 | + if ( |
| 48 | + 0 <= n[0] < len(grid) |
| 49 | + and 0 <= n[1] < len(row) |
| 50 | + and grid[n[0]][n[1]] == "@" |
| 51 | + ): |
| 52 | + cnt += 1 |
| 53 | + if cnt < 4: |
| 54 | + ans += 1 |
| 55 | + return ans |
| 56 | + |
| 57 | + def part_2(self, inputs: Input) -> Output2: |
| 58 | + grid = [list(row) for row in inputs] |
| 59 | + ans = 0 |
| 60 | + while True: |
| 61 | + new_grid = [list(row) for row in grid] |
| 62 | + for r, row in enumerate(grid): |
| 63 | + for c, ch in enumerate(row): |
| 64 | + if ch == "@": |
| 65 | + cnt = 0 |
| 66 | + cell = Cell(r, c) |
| 67 | + for d in Direction.octants(): |
| 68 | + n = cell.at(d) |
| 69 | + if ( |
| 70 | + 0 <= n[0] < len(grid) |
| 71 | + and 0 <= n[1] < len(row) |
| 72 | + and grid[n[0]][n[1]] == "@" |
| 73 | + ): |
| 74 | + cnt += 1 |
| 75 | + if cnt < 4: |
| 76 | + new_grid[r][c] = "." |
| 77 | + ans += 1 |
| 78 | + if new_grid == grid: |
| 79 | + break |
| 80 | + grid = new_grid |
| 81 | + return ans |
| 82 | + |
| 83 | + @aoc_samples( |
| 84 | + ( |
| 85 | + ("part_1", TEST, 13), |
| 86 | + ("part_2", TEST, 43), |
| 87 | + ) |
| 88 | + ) |
| 89 | + def samples(self) -> None: |
| 90 | + pass |
| 91 | + |
| 92 | + |
| 93 | +solution = Solution(2025, 4) |
| 94 | + |
| 95 | + |
| 96 | +def main() -> None: |
| 97 | + solution.run(sys.argv) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main() |
0 commit comments