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