Created
October 23, 2021 11:20
-
-
Save bennuttall/47a1982c8d71ab5f495e3f15f9e5846e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import product | |
def count_neighbours(x, y): | |
deltas = set(product((-1, 0, 1), repeat=2)) - {(0, 0)} | |
return len({(x + dx, y + dy) for (dx, dy) in deltas} & lights_on) | |
def evolve(lights_on): | |
still_on = {(x, y) for x, y in lights_on if count_neighbours(x, y) in (2, 3)} | |
now_off = set(product(range(SIZE), repeat=2)) - lights_on | |
turn_on = {(x, y) for x, y in now_off if count_neighbours(x, y) == 3} | |
return still_on | turn_on | |
def print_grid(): | |
for y in range(SIZE): | |
print(''.join('#' if (x, y) in lights_on else ' ' for x in range(SIZE))) | |
SIZE = 10 | |
lights_on = {(1, 1), (2, 1), (3, 1)} | |
print_grid() | |
for i in range(100): | |
lights_on = evolve(lights_on) | |
print_grid() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment