Advertisement
hugseverycat

AoC Day 24

Dec 24th, 2020 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. import re
  2. my_regex = "((?<!n|s)e)|((?<!n|s)w)|(se)|(nw)|(sw)|(ne)"
  3.  
  4. flip_list = []
  5. tile_map = {}
  6.  
  7. input_filename = "day24.txt"
  8. # input_filename = "sample_input.txt"
  9. with open(input_filename, 'r') as reader:
  10.     for line in reader:
  11.         temp = str(line.rstrip())
  12.         flip_list.append(["".join(d) for d in re.findall(my_regex, temp)])
  13.  
  14.  
  15. def hex_step(location, direction):
  16.     # Function to take a step in the given direction on the hexagonal grid
  17.     x = location[0]
  18.     y = location[1]
  19.     z = location[2]
  20.     if direction == 'w':
  21.         return x - 1, y + 1, z
  22.     elif direction == 'e':
  23.         return x + 1, y - 1, z
  24.     elif direction == 'nw':
  25.         return x, y + 1, z - 1
  26.     elif direction == 'ne':
  27.         return x + 1, y, z - 1
  28.     elif direction == 'se':
  29.         return x, y - 1, z + 1
  30.     elif direction == 'sw':
  31.         return x - 1, y, z + 1
  32.     else:
  33.         print(f"Unknown direction: {direction}. Location = {location}")
  34.         return location
  35.  
  36.  
  37. def neighbor_coord_list(coords: tuple):
  38.     # A function to get the coordinates of all tiles next to the tile at coords
  39.     # Using an x, y, z hexagonal coordinate grid
  40.  
  41.     tx = coords[0]
  42.     ty = coords[1]
  43.     tz = coords[2]
  44.     coord_list = [(tx - 1, ty + 1, tz), (tx + 1, ty - 1, tz), (tx, ty + 1, tz - 1),
  45.                   (tx + 1, ty, tz - 1), (tx, ty - 1, tz + 1), (tx - 1, ty, tz + 1)]
  46.     return coord_list
  47.  
  48.  
  49. def update_map(c_dict: dict):
  50.     # One round of Game of Life
  51.     # Takes a dictionary of tile coordinates, counts neighbors, and updates tiles
  52.     neighbor_count = {}
  53.  
  54.     # Find each black tile, and increment a neighbor counter for each of its neighbors
  55.     for this_coord in c_dict:
  56.         if c_dict[this_coord] == 'black':
  57.             neighbor_list = neighbor_coord_list(this_coord)
  58.             for this_neighbor in neighbor_list:
  59.                 try:
  60.                     neighbor_count[this_neighbor] += 1
  61.                 except KeyError:
  62.                     neighbor_count[this_neighbor] = 1
  63.  
  64.     # n_map is the new c_dict that we will build
  65.     n_map = {}
  66.     active_count = 0
  67.  
  68.     # Go through each tile with a black neighbor tile and flip it if necessary
  69.     for this_coord in neighbor_count:
  70.         try:
  71.             current_state = c_dict[this_coord]
  72.         except KeyError:
  73.             current_state = 'white'
  74.         if current_state == 'black' and neighbor_count[this_coord] not in [1, 2]:
  75.             n_map[this_coord] = 'white'
  76.         elif current_state == 'white' and neighbor_count[this_coord] == 2:
  77.             n_map[this_coord] = 'black'
  78.         else:
  79.             n_map[this_coord] = current_state
  80.  
  81.         # Increment the counter if this tile is black after all the flipping
  82.         if n_map[this_coord] == 'black':
  83.             active_count += 1
  84.  
  85.     return n_map, active_count
  86.  
  87.  
  88. for this_flipped_tile in flip_list:
  89.     # Starting at the origin, step through each instruction and flip the tile
  90.     loc = (0, 0, 0)
  91.     for this_step in this_flipped_tile:
  92.         loc = hex_step(loc, this_step)
  93.     try:
  94.         if tile_map[loc] == "black":
  95.             tile_map[loc] = "white"
  96.         else:
  97.             tile_map[loc] = "black"
  98.     except KeyError:
  99.         tile_map[loc] = "black"
  100.  
  101. # Count the black tiles for part 1
  102. p1counter = 0
  103. for this_tile in tile_map:
  104.     if tile_map[this_tile] == "black":
  105.         p1counter += 1
  106.  
  107. # Game of life for part 2
  108. for _ in range(100):
  109.     tile_map, p2counter = update_map(tile_map)
  110.  
  111. print(f"Part 1: {p1counter}")
  112. print(f"Part 2: {p2counter}")
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement