-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15B.py
32 lines (31 loc) · 1.12 KB
/
15B.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
import sys
arr, moves = sys.stdin.read().split("\n\n")
moves = moves.replace("\n", "")
arr = arr.replace("#", "##").replace(".", "..").replace("O", "[]").replace("@", "@.")
grid = [list(i) for i in arr.splitlines()]
n, m = len(grid), len(grid[0])
for i in range(1, n - 1):
for j in range(1, m - 1):
if grid[i][j] == "@":
x, y = i, j
break
d = {"<": (0, -1), ">": (0, 1), "^": (-1, 0), "v": (1, 0)}
for move in moves:
dx, dy = d[move]
to_move = [(x, y)]
flag = True
for i, j in to_move:
nx, ny = i + dx, j + dy
if (nx, ny) not in to_move:
if grid[nx][ny] == "#":
flag = False
break
elif grid[nx][ny] == "[":
to_move.extend([(nx, ny), (nx, ny + 1)])
if grid[nx][ny] == "]":
to_move.extend([(nx, ny), (nx, ny - 1)])
if flag:
for i, j in to_move[::-1]:
grid[i + dx][j + dy], grid[i][j] = grid[i][j], grid[i + dx][j + dy]
x, y = x + dx, y + dy
print(sum(100 * i + j if grid[i][j] == "[" else 0 for i in range(1, n - 1) for j in range(1, m - 1)))