|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# |
| 3 | +# Advent of Code 2017 Day 20 |
| 4 | +# |
| 5 | +from __future__ import annotations |
| 6 | +from aoc import my_aocd |
| 7 | +from aoc.geometry3d import Vector3D, Position3D |
| 8 | +import aocd |
| 9 | + |
| 10 | + |
| 11 | +ORIGIN = Position3D.of(0, 0, 0) |
| 12 | +HEADING = { |
| 13 | + "n": Vector3D.of(0, -1, 1), |
| 14 | + "ne": Vector3D.of(1, -1, 0), |
| 15 | + "se": Vector3D.of(1, 0, -1), |
| 16 | + "s": Vector3D.of(0, 1, -1), |
| 17 | + "sw": Vector3D.of(-1, 1, 0), |
| 18 | + "nw": Vector3D.of(-1, 0, 1), |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +def _parse(inputs: tuple[str]) -> list[Vector3D]: |
| 23 | + assert len(inputs) == 1 |
| 24 | + return [HEADING[p] for p in inputs[0].split(",")] |
| 25 | + |
| 26 | + |
| 27 | +def _positions(path: list[Vector3D]) -> list[Position3D]: |
| 28 | + positions = [ORIGIN] |
| 29 | + [positions.append(positions[-1].translate(p)) for p in path] |
| 30 | + return positions |
| 31 | + |
| 32 | + |
| 33 | +def _steps(p: Position3D) -> int: |
| 34 | + return p.manhattan_distance_to_origin() // 2 |
| 35 | + |
| 36 | + |
| 37 | +def part_1(inputs: tuple[str]) -> int: |
| 38 | + path = _parse(inputs) |
| 39 | + return _steps(_positions(path)[-1]) |
| 40 | + |
| 41 | + |
| 42 | +def part_2(inputs: tuple[str]) -> int: |
| 43 | + path = _parse(inputs) |
| 44 | + return max(_steps(p) for p in _positions(path)) |
| 45 | + |
| 46 | + |
| 47 | +TEST1 = """ne,ne,ne""".splitlines() |
| 48 | +TEST2 = """ne,ne,sw,sw""".splitlines() |
| 49 | +TEST3 = """ne,ne,s,s""".splitlines() |
| 50 | +TEST4 = """se,sw,se,sw,sw""".splitlines() |
| 51 | + |
| 52 | + |
| 53 | +def main() -> None: |
| 54 | + puzzle = aocd.models.Puzzle(2017, 11) |
| 55 | + my_aocd.print_header(puzzle.year, puzzle.day) |
| 56 | + |
| 57 | + assert part_1(TEST1) == 3 |
| 58 | + assert part_1(TEST2) == 0 |
| 59 | + assert part_1(TEST3) == 2 |
| 60 | + assert part_1(TEST4) == 3 |
| 61 | + |
| 62 | + inputs = my_aocd.get_input(2017, 11, 1) |
| 63 | + result1 = part_1(inputs) |
| 64 | + print(f"Part 1: {result1}") |
| 65 | + result2 = part_2(inputs) |
| 66 | + print(f"Part 2: {result2}") |
| 67 | + my_aocd.check_results(puzzle, result1, result2) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments