Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day24_2():
- import re
- from collections import defaultdict
- with open("input24.txt", "r") as file:
- data = file.readlines()
- floor = defaultdict(lambda: False)
- for line in data:
- coords = re.findall(r"e|se|sw|w|nw|ne", line)
- ns = coords.count("se") + coords.count("sw") - coords.count("ne") - coords.count("nw")
- we = coords.count("e") + coords.count("ne") - coords.count("w") - coords.count("sw")
- floor[(ns, we)] = not floor[(ns, we)]
- for _ in range(100):
- new_floor = defaultdict(lambda: False)
- # Add the outer ring of the current floor for consideration.
- for k, v in floor.items():
- for o in [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]:
- tile = (k[0] + o[0], k[1] + o[1])
- if tile not in new_floor:
- new_floor[tile] = floor.get(tile, False)
- for k, v in new_floor.items():
- 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)]])
- if v:
- if not neighbors or neighbors > 2:
- new_floor[k] = False
- else:
- if neighbors == 2:
- new_floor[k] = True
- floor = new_floor
- return sum([v for v in floor.values()])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement