-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22-01.py
127 lines (125 loc) · 4.45 KB
/
22-01.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
import re
from bisect import bisect_left
raw_rows = []
max_row_size = 0
while True:
inputval = input()
if inputval == "": break
raw_rows.append(inputval)
max_row_size = max(max_row_size, len(inputval))
row_bounds = []
col_bounds = []
row_obs = []
col_obs = []
row, col = None, None
for i in range(max_row_size):
col_bounds.append([])
col_obs.append([])
for i in range(len(raw_rows)):
row_obs.append([])
for j in range(len(raw_rows[i])):
if raw_rows[i][j] == "." and row is None:
row, col = i, j
if raw_rows[i][j] != " " and len(row_bounds) == i:
row_bounds.append([j, len(raw_rows[i]) - 1])
if raw_rows[i][j] != " " and len(col_bounds[j]) == 0:
col_bounds[j].append(i)
if raw_rows[i][j] == " " and len(col_bounds[j]) == 1:
col_bounds[j].append(i - 1)
if raw_rows[i][j] == "#":
row_obs[i].append(j)
col_obs[j].append(i)
for j in range(len(raw_rows[i]), max_row_size):
if len(col_bounds[j]) == 1:
col_bounds[j].append(i - 1)
for j in range(max_row_size):
if len(col_bounds[j]) == 1:
col_bounds[j].append(len(raw_rows) - 1)
direction = 0
raw_moves = input()
moves = list(map(lambda x: int(x), re.findall(r"\d+", raw_moves)))
rotations = re.findall(r"L|R", raw_moves)
for i in range(len(moves)):
if direction == 0:
# Right
left_bound, right_bound = row_bounds[row]
if len(row_obs[row]) > 0:
newCol = bisect_left(row_obs[row], col)
if len(row_obs[row]) == newCol:
newCol = 0
move_pos = row_obs[row][newCol] - 1
rel_move_pos = move_pos
if move_pos < col:
rel_move_pos += right_bound - left_bound + 1
actual_moves = min(moves[i], rel_move_pos - col)
col += actual_moves
if col > right_bound:
col -= right_bound - left_bound + 1
else:
col += moves[i]
while col > right_bound:
col -= right_bound - left_bound + 1
elif direction == 1:
# down
top_bound, bottom_bound = col_bounds[col]
if len(col_obs[col]) > 0:
newRow = bisect_left(col_obs[col], row)
if len(col_obs[col]) == newRow:
newRow = 0
move_pos = col_obs[col][newRow] - 1
rel_move_pos = move_pos
if move_pos < row:
rel_move_pos += bottom_bound - top_bound + 1
actual_moves = min(moves[i], rel_move_pos - row)
row += actual_moves
if row > bottom_bound:
row -= bottom_bound - top_bound + 1
else:
row += moves[i]
while row > bottom_bound:
row -= bottom_bound - top_bound + 1
elif direction == 2:
# left
left_bound, right_bound = row_bounds[row]
if len(row_obs[row]) > 0:
newCol = bisect_left(row_obs[row], col)
if newCol == 0:
newCol = len(row_obs[row])
move_pos = row_obs[row][newCol - 1] + 1
rel_move_pos = move_pos
if move_pos > col:
rel_move_pos -= right_bound - left_bound + 1
actual_moves = min(moves[i], col - rel_move_pos)
col -= actual_moves
if col < left_bound:
col += right_bound - left_bound + 1
else:
col -= moves[i]
while col < left_bound:
col += right_bound - left_bound + 1
elif direction == 3:
# up
top_bound, bottom_bound = col_bounds[col]
if len(col_obs[col]) > 0:
newRow = bisect_left(col_obs[col], row)
if newRow == 0:
newRow = len(col_obs[col])
move_pos = col_obs[col][newRow - 1] + 1
rel_move_pos = move_pos
if move_pos > row:
rel_move_pos -= bottom_bound - top_bound + 1
actual_moves = min(moves[i], row - rel_move_pos)
row -= actual_moves
if row < top_bound:
row += bottom_bound - top_bound + 1
else:
row += moves[i]
while row < top_bound:
row += bottom_bound - top_bound + 1
if i < len(rotations):
if rotations[i] == "R":
direction = (direction + 1) % 4
else:
direction = (direction + 3) % 4
# print(row, col, direction)
print((row + 1) * 1000 + (col + 1) * 4 + direction)