# /// script # requires-python = ">=3.13" # dependencies = ["pytest>=8"] # /// import sys import time from enum import StrEnum from itertools import permutations from pathlib import Path INPUT = Path(__file__).parent / "data/inputs/03.txt" class Field(StrEnum): TREE = "#" FREE = "." type Position = complex type Distance2D = complex def create_forest(puzzle: str) -> set[Position]: return { complex(x, y) for y, line in enumerate(puzzle.strip().splitlines()) for x, char in enumerate(line.strip()) if char == "#" } def part_one(puzzle: str) -> int: forest = create_forest(puzzle) max_x = int(max(p.real for p in forest)) squares = 0 for pos in forest: for x in range(int(pos.real) + 1, max_x + 1): if is_aligned_square(forest, pos, x + pos.imag * 1j): squares += 1 return squares def is_aligned_square(forest: set[Position], pos_lo: Position, pos_ro: Position) -> bool: assert pos_lo in forest distance = pos_ro.real - pos_lo.real pos_lu = pos_lo + distance * 1j pos_ru = pos_ro + distance * 1j return pos_ro in forest and pos_lu in forest and pos_ru in forest def part_two(puzzle: str) -> int: forest = create_forest(puzzle) squares = set[frozenset[Position]]() for a, b in permutations(forest, 2): # Now check two squares, as this can now go only in two directions: dis = b - a c = a + dis.real * 1j - dis.imag d = b + dis.real * 1j - dis.imag if c in forest and d in forest: squares.add(frozenset((a, b, c, d))) return len(squares) def main() -> None: try: text = INPUT.read_text() except FileNotFoundError: raise SystemExit("no input, run `./aoc get 03`") from None # `./aoc submit` parses these "Part N: " lines, keep the prefix. Timing goes to stderr. for n, part in [(1, part_one), (2, part_two)]: t = time.perf_counter() print(f"Part {n}: {part(text)}") print(f" ({time.perf_counter() - t:.2f}s)", file=sys.stderr) if __name__ == "__main__": main() EXAMPLES_1_1 = ( """ #...# ..... ..... ..... #...# """, """ .#..# ..... ..... .#..# ..... """, """ ..... ..... .##.. .##.. ..... """, """ #.### .##.# ##.## .#.#. .##.# """, ) EXAMPLES_1_2 = ( """ #..# .##. .##. #..# """, """ #...# ..... ..#.# ..... #.#.# """, """ #...#.. ....... ..#...# ....... #...#.. ....... ..#...# """, """ .##.. .##.# ..... #.##. ..##. """, ) def test_part_one_example_1(): for puzzle in EXAMPLES_1_1: assert part_one(puzzle) == 1 def test_part_one_example_2(): for puzzle in EXAMPLES_1_2: assert part_one(puzzle) == 2 def test_part_one_example_5_30(): example = """ #.#.# ..... #.#.# ..... #.#.# """ assert part_one(example) == 5 example = """ ##### ##### ##### ##### ##### """ assert part_one(example) == 30 def test_part_one_solution(): text = INPUT.read_text() assert part_one(text) == 314 def test_part_two_example_1(): examples = """ #...# ..... ..... ..... #...# .#... ....# ..... #.... ...#. ..#.. ..... #...# ..... ..#.. ....#.. ....... #...... ....... ......# ....... ..#.... """ for example in examples.split("\n\n"): assert part_two(example.strip()) == 1 def test_part_two_6_1(): example = """ #..#..# ....... ....... #..#..# ....... ....... #..#..# """ assert part_two(example) == 6 def test_part_two_6_2(): example = """ ..#.... ....#.. .#....# ...#... #....#. ..#.... ....#.. """ assert part_two(example) == 6 def test_part_two_solution(): assert part_two(INPUT.read_text().strip()) == 1596