Advertisement
Guest User

Untitled

a guest
Dec 24th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. def day24_2():
  2.     import re
  3.     from collections import defaultdict
  4.    
  5.     with open("input24.txt", "r") as file:
  6.         data = file.readlines()
  7.        
  8.     floor = defaultdict(lambda: False)
  9.     for line in data:
  10.         coords = re.findall(r"e|se|sw|w|nw|ne", line)
  11.         ns = coords.count("se") + coords.count("sw") - coords.count("ne") - coords.count("nw")
  12.         we = coords.count("e") + coords.count("ne") - coords.count("w") - coords.count("sw")
  13.  
  14.         floor[(ns, we)] = not floor[(ns, we)]
  15.        
  16.     for _ in range(100):
  17.         new_floor = defaultdict(lambda: False)
  18.        
  19.         # Add the outer ring of the current floor for consideration.
  20.         for k, v in floor.items():
  21.             for o in [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]:
  22.                 tile = (k[0] + o[0], k[1] + o[1])
  23.                 if tile not in new_floor:
  24.                     new_floor[tile] = floor.get(tile, False)
  25.        
  26.         for k, v in new_floor.items():
  27.             neighbors = sum([floor[(k[0] + o[0], k[1] + o[1])] for o in [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]])
  28.             if v:
  29.                 if not neighbors or neighbors > 2:
  30.                     new_floor[k] = False
  31.             else:
  32.                 if neighbors == 2:
  33.                     new_floor[k] = True
  34.                    
  35.         floor = new_floor
  36.        
  37.     return sum([v for v in floor.values()])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement