Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,55 @@
import random
from collections import defaultdict
printdead, printlive = '-#'
maxgenerations = 3
cellcount = 3,3
celltable = defaultdict(int, {
(1, 2): 1,
(1, 3): 1,
(0, 3): 1,
} ) # Only need to populate with the keys leading to life
##
## Start States
##
# blinker
u = universe = defaultdict(int)
u[(1,0)], u[(1,1)], u[(1,2)] = 1,1,1
## toad
#u = universe = defaultdict(int)
#u[(5,5)], u[(5,6)], u[(5,7)] = 1,1,1
#u[(6,6)], u[(6,7)], u[(6,8)] = 1,1,1
## glider
#u = universe = defaultdict(int)
#maxgenerations = 16
#u[(5,5)], u[(5,6)], u[(5,7)] = 1,1,1
#u[(6,5)] = 1
#u[(7,6)] = 1
## random start
#universe = defaultdict(int,
# # array of random start values
# ( ((row, col), random.choice((0,1)))
# for col in range(cellcount[0])
# for row in range(cellcount[1])
# ) ) # returns 0 for out of bounds
for i in range(maxgenerations):
print("\nGeneration %3i:" % ( i, ))
for row in range(cellcount[1]):
print(" ", ''.join(str(universe[(row,col)])
for col in range(cellcount[0])).replace(
'0', printdead).replace('1', printlive))
nextgeneration = defaultdict(int)
for row in range(cellcount[1]):
for col in range(cellcount[0]):
nextgeneration[(row,col)] = celltable[
( universe[(row,col)],
-universe[(row,col)] + sum(universe[(r,c)]
for r in range(row-1,row+2)
for c in range(col-1, col+2) )
) ]
universe = nextgeneration

View file

@ -0,0 +1,37 @@
from collections import Counter
def life(world, N):
"Play Conway's game of life for N generations from initial world."
for g in range(N+1):
display(world, g)
counts = Counter(n for c in world for n in offset(neighboring_cells, c))
world = {c for c in counts
if counts[c] == 3 or (counts[c] == 2 and c in world)}
neighboring_cells = [(-1, -1), (-1, 0), (-1, 1),
( 0, -1), ( 0, 1),
( 1, -1), ( 1, 0), ( 1, 1)]
def offset(cells, delta):
"Slide/offset all the cells by delta, a (dx, dy) vector."
(dx, dy) = delta
return {(x+dx, y+dy) for (x, y) in cells}
def display(world, g):
"Display the world as a grid of characters."
print(' GENERATION {}:'.format(g))
Xs, Ys = zip(*world)
Xrange = range(min(Xs), max(Xs)+1)
for y in range(min(Ys), max(Ys)+1):
print(''.join('#' if (x, y) in world else '.'
for x in Xrange))
blinker = {(1, 0), (1, 1), (1, 2)}
block = {(0, 0), (1, 1), (0, 1), (1, 0)}
toad = {(1, 2), (0, 1), (0, 0), (0, 2), (1, 3), (1, 1)}
glider = {(0, 1), (1, 0), (0, 0), (0, 2), (2, 1)}
world = (block | offset(blinker, (5, 2)) | offset(glider, (15, 5)) | offset(toad, (25, 5))
| {(18, 2), (19, 2), (20, 2), (21, 2)} | offset(block, (35, 7)))
life(world, 5)

View file

@ -0,0 +1,62 @@
import numpy as np
from pandas import DataFrame
import matplotlib.pyplot as plt
#import time
def conway_life(len=10, wid=10, gen=5):
curr_gen = DataFrame(np.random.randint(0, 2, (len+2, wid+2)),
index = range(len+2),
columns = range(wid+2))
curr_gen[0] = 0
curr_gen[wid+1] = 0
curr_gen[0: 1] = 0
curr_gen[len+1: len+2] = 0
for i in range(gen):
fig, ax = plt.subplots()
draw = curr_gen[1:len+1].drop([0, wid+1], axis=1)
# 画图
image = draw
ax.imshow(image, cmap=plt.cm.cool, interpolation='nearest')
ax.set_title("Conway's game of life.")
# Move left and bottom spines outward by 10 points
ax.spines['left'].set_position(('outward', 10))
ax.spines['bottom'].set_position(('outward', 10))
# Hide the right and top spines
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# Only show ticks on the left and bottom spines
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
plt.show()
# time.sleep(1)
# 初始化空表
next_gen = DataFrame(np.random.randint(0, 1, (len+2, wid+2)),
index = range(len+2),
columns = range(wid+2))
# 生成下一代
for x in range(1, wid+1):
for y in range(1, len+1):
env = (curr_gen[x-1][y-1] + curr_gen[x][y-1] +
curr_gen[x+1][y-1]+ curr_gen[x-1][y] +
curr_gen[x+1][y] + curr_gen[x-1][y+1] +
curr_gen[x][y+1] + curr_gen[x+1][y+1])
if (not curr_gen[x][y] and env == 3):
next_gen[x][y] = 1
if (curr_gen[x][y] and env in (2, 3)):
next_gen[x][y] = 1
curr_gen = next_gen
conway_life()