Advertisement
Guest User

Advent of Code Day 18 part 2

a guest
Dec 18th, 2023
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. with open("2023_18input.txt", 'r') as file:
  2.     my_input = file.read()
  3.    
  4. get_way = {
  5.     '1' :   'D',
  6.     '3' :   'U',
  7.     '0' :   'R',
  8.     '2' :   'L',
  9.     }
  10.    
  11. raw_data = my_input
  12. raw_data = raw_data.split('\n')
  13. raw_data = [x.split(" ") for x in raw_data]
  14.  
  15. plan = []
  16. for line in raw_data:
  17.     plan.append((int(line[2].strip("()#")[:-1], 16), get_way[line[2].strip("()#")[-1]]))
  18.    
  19. ways = {
  20.     'U' :   (0, -1),
  21.     'D' :   (0, 1),
  22.     'L' :   (-1, 0),
  23.     'R' :   (1, 0),
  24.     }
  25.    
  26. def add_pos(a, b):
  27.     return tuple([x+y for x, y in zip(a, b)])
  28.  
  29. max_h, max_v, min_h, min_v = 0, 0, 0, 0
  30. pos = (0, 0)
  31.  
  32. inverse = {
  33.     'U' :   'D',
  34.     'D' :   'U',
  35.     'L' :   'R',
  36.     'R' :   'L',
  37.     }
  38.  
  39. for amount, way in plan:
  40.     add_x, add_y = ways[way]
  41.     x, y = pos
  42.     pos = (x + (add_x * amount), y + (add_y * amount))
  43.     if pos[0] > max_h:
  44.         max_h = pos[0]
  45.     if pos[0] < min_h:
  46.         min_h = pos[0]
  47.     if pos[1] > max_v:
  48.         max_v = pos[1]
  49.     if pos[1] < min_v:
  50.         min_v = pos[1]
  51.  
  52. h_correction, v_correction = 0, 0
  53. if min_h < 0:
  54.     h_correction = -min_h
  55. if min_v < 0:
  56.     v_correction = -min_v
  57.  
  58. count = 0
  59.  
  60. grid = {}
  61. multiples = {}
  62.  
  63. # go through the instructions and record where all the vertices are
  64. # also count the number of squares on the border
  65. pos = (h_correction, v_correction)
  66. for i, (amount, way) in enumerate(plan):
  67.     count += amount
  68.     x, y = pos
  69.     if y in grid:
  70.         grid[y].append((x, way + inverse[plan[(i - 1) % len(plan)][1]]))
  71.     else:
  72.         grid[y] = [(x, way + inverse[plan[(i - 1) % len(plan)][1]])]
  73.     add_x, add_y = ways[way]
  74.     pos = add_pos((add_x*amount, add_y*amount), pos)
  75.        
  76. # for blank rows, add one row and record how many times it needs to be counted
  77. rows = list(grid.keys())
  78. rows = sorted(rows)
  79.  
  80. to_add = []
  81. for i, y in enumerate(rows[:-1]):
  82.     if y+1 in rows:
  83.         pass
  84.     else:
  85.         to_add.append(y+1)
  86.         multiples[y+1] = rows[i+1] - (y+1)
  87. for entry in to_add:
  88.     grid[entry] = []
  89.  
  90. rows += to_add
  91. rows = sorted(rows)
  92.  
  93. # fill in the vertical wall segments
  94. for i, y in enumerate(rows[1:], start=1):
  95.     for place, vertex in grid[y]:
  96.         if 'U' in vertex:
  97.             y2 = rows[i - 1]
  98.             while place not in [a for a, b in grid[y2]]:
  99.                 grid[y2].append((place, 'UD'))
  100.                 position = rows.index(y2)
  101.                 y2 = rows[position - 1]
  102. for y in grid:
  103.     grid[y] = sorted(grid[y], key=lambda x: x[0])
  104.  
  105. # go through row by row and count what's between the boundaries
  106. for y in range(1, max_v + v_correction):
  107.     if y in grid:
  108.         row_count = 0
  109.         inside = False
  110.         for i, (place, vertex) in enumerate(grid[y][:-1]):
  111.             if 'U' in vertex:
  112.                 inside = not inside
  113.             if 'R' not in vertex and inside:
  114.                 if y in multiples:
  115.                     multiple = multiples[y]
  116.                 else:
  117.                     multiple = 1
  118.                 row_count += grid[y][i+1][0] - place - 1
  119.         print("%s in row %s, multiplied %s times" % (row_count, y, multiple))
  120.         count += row_count * multiple
  121.  
  122. print(count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement