|
@@ -0,0 +1,83 @@
|
|
|
+from queue import deque
|
|
|
+from itertools import zip_longest
|
|
|
+
|
|
|
+def transpose(l):
|
|
|
+ return list(zip_longest(*l, fillvalue=None))
|
|
|
+
|
|
|
+def rot_left(grid):
|
|
|
+ return list(reversed(transpose(grid)))
|
|
|
+
|
|
|
+def rot_right(grid):
|
|
|
+ return list(transpose(reversed(grid)))
|
|
|
+
|
|
|
+
|
|
|
+lines = [line[:-1] for line in open("22.input")]
|
|
|
+
|
|
|
+board = [line for line in lines[:-2]]
|
|
|
+max_width = max(len(line) for line in board)
|
|
|
+board = [line + " " * max(0, max_width - len(line)) for line in board]
|
|
|
+
|
|
|
+pw = deque(lines[-1])
|
|
|
+
|
|
|
+def show(grid):
|
|
|
+ for line in grid:
|
|
|
+ for c in line:
|
|
|
+ print(c, end='')
|
|
|
+ print("")
|
|
|
+
|
|
|
+pos = ([x for x, c in enumerate(board[0]) if c == '.'][0], 0)
|
|
|
+facing = 0
|
|
|
+
|
|
|
+num = ""
|
|
|
+
|
|
|
+def walk(steps):
|
|
|
+ global pos
|
|
|
+ for _ in range(steps):
|
|
|
+ x, y = pos
|
|
|
+ new_x = pos[0] + 1
|
|
|
+ if new_x >= len(board[y]) or board[y][new_x] == " ":
|
|
|
+ new_x = [x for x, c in enumerate(board[y]) if c != " "][0]
|
|
|
+ if board[y][new_x] == '#':
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ pos = (new_x, y)
|
|
|
+
|
|
|
+def rotate(rot):
|
|
|
+ global board, pos, facing
|
|
|
+ if rot == "R":
|
|
|
+ board = rot_left(board)
|
|
|
+
|
|
|
+ x, y = pos
|
|
|
+ pos = (y, len(board)-x-1)
|
|
|
+ facing = (facing + 1) % 4
|
|
|
+ elif rot == "L":
|
|
|
+ x, y = pos
|
|
|
+ new_x = len(board) - y - 1
|
|
|
+ board = rot_right(board)
|
|
|
+ pos = (new_x, x)
|
|
|
+ facing = (facing - 1) % 4
|
|
|
+
|
|
|
+while pw:
|
|
|
+ char = pw.popleft()
|
|
|
+ if char == 'R' or char == 'L':
|
|
|
+ steps = int(num)
|
|
|
+
|
|
|
+ walk(steps)
|
|
|
+
|
|
|
+ rotate(char)
|
|
|
+
|
|
|
+ num = ""
|
|
|
+ else:
|
|
|
+ num += char
|
|
|
+
|
|
|
+
|
|
|
+walk(int(num))
|
|
|
+
|
|
|
+final_facing = facing
|
|
|
+
|
|
|
+while facing != 0:
|
|
|
+ rotate("R")
|
|
|
+
|
|
|
+pos = (pos[0] + 1, pos[1] + 1)
|
|
|
+
|
|
|
+print(pos[1] * 1000 + pos[0] * 4 + final_facing)
|