-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-9.py
154 lines (138 loc) · 5.3 KB
/
day-9.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from common import parse
class Pos():
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self): return f"({self.x}, {self.y})"
def next(self, direction):
match(direction):
case "U":
return Pos(self.x, self.y + 1)
case "D":
return Pos(self.x, self.y - 1)
case "R":
return Pos(self.x + 1, self.y)
case "L":
return Pos(self.x - 1, self.y)
def distance(self, other):
return abs(self.x - other.x) + abs(self.y - other.y) - 1
def touches(self, other):
return max(abs(self.x - other.x), abs(self.y - other.y)) <= 1
def next_position(head: Pos, tails, direction: str, debug=False):
new_head = head.next(direction)
new_tails = [t for t in tails]
in_front = new_head
if debug:
print(f"Head: {new_head} -- {direction}")
last_move = direction
for i in range(len(tails)):
tail = tails[i]
new_tail = tail
if new_tail.touches(in_front):
if debug:
print(f"Tail {i+1} doesn't move")
in_front = new_tail
continue
d = new_tail.distance(in_front)
match(d):
case 1:
if new_tail.x < in_front.x:
last_move = "R"
elif new_tail.x > in_front.x:
last_move = "L"
elif new_tail.y < in_front.y:
last_move = "U"
elif new_tail.y > in_front.y:
last_move = "D"
new_tail = new_tail.next(last_move)
if debug:
print(
f"Tail {i+1} {new_tail} dist to {in_front} = {d} => {last_move}")
case _ if d > 3:
print("fuck")
raise ValueError()
case _ if d > 1:
new_tail = new_tail.next(last_move)
if debug:
print(
f"Tail {i+1} {new_tail} dist to {in_front} = {d} => {last_move}")
if last_move in ["R", "L"]:
if tail.y < in_front.y:
new_tail = new_tail.next("U")
last_move = "U"
else:
new_tail = new_tail.next("D")
last_move = "D"
else:
if tail.x < in_front.x:
new_tail = new_tail.next("R")
last_move = "R"
else:
new_tail = new_tail.next("L")
last_move = "L"
if debug:
print(
f"Tail {i+1} {new_tail} dist to {in_front} = {d} => {last_move}")
in_front = new_tail
new_tails[i] = new_tail
return new_head, new_tails
def perform(filename, nb_of_tails=1, debug=False):
min_x, min_y, max_x, max_y = 0, 0, 0, 0
head = Pos(0, 0)
tails = [Pos(0, 0) for i in range(nb_of_tails)]
tail_positions = {(head.x, head.y)}
for move in parse(filename):
# print(f"=={move}==")
direction, count = move.split(" ")
for i in range(0, int(count)):
head, tails = next_position(head, tails, direction, debug)
tail_positions.add((tails[-1].x, tails[-1].y))
# print(f"New pos: head: {head}, tail: {tail}")
if debug:
print(f"{direction}")
max_x, max_y = max(max_x, head.x, max(t.x for t in tails)), max(
max_y, head.y, max(t.y for t in tails))
min_x, min_y = min(min_x, head.x, min(t.x for t in tails)), min(
min_y, head.y, min(t.y for t in tails))
if debug:
for y in range(min_y, max_y+1)[::-1]:
line_str = ""
for x in range(min_x, max_x+1):
if x == 0 and y == 0:
line_str += "s"
elif head.x == x and head.y == y:
line_str += "H"
else:
flag = False
for i in range(len(tails)):
t = tails[i]
if t.x == x and t.y == y:
flag = True
line_str += f"{i+1}"
break
if not flag:
line_str += "."
print(line_str)
if debug:
print("================================================================")
for y in range(min_y, max_y+1)[::-1]:
line_str = ""
for x in range(min_x, max_x+1):
if x == 0 and y == 0:
line_str += 's'
elif any(t[0] == x and t[1] == y for t in tail_positions):
line_str += "x"
else:
line_str += "."
print(line_str)
return len(tail_positions)
result_test = perform("9-test", 1)
print(result_test)
result_test = perform("9")
print(result_test)
result_test = perform("9-test", 9)
print(result_test)
result_test = perform("9-test2", 9)
print(result_test)
result_test = perform("9", 9)
print(result_test)