-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-02-efficient.py
73 lines (73 loc) · 2.43 KB
/
06-02-efficient.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
import bisect
inputval = ""
columns = []
rows = []
while True:
inputval = input()
if not inputval:
break
rows.append([])
for i, cell in enumerate(inputval):
if i >= len(columns):
columns.append([])
if cell == "#":
rows[-1].append(i)
columns[i].append(len(rows) - 1)
elif cell == "^":
start_row = len(rows) - 1
start_col = i
visited_cells = set()
cur_row = start_row
cur_col = start_col
dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)]
cur_dir = 0
while cur_row >= 0 and cur_row < len(rows) and cur_col >= 0 and cur_col < len(columns):
visited_cells.add((cur_row, cur_col))
new_row = cur_row + dirs[cur_dir][0]
new_col = cur_col + dirs[cur_dir][1]
if new_row >= 0 and new_row < len(rows) and new_col >= 0 and new_col < len(columns) and new_col in rows[new_row]:
cur_dir = (cur_dir + 1) % 4
continue
cur_row = new_row
cur_col = new_col
print("P1: ", len(visited_cells))
total = 0
def validate_loop():
cur_row = start_row
cur_col = start_col
cur_dir = 0
visited = set()
while (cur_row, cur_col, cur_dir) not in visited:
visited.add((cur_row, cur_col, cur_dir))
if cur_dir == 0 or cur_dir == 2:
new_row = bisect.bisect(columns[cur_col], cur_row)
if cur_dir == 0:
new_row -= 1
if new_row < 0 or new_row >= len(columns[cur_col]):
return False # Escape
if cur_dir == 0:
cur_row = columns[cur_col][new_row] + 1
else:
cur_row = columns[cur_col][new_row] - 1
else:
new_col = bisect.bisect(rows[cur_row], cur_col)
if cur_dir == 3:
new_col -= 1
if new_col < 0 or new_col >= len(rows[cur_row]):
return False # Escape
if cur_dir == 1:
cur_col = rows[cur_row][new_col] - 1
else:
cur_col = rows[cur_row][new_col] + 1
cur_dir = (cur_dir + 1) % 4
return True
for obs_row, obs_col in visited_cells:
if obs_row == start_row and obs_col == start_col:
continue
bisect.insort(rows[obs_row], obs_col)
bisect.insort(columns[obs_col], obs_row)
if validate_loop():
total += 1
del rows[obs_row][bisect.bisect_left(rows[obs_row], obs_col)]
del columns[obs_col][bisect.bisect_left(columns[obs_col], obs_row)]
print("P2: ", total)