Advertisement
HexTree

Advent of Code 2022 Day 23

Dec 23rd, 2022
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | Source Code | 0 0
  1. from collections import Counter
  2.  
  3. grid = set()
  4.  
  5. directions = [(((-1, 0), (-1, 1), (-1, -1)), (-1, 0)),
  6.               (((1, 0), (1, 1), (1, -1)), (1, 0)),
  7.               (((0,-1), (-1, -1), (1, -1)), (0, -1)),
  8.               (((0, 1), (-1, 1), (1, 1)), (0, 1))]
  9. compass = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
  10.  
  11. with open('input', 'r') as f:
  12.     for i, line in enumerate(f.readlines()):
  13.         for j, char in enumerate(line.strip()):
  14.             if char == '#':
  15.                 grid.add((i, j))
  16.  
  17. num_moved = 1
  18. r = 0
  19. while num_moved:
  20.     # first half
  21.     proposed_destinations = {}
  22.     for elf_position in grid:
  23.         if not any((elf_position[0] + c[0], elf_position[1] + c[1]) in grid for c in compass):
  24.             continue
  25.         for it in range(4):
  26.             check_directions, proposed_direction = directions[(r + it)%4]
  27.             if not any((d[0] + elf_position[0], d[1] + elf_position[1]) in grid for d in check_directions):
  28.                 proposed_destinations[elf_position] = (elf_position[0] + proposed_direction[0], elf_position[1] + proposed_direction[1])
  29.                 break
  30.     num_moved = 0
  31.     # second half
  32.     proposed_destinations_counter = Counter(proposed_destinations.values())
  33.     for elf_position, proposed_destination in proposed_destinations.items():
  34.         if proposed_destinations_counter[proposed_destination] == 1:
  35.             grid.remove(elf_position)
  36.             grid.add(proposed_destination)
  37.             num_moved += 1
  38.     print(r, num_moved)
  39.     r += 1
  40.     # # uncomment for part 1
  41.     # if r == 10:
  42.     #     break
  43.  
  44.  
  45. # def get_grid(g):
  46. #     for elf in g:
  47. #         min_i, max_i = elf[0], elf[0]
  48. #         min_j, max_j = elf[1], elf[1]
  49. #         break
  50. #     for elf in g:
  51. #         min_i, max_i = min(min_i, elf[0]), max(max_i, elf[0])
  52. #         min_j, max_j = min(min_j, elf[1]), max(max_j, elf[1])
  53.  
  54.  
  55. for elf in grid:
  56.     min_i, max_i = elf[0], elf[0]
  57.     min_j, max_j = elf[1], elf[1]
  58.     break
  59. for elf in grid:
  60.     min_i, max_i = min(min_i, elf[0]), max(max_i, elf[0])
  61.     min_j, max_j = min(min_j, elf[1]), max(max_j, elf[1])
  62.  
  63. height = max_i - min_i + 1
  64. width = max_j - min_j + 1
  65. empty_spaces = width * height - len(grid)
  66. print("empty spaces:", empty_spaces)
  67. print("round number:", r)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement