-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
48 lines (35 loc) · 1.25 KB
/
part2.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
import typing as t
from pathlib import Path
# Reference: https://www.redblobgames.com/grids/hexagons/
class Cube(t.NamedTuple):
q: int
r: int
s: int
directions = Path(Path(__file__).parent, "input").read_text().split(",")
def get_neighbor(
hex: Cube, direction: t.Literal["n", "s", "ne", "se", "nw", "sw"]
) -> Cube:
match direction:
case "n":
return Cube(hex.q, hex.r - 1, hex.s + 1)
case "s":
return Cube(hex.q, hex.r + 1, hex.s - 1)
case "ne":
return Cube(hex.q + 1, hex.r - 1, hex.s)
case "se":
return Cube(hex.q + 1, hex.r, hex.s - 1)
case "nw":
return Cube(hex.q - 1, hex.r, hex.s + 1)
case "sw":
return Cube(hex.q - 1, hex.r + 1, hex.s)
case _:
raise ValueError("Unknown direction")
def hex_distance(hex1: Cube, hex2: Cube) -> int:
vec = Cube(hex1.q - hex2.q, hex1.r - hex2.r, hex1.s - hex2.s)
return (abs(vec.q) + abs(vec.r) + abs(vec.s)) // 2
position = Cube(0, 0, 0)
max_distance = 0
for direction in directions:
position = get_neighbor(position, direction) # type: ignore
max_distance = max(max_distance, hex_distance(Cube(0, 0, 0), position))
print(f"Result: {max_distance}")