-
Notifications
You must be signed in to change notification settings - Fork 0
/
09.py
91 lines (72 loc) · 2.31 KB
/
09.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import annotations
from dataclasses import dataclass
from typing import List, Tuple
with open("input/09.txt") as f:
lines = f.read().split("\n")
@dataclass
class Move:
def __init__(self, line: str):
items = line.split()
self.direction = items[0]
self.distance = int(items[1])
@dataclass
class Point:
x: int
y: int
def move(self, direction: str):
if direction == "U":
self.x += 1
elif direction == "R":
self.y += 1
elif direction == "D":
self.x -= 1
elif direction == "L":
self.y -= 1
else:
raise ValueError("Undefined direction")
def distance(self, other: Point):
x_distance = abs(self.x - other.x)
y_distance = abs(self.y - other.y)
return max(x_distance, y_distance)
def is_far_from(self, other: Point):
return self.distance(other) > 1
def follow(self, other: Point, movement: Move):
if other.is_far_from(self):
if movement.direction in ["D", "U"]:
self.y = other.y
if movement.direction in ["R", "L"]:
self.x = other.x
self.move(direction=movement.direction)
def to_tuple(self) -> Tuple[int, int]:
return self.x, self.y
moves: List[Move] = [Move(line) for line in lines]
tail_positions: List[Tuple[int, int]] = []
H = Point(0, 0)
T = Point(0, 0)
tail_positions.append((T.x, T.y))
for move in moves:
for _ in range(move.distance):
H.move(move.direction)
T.follow(H, movement=move)
tail_positions.append((T.x, T.y))
# Part A
print(len(set(tail_positions)))
# Part B
tail_positions.clear()
Rope = [Point(0, 0) for _ in range(10)]
rope_length = len(Rope) - 1
H = Rope[0]
Tail = Rope[-1]
for move in moves:
# print(f"Moving to {move.direction} for {move.distance}:")
for move_index in range(move.distance):
H.move(direction=move.direction)
for index in range(rope_length):
B = Rope[index]
T = Rope[index + 1]
T.follow(B, movement=move)
# print(f"\tMove {move.direction} completed ({move_index + 1}/{move.distance}).")
if (tail_pos := Tail.to_tuple()) not in tail_positions:
tail_positions.append(tail_pos)
# print("-"*40)
print(tail_positions)