Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grid = []
- with open('input', 'r') as f:
- for line in f.readlines():
- if not line.strip():
- continue
- if '2' in line:
- orders = line.strip()
- break
- grid.append(list(line[:-1]))
- # for row in grid:
- # print(row)
- # print(orders)
- width = len(grid[0])
- height = len(grid)
- for row in grid:
- if len(row) < width:
- for _ in range(width - len(row)):
- row.append(' ')
- grid_copy = [[char for char in row] for row in grid]
- i = 0
- j = grid[0].index('.')
- position = (i, j)
- direction = (0, 1)
- print("starting at", position, direction)
- def get_next1(position, direction):
- # torus
- next_tile = ((position[0] + direction[0]) % height, (position[1] + direction[1]) % width)
- while grid[next_tile[0]][next_tile[1]] == ' ':
- next_tile = ((next_tile[0] + direction[0]) % height, (next_tile[1] + direction[1]) % width)
- if grid[next_tile[0]][next_tile[1]] == '.':
- return next_tile, direction
- return position, direction
- wrap_map = {}
- # wrap 1
- e1 = [(0+k, 50) for k in range(50)]
- e2 = [(149-k, 0) for k in range(50)]
- for k in range(50):
- wrap_map[(e1[k], (0, -1))] = (e2[k], (0, 1))
- wrap_map[(e2[k], (0, -1))] = (e1[k], (0, 1))
- # wrap 2
- e1 = [(50+k, 50) for k in range(50)]
- e2 = [(100, 0+k) for k in range(50)]
- for k in range(50):
- wrap_map[(e1[k], (0, -1))] = (e2[k], (1, 0))
- wrap_map[(e2[k], (-1, 0))] = (e1[k], (0, 1))
- # wrap 3
- e1 = [(0, 50+k) for k in range(50)]
- e2 = [(150+k, 0) for k in range(50)]
- for k in range(50):
- wrap_map[(e1[k], (-1, 0))] = (e2[k], (0, 1))
- wrap_map[(e2[k], (0, -1))] = (e1[k], (1, 0))
- # wrap 4
- e1 = [(49, 100+k) for k in range(50)]
- e2 = [(50+k, 99) for k in range(50)]
- for k in range(50):
- wrap_map[(e1[k], (1, 0))] = (e2[k], (0, -1))
- wrap_map[(e2[k], (0, 1))] = (e1[k], (-1, 0))
- # wrap 5
- e1 = [(149, 50+k) for k in range(50)]
- e2 = [(150+k, 49) for k in range(50)]
- for k in range(50):
- wrap_map[(e1[k], (1, 0))] = (e2[k], (0, -1))
- wrap_map[(e2[k], (0, 1))] = (e1[k], (-1, 0))
- # wrap 6
- e1 = [(0+k, 149) for k in range(50)]
- e2 = [(149-k, 99) for k in range(50)]
- for k in range(50):
- wrap_map[(e1[k], (0, 1))] = (e2[k], (0, -1))
- wrap_map[(e2[k], (0, 1))] = (e1[k], (0, -1))
- # wrap 7
- e1 = [(0, 100+k) for k in range(50)]
- e2 = [(199, 0+k) for k in range(50)]
- for k in range(50):
- wrap_map[(e1[k], (-1, 0))] = (e2[k], (-1, 0))
- wrap_map[(e2[k], (1, 0))] = (e1[k], (1, 0))
- def get_next2(position, direction):
- # cube
- if (position, direction) in wrap_map:
- next_tile, next_direction = wrap_map[(position, direction)]
- else:
- next_tile = ((position[0] + direction[0]), (position[1] + direction[1]))
- next_direction = direction
- if grid[next_tile[0]][next_tile[1]] == '.':
- return next_tile, next_direction
- return position, direction
- it = 0
- while it < len(orders):
- if orders[it].isdigit():
- newit = it
- while newit < len(orders) and orders[newit].isdigit():
- newit += 1
- num = int(orders[it:newit])
- it = newit
- # move forward num
- # print("move forward", num)
- for _ in range(num):
- position, direction = get_next2(position, direction)
- grid_copy[position[0]][position[1]] = {(1, 0): 'v', (0, -1): '<', (-1, 0): '^', (0, 1): '>'}[direction]
- # print(position, direction)
- else:
- turn = orders[it]
- it += 1
- # turn
- # print("turn", turn)
- if turn == 'L':
- direction = {(1, 0): (0, 1), (0, 1): (-1, 0), (-1, 0): (0, -1), (0, -1): (1, 0)}[direction]
- else:
- direction = {(1, 0): (0, -1), (0, -1): (-1, 0), (-1, 0): (0, 1), (0, 1): (1, 0)}[direction]
- grid_copy[position[0]][position[1]] = {(1, 0): 'v', (0, -1): '<', (-1, 0): '^', (0, 1): '>'}[direction]
- # print(position, direction)
- row = position[0]+1
- column = position[1]+1
- facing = {(1, 0): 1, (0, -1): 2, (-1, 0): 3, (0, 1): 0}[direction]
- print(1000*row + 4*column + facing)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement