Last active
July 4, 2025 00:36
-
-
Save CodeMan99/92423fd8156296293dd21cfcce9b4737 to your computer and use it in GitHub Desktop.
Conway's Game of Life - functional python from https://youtu.be/o9pEzgHorH0?feature=shared&t=1040
This file contains hidden or 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
import sys | |
from itertools import chain | |
from time import sleep | |
def neighbors(point): | |
x, y = point | |
yield x + 1, y | |
yield x - 1, y | |
yield x, y + 1 | |
yield x, y - 1 | |
yield x + 1, y + 1 | |
yield x + 1, y - 1 | |
yield x - 1, y + 1 | |
yield x - 1, y - 1 | |
def advance(board): | |
newstate = set() | |
recalc = board | set(chain(*map(neighbors, board))) | |
for point in recalc: | |
count = sum((neigh in board) for neigh in neighbors(point)) | |
if count == 3 or (count == 2 and point in board): | |
newstate.add(point) | |
return newstate | |
def print_board(board, *, min_point=None, max_point=None): | |
min_x = min_point[0] if min_point is not None else min(x for (x, _) in board) | |
min_y = min_point[1] if min_point is not None else min(y for (_, y) in board) | |
max_x = max_point[0] if max_point is not None else max(x for (x, _) in board) | |
max_y = max_point[1] if max_point is not None else max(y for (_, y) in board) | |
for x in range(min_x, max_x + 1): | |
for y in range(min_y, max_y + 1): | |
point = x, y | |
yield '•' if point in board else ' ' | |
yield '\n' | |
board_size = dict(min_point=(0, -2), max_point=(17, 20)) | |
glider = set([ (1,0), (2,1), (2,2), (1,2), (0,2) ]) | |
star = set([ (10,12), (10,14), (9,13), (11,13) ]) | |
board = glider | star | |
for x in range(112): | |
sys.stdout.write('\x1bc ┌') | |
sys.stdout.write('─' * 23) | |
sys.stdout.write('┐\n │') | |
for char in print_board(board, **board_size): | |
sys.stdout.write('│\n │' if char == '\n' else char) | |
sys.stdout.write('\b└') | |
sys.stdout.write('─' * 23) | |
sys.stdout.write(f'┘\n step={x}\n\n') | |
sleep(1 / 12) | |
board = advance(board) |
This file contains hidden or 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
#!/usr/bin/env python3 | |
import sys | |
def read_coordinates(filename, active_cell='#'): | |
with open(filename, 'r') as board: | |
lines = board.readlines() | |
for y, line in enumerate(lines): | |
for x, c in enumerate(line.rstrip()): | |
if c == active_cell: | |
yield x, y | |
if __name__ == "__main__": | |
_args = iter(sys.argv[1:]) | |
_filename_arg = next(_args, None) | |
if _filename_arg is None: | |
print("Usage: lcoord.py <board.lfe> [active-cell]") | |
else: | |
print("board = set([") | |
for x, y in read_coordinates(_filename_arg, active_cell=next(_args, '#')): | |
print(f" ({x}, {y}),") | |
print("])") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pulsar start: