Advertisement
HexTree

Advent of Code 2022 Day 22

Dec 22nd, 2022
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | Source Code | 0 0
  1. grid = []
  2.  
  3. with open('input', 'r') as f:
  4.     for line in f.readlines():
  5.         if not line.strip():
  6.             continue
  7.         if '2' in line:
  8.             orders = line.strip()
  9.             break
  10.         grid.append(list(line[:-1]))
  11.  
  12. # for row in grid:
  13. #     print(row)
  14. # print(orders)
  15.  
  16. width = len(grid[0])
  17. height = len(grid)
  18.  
  19. for row in grid:
  20.     if len(row) < width:
  21.         for _ in range(width - len(row)):
  22.             row.append(' ')
  23.  
  24. grid_copy = [[char for char in row] for row in grid]
  25.  
  26. i = 0
  27. j = grid[0].index('.')
  28. position = (i, j)
  29. direction = (0, 1)
  30. print("starting at", position, direction)
  31.  
  32. def get_next1(position, direction):
  33.     # torus
  34.     next_tile = ((position[0] + direction[0]) % height, (position[1] + direction[1]) % width)
  35.     while grid[next_tile[0]][next_tile[1]] == ' ':
  36.         next_tile = ((next_tile[0] + direction[0]) % height, (next_tile[1] + direction[1]) % width)
  37.     if grid[next_tile[0]][next_tile[1]] == '.':
  38.         return next_tile, direction
  39.     return position, direction
  40.  
  41. wrap_map = {}
  42. # wrap 1
  43. e1 = [(0+k, 50) for k in range(50)]
  44. e2 = [(149-k, 0) for k in range(50)]
  45. for k in range(50):
  46.     wrap_map[(e1[k], (0, -1))] = (e2[k], (0, 1))
  47.     wrap_map[(e2[k], (0, -1))] = (e1[k], (0, 1))
  48. # wrap 2
  49. e1 = [(50+k, 50) for k in range(50)]
  50. e2 = [(100, 0+k) for k in range(50)]
  51. for k in range(50):
  52.     wrap_map[(e1[k], (0, -1))] = (e2[k], (1, 0))
  53.     wrap_map[(e2[k], (-1, 0))] = (e1[k], (0, 1))
  54. # wrap 3
  55. e1 = [(0, 50+k) for k in range(50)]
  56. e2 = [(150+k, 0) for k in range(50)]
  57. for k in range(50):
  58.     wrap_map[(e1[k], (-1, 0))] = (e2[k], (0, 1))
  59.     wrap_map[(e2[k], (0, -1))] = (e1[k], (1, 0))
  60. # wrap 4
  61. e1 = [(49, 100+k) for k in range(50)]
  62. e2 = [(50+k, 99) for k in range(50)]
  63. for k in range(50):
  64.     wrap_map[(e1[k], (1, 0))] = (e2[k], (0, -1))
  65.     wrap_map[(e2[k], (0, 1))] = (e1[k], (-1, 0))
  66. # wrap 5
  67. e1 = [(149, 50+k) for k in range(50)]
  68. e2 = [(150+k, 49) for k in range(50)]
  69. for k in range(50):
  70.     wrap_map[(e1[k], (1, 0))] = (e2[k], (0, -1))
  71.     wrap_map[(e2[k], (0, 1))] = (e1[k], (-1, 0))
  72. # wrap 6
  73. e1 = [(0+k, 149) for k in range(50)]
  74. e2 = [(149-k, 99) for k in range(50)]
  75. for k in range(50):
  76.     wrap_map[(e1[k], (0, 1))] = (e2[k], (0, -1))
  77.     wrap_map[(e2[k], (0, 1))] = (e1[k], (0, -1))
  78. # wrap 7
  79. e1 = [(0, 100+k) for k in range(50)]
  80. e2 = [(199, 0+k) for k in range(50)]
  81. for k in range(50):
  82.     wrap_map[(e1[k], (-1, 0))] = (e2[k], (-1, 0))
  83.     wrap_map[(e2[k], (1, 0))] = (e1[k], (1, 0))
  84.  
  85.  
  86. def get_next2(position, direction):
  87.     # cube
  88.     if (position, direction) in wrap_map:
  89.         next_tile, next_direction = wrap_map[(position, direction)]
  90.     else:
  91.         next_tile = ((position[0] + direction[0]), (position[1] + direction[1]))
  92.         next_direction = direction
  93.     if grid[next_tile[0]][next_tile[1]] == '.':
  94.         return next_tile, next_direction
  95.     return position, direction
  96.  
  97.  
  98. it = 0
  99. while it < len(orders):
  100.     if orders[it].isdigit():
  101.         newit = it
  102.         while newit < len(orders) and orders[newit].isdigit():
  103.             newit += 1
  104.         num = int(orders[it:newit])
  105.         it = newit
  106.         # move forward num
  107.         # print("move forward", num)
  108.         for _ in range(num):
  109.             position, direction = get_next2(position, direction)
  110.             grid_copy[position[0]][position[1]] = {(1, 0): 'v', (0, -1): '<', (-1, 0): '^', (0, 1): '>'}[direction]
  111.             # print(position, direction)
  112.     else:
  113.         turn = orders[it]
  114.         it += 1
  115.         # turn
  116.         # print("turn", turn)
  117.         if turn == 'L':
  118.             direction = {(1, 0): (0, 1), (0, 1): (-1, 0), (-1, 0): (0, -1), (0, -1): (1, 0)}[direction]
  119.         else:
  120.             direction = {(1, 0): (0, -1), (0, -1): (-1, 0), (-1, 0): (0, 1), (0, 1): (1, 0)}[direction]
  121.         grid_copy[position[0]][position[1]] = {(1, 0): 'v', (0, -1): '<', (-1, 0): '^', (0, 1): '>'}[direction]
  122.         # print(position, direction)
  123.  
  124. row = position[0]+1
  125. column = position[1]+1
  126. facing = {(1, 0): 1, (0, -1): 2, (-1, 0): 3, (0, 1): 0}[direction]
  127.  
  128. print(1000*row + 4*column + facing)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement