-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2222.py
312 lines (281 loc) Β· 7.5 KB
/
aoc2222.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import re
def main():
path = '_inputs/2222.0'
res_eg = p1(open('_inputs/2222.1'))#.read()
res = p1(open(path))#.read()
res2 = p2(open(path))
print('Star 1:', res_eg, '(test)')#.read()
print('Star 1:', res)#.read()
print('Star 2:', res2)#.read()
assert res2 == 115311
# 129198 -> hi
"""
b___e___D
| | |
a|___|___|c
| |
a ___|___|c
| | |
b|___|___|D
| |
e|___|D
_ _
|_|_|
|_|
_|_|
|_|
|_|
"""
"""
\ b___e___D
\ | | |
a\|___|___|c
|\ |
a ___|__\|c
| | |\
b|___|___|D\
| |
e|___|D
"""
"""
0...50..100..150
b___e___D .0
| | |
a|___|...|c .50
| |
a ...|___|c .100
| | |
b|___|...|D .150
| |
e|___|D .200
"""
# Dc matching cD
# be matching be
# ab mathcing ba
# eD matching eD
# for dotted-edge ... :
# each matches the same-name adjacent dotted-edge
def p2(file) -> int:
G = []
sep = False
move = ''
W = 0
for line in file: # parse map and get width of field
if line == '\n':
sep = True
else:
if not sep:
line = line[:-1]
G.append(line)
else:
W = max(len(line), W)
move = line
for i in range(len(G)): # fill zero at lines that come short
G[i] = G[i] + ' ' * (W - len(G[i]))
# for line in G: print(line)
c = 0
while G[0][c] != '.': # find starting point
c += 1
r = 0
print('\nstarting at', r, c, '\n')
dr = 0 # current facing: if right, DR increments but DC doesnt
dc = 1
R = len(G)
C = len(G[0])
for val, key in re.findall(r'(\d+)(\D?)', move):
val = int(val)
# part 2
for _ in range(val):
ddr = dr
ddc = dc
rr = r + dr
cc = c + dc
# 14 wrap around cases
"""
0...50..100..150
b___e___D .0
| | |
a|___|...|c .50
| |
a ...|___|c .100
| | |
b|___|...|D .150
| |
e|___|D .200"""
# Case: ba .. ab
# upper ba...lower ab...moving left
if 0 <= rr < 50 and cc == 49 and dc == -1:
dc = 1
rr = 149 - rr
cc = 0
# lower ab...upper ba...moving left
if 100 <= rr < 150 and cc < 0 and dc == -1:
# if 100 <= rr < 150 and cc < 0 and dr == -1:
dc = 1
rr = 149 - rr
cc = 50
# Case: be ... be
# upper be...lower be...moving on Up
if rr < 0 and 50 <= cc < 100 and dr == -1:
dr = 0
dc = 1
rr = cc + 100
cc = 0
# lower be...upper be...moving Left
if cc < 0 and 150 <= rr < 200 and dc == -1:
dr = 1
dc = 0
cc = rr - 100
rr = 0
# Case: eD ... eD
# upper eD...lower eD...moving Up
if rr < 0 and 100 <= cc < 150 and dr == -1:
rr = 199
cc = cc - 100
# lower eD...upper eD...moving Down
if 200 <= rr and 0 <= cc < 50 and dr == 1:
rr = 0
cc += 100
# Case: Dc ... cD
# upper Dc...lower cD...moving Right (dc is 1 to be flipped)
if cc >= 150 and 0 <= rr < 50 and dc == 1:
dc = -1
rr = 149 - rr
cc = 99
# lower cD...upper Dc...moving right (dc 1 to -1)
if cc == 100 and 100 <= rr < 150 and dc == 1:
dc = -1
rr = 149 - rr
cc = 149
# Case: a...
# a dotted-edge 45-clockwise . moving Up
if 0 <= cc < 50 and rr == 99 and dr == -1:
dr = 0
# dc = -1
dc = 1
rr = cc + 50
cc = 50
# a dotted-edge 45-counterclockwise . moving left
if cc == 49 and 50 <= rr <= 100 and dc == -1:
dr = 1
dc = 0
cc = rr - 50
rr = 100
# Case: D...
# D dotted-edge 45-clockwise . moving down
if rr == 150 and 50 <= cc < 100 and dr == 1:
dr = 0
dc = -1
rr = cc + 100
cc = 49
# D dotted-edge 45-counterclockwise . moving right
if cc == 50 and 150 <= rr < 200 and dc == 1:
dr = -1
dc = 0
cc = rr - 100
rr = 149
# Case: c...
# c dotted-edge 45-clockwise . moving down
if rr == 50 and 100 <= cc < 150 and dr == 1:
dr = 0
dc = -1
rr = cc - 50
cc = 99
# c dotted-edge 45-counterclockwise . moving right
if 50 <= rr < 100 and cc == 100 and dc == 1:
dr = -1
dc = 0
cc = rr + 50
rr = 49
if G[rr][cc] == '#':
dr = ddr
dc = ddc
break
r = rr
c = cc
# same as p1
if key == 'R':
dr, dc = dc, -dr
if key == 'L':
dr, dc = -dc, dr
# print('(', dr, dc, ')', val, key)
print('final facing: (', dr, dc, ')\n')
if dr == 0:
if dc == 1:
x = 0
else:
x = 2
else:
if dc == 1:
x = 1
else:
x = 3
res = 1000 * (r + 1) + 4 * (c + 1) + x
return res
print('Star 2: ', res)
def p1(file) -> int:
G = []
sep = False
move = ''
W = 0
for line in file: # parse map and get width of field
if line == '\n':
sep = True
else:
if not sep:
line = line[:-1]
G.append(line)
else:
W = max(len(line), W)
move = line
for i in range(len(G)): # fill zero at lines that come short
G[i] = G[i] + ' ' * (W - len(G[i]))
# for line in G: print(line)
c = 0
while G[0][c] != '.': # find starting point
c += 1
r = 0
print('\nstarting at', r, c, '\n')
dr = 0 # current facing: if right, DR increments but DC doesnt
dc = 1
R = len(G)
C = len(G[0])
for val, key in re.findall(r'(\d+)(\D?)', move):
val = int(val)
for _ in range(val):
rr = r
cc = c
while True:
rr += dr
rr %= R
cc += dc
cc %= C
if G[rr][cc] != ' ':
break
if G[rr][cc] == '#' :
break
r = rr
c = cc
#if key in ['L', 'R']:
# dr, dc = dc, dr"""
if key == 'R':
dr, dc = dc, -dr
if key == 'L':
dr, dc = -dc, dr
# print('(', dr, dc, ')', val, key)
print('final facing: (', dr, dc, ')\n')
if dr == 0:
if dc == 1:
x = 0
else:
x = 2
else:
if dc == 1:
x = 1
else:
x = 3
res = 1000 * (r + 1) + 4 * (c + 1) + x
return res
print('Star 1: ', res)
if __name__ == '__main__':
main()