Advertisement
Guest User

Untitled

a guest
Dec 17th, 2020
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. def do_part_2():
  2.     coord_dict = {}
  3.     input_filename = "day17.txt"
  4.     #input_filename = "sample_input.txt"  # Un-comment this line to use sample data instead
  5.     y = 0
  6.     z = 0
  7.     w = 0
  8.  
  9.     with open(input_filename, 'r') as reader:
  10.         for line in reader:
  11.             this_line = str(line.rstrip())
  12.             for x, c in enumerate(this_line):
  13.                 coord_dict[(x, y, z, w)] = c
  14.             y += 1
  15.  
  16.     def neighbor_coord_list(coords: tuple):
  17.         tx = coords[0]
  18.         ty = coords[1]
  19.         tz = coords[2]
  20.         tw = coords[3]
  21.         coord_list = []
  22.         for nx in range(tx - 1, tx + 2):
  23.             for ny in range(ty - 1, ty + 2):
  24.                 for nz in range(tz - 1, tz + 2):
  25.                     for nw in range(tw - 1, tw + 2):
  26.                         coord_list.append((nx, ny, nz, nw))
  27.         coord_list.remove((tx, ty, tz, tw))
  28.         return coord_list
  29.  
  30.     def update_map(c_dict: dict):
  31.         neighbor_count = {}
  32.  
  33.         # Find each active coordinate, and update its neighbors
  34.         for this_coord in c_dict:
  35.             if c_dict[this_coord] == '#':
  36.                 neighbor_list = neighbor_coord_list(this_coord)
  37.                 for this_neighbor in neighbor_list:
  38.                     # If this neighbor hasn't been added, add it
  39.                     try:
  40.                         neighbor_count[this_neighbor] += 1
  41.                     except KeyError:
  42.                         neighbor_count[this_neighbor] = 1
  43.  
  44.         n_map = {}
  45.         active_count = 0
  46.  
  47.         # Go through each coord with active neighbors and see if it needs to change its state
  48.         for this_coord in neighbor_count:
  49.             # If the coord is newly viewed, then its initial state must be inactive.
  50.             try:
  51.                 current_state = c_dict[this_coord]
  52.             except KeyError:
  53.                 current_state = '.'
  54.  
  55.             # Active coordinates stay active only if they have 2 or 3 active neighbors
  56.             if current_state == '#' and not (2 <= neighbor_count[this_coord] <= 3):
  57.                 n_map[this_coord] = '.'
  58.             # Inactive coordinates become active if they have exactly 3 active neighbors
  59.             elif current_state == '.' and neighbor_count[this_coord] == 3:
  60.                 n_map[this_coord] = '#'
  61.             # Otherwise, their state remains unchanged
  62.             else:
  63.                 n_map[this_coord] = current_state
  64.  
  65.             # Up the count if this coordinate is active
  66.             if n_map[this_coord] == '#':
  67.                 active_count += 1
  68.  
  69.         return n_map, active_count
  70.  
  71.     for i in range(6):
  72.         coord_dict, counter = update_map(coord_dict)
  73.     print(counter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement