-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
154 lines (143 loc) · 4.52 KB
/
day9.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
import os
from pathlib import Path
the_day = 9
download_data_path = Path("/Users/relyea/Downloads/input.txt")
local_data_path = "/Users/relyea/code/advent_of_code_2022/input"+str(the_day)+".txt"
if download_data_path.exists():
download_data_path.rename(local_data_path)
with open(local_data_path) as input_file:
inpstring = input_file.readlines()
data = [
line.strip()
for line in inpstring
]
# data = open(local_data_path).read().strip().split("\n")
def move_both(movedir, head, tail):
if movedir == 'U':
head[1] += 1
if head[0] == tail[0]:
if head[1] == tail[1] or head[1] == tail[1]+1:
pass
else:
tail[1] = head[1]-1
elif head[0] == tail[0] - 1 or head[0] == tail[0] + 1:
if head[1] == tail[1] or head[1] == tail[1]+1:
pass
else:
tail[1] = head[1]-1
tail[0] = head[0]
if movedir == 'D':
head[1] -= 1
if head[0] == tail[0]:
if head[1] == tail[1] or head[1] == tail[1]-1:
pass
else:
tail[1] = head[1]+1
elif head[0] == tail[0] - 1 or head[0] == tail[0] + 1:
if head[1] == tail[1] or head[1] == tail[1]-1:
pass
else:
tail[1] = head[1]+1
tail[0] = head[0]
if movedir == 'R':
head[0] += 1
if head[1] == tail[1]:
if head[0] == tail[0] or head[0] == tail[0]+1:
pass
else:
tail[0] = head[0]-1
elif head[1] == tail[1] - 1 or head[1] == tail[1] + 1:
if head[0] == tail[0] or head[0] == tail[0]+1:
pass
else:
tail[0] = head[0]-1
tail[1] = head[1]
if movedir == 'L':
head[0] -= 1
if head[1] == tail[1]:
if head[0] == tail[0] or head[0] == tail[0]-1:
pass
else:
tail[0] = head[0]+1
elif head[1] == tail[1] - 1 or head[1] == tail[1] + 1:
if head[0] == tail[0] or head[0] == tail[0]-1:
pass
else:
tail[0] = head[0]+1
tail[1] = head[1]
return head, tail
head = [0,0]
tail = [0,0]
tailset = set()
tailset.add(tuple(tail))
for move in data:
movenum = int(move.split(" ")[1])
movedir = move.split(" ")[0]
for imove in range(movenum):
head, tail = move_both(movedir, head, tail)
tailset.add(tuple(tail))
print(len(tailset))
# PART TWO
def move_relative(oldhead, newhead, oldtail):
tail = oldtail.copy()
if tail[1] == newhead[1]-2:
tail[1] = newhead[1]-1
if tail[0] == newhead[0] - 2:
tail[0] = newhead[0]-1
elif tail[0] == newhead[0] + 2:
tail[0] = newhead[0]+1
else:
tail[0] = newhead[0]
elif tail[1] == newhead[1]+2:
tail[1] = newhead[1]+1
if tail[0] == newhead[0] - 2:
tail[0] = newhead[0]-1
elif tail[0] == newhead[0] + 2:
tail[0] = newhead[0]+1
else:
tail[0] = newhead[0]
elif tail[0] == newhead[0]-2:
tail[0] = newhead[0]-1
if tail[1] == newhead[1] - 2:
tail[1] = newhead[1]-1
elif tail[1] == newhead[1] + 2:
tail[1] = newhead[1]+1
else:
tail[1] = newhead[1]
elif tail[0] == newhead[0]+2:
tail[0] = newhead[0]+1
if tail[1] == newhead[1] - 2:
tail[1] = newhead[1]-1
elif tail[1] == newhead[1] + 2:
tail[1] = newhead[1]+1
else:
tail[1] = newhead[1]
return tail
n_chains = 10
chain = dict()
for link in range(n_chains):
chain[link] = [0,0]
tailset = set()
tailset.add(tuple(chain[8]))
for move in data:
movenum = int(move.split(" ")[1])
movedir = move.split(" ")[0]
print(movenum, movedir)
for imove in range(movenum):
for link in range(n_chains):
if link == 0:
oldpos = chain[0].copy()
if movedir=='U':
chain[link][1]+=1
if movedir=='D':
chain[link][1]-=1
if movedir=='L':
chain[link][0]-=1
if movedir=='R':
chain[link][0]+=1
else:
tail = move_relative(oldpos, chain[link-1], chain[link])
oldpos = chain[link].copy()
chain[link] = tail
tailset.add(tuple(chain[n_chains-1]))
print(len(tailset))