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
|
#!/usr/bin/python3
from typing import NamedTuple, Self
class Vec2(NamedTuple):
x: int
y: int
def __add__(self, other: Self) -> Self:
return Vec2(self.x + other.x, self.y + other.y)
def __sub__(self, other: Self) -> Self:
return Vec2(self.x - other.x, self.y - other.y)
def main() -> None:
with open("input", "r") as f:
data = [line.rstrip().split(',') for line in f.readlines()]
p1 = plot(data[0])
p2 = plot(data[1])
# START PART 1
p1 = set(p1.keys())
p2 = set(p2.keys())
print(min(abs(x) + abs(y) for x, y in p1 & p2))
# END PART 1 START PART 2
print(min(p1[k] + p2[k] for k in p1.keys() if k in p2))
# END PART 2
def plot(data: list[str]) -> dict[Vec2, int]:
t = 0
p = Vec2(0, 0)
s: dict[Vec2, int] = {}
VMAP = {
'U': Vec2(0, +1),
'D': Vec2(0, -1),
'L': Vec2(-1, 0),
'R': Vec2(+1, 0),
}
for spec in data:
d, n = spec[0], int(spec[1:])
v⃗ = VMAP[d]
for _ in range(n):
t += 1
p += v⃗
s[p] = t
return s
if __name__ == "__main__":
main()
|