Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,48 @@
|
|||
import random
|
||||
|
||||
|
||||
type
|
||||
BoolArray = array[30, bool]
|
||||
Symbols = array[bool, char]
|
||||
|
||||
|
||||
proc neighbours(map: BoolArray, i: int): int =
|
||||
if i > 0: inc(result, int(map[i - 1]))
|
||||
if i + 1 < len(map): inc(result, int(map[i + 1]))
|
||||
|
||||
proc print(map: BoolArray, symbols: Symbols) =
|
||||
for i in map: write(stdout, symbols[i])
|
||||
write(stdout, "\l")
|
||||
|
||||
proc randomMap: BoolArray =
|
||||
randomize()
|
||||
for i in mitems(result): i = sample([true, false])
|
||||
|
||||
|
||||
const
|
||||
num_turns = 20
|
||||
symbols = ['_', '#']
|
||||
|
||||
T = true
|
||||
F = false
|
||||
|
||||
var map =
|
||||
[F, T, T, T, F, T, T, F, T, F, T, F, T, F, T,
|
||||
F, F, T, F, F, F, F, F, F, F, F, F, F, F, F]
|
||||
|
||||
# map = randomMap() # uncomment for random start
|
||||
|
||||
print(map, symbols)
|
||||
|
||||
for _ in 0 ..< num_turns:
|
||||
var map2 = map
|
||||
|
||||
for i, v in pairs(map):
|
||||
map2[i] =
|
||||
if v: neighbours(map, i) == 1
|
||||
else: neighbours(map, i) == 2
|
||||
|
||||
print(map2, symbols)
|
||||
|
||||
if map2 == map: break
|
||||
map = map2
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import strutils
|
||||
|
||||
const
|
||||
s_init: string = "_###_##_#_#_#_#__#__"
|
||||
arrLen: int = 20
|
||||
|
||||
var q0: string = s_init & repeat('_',arrLen-20)
|
||||
var q1: string = q0
|
||||
|
||||
proc life(s:string): char =
|
||||
var str: string = s
|
||||
if len(normalize(str)) == 2: # normalize eliminates underscores
|
||||
return '#'
|
||||
return '_'
|
||||
|
||||
proc evolve(q: string): string =
|
||||
result = repeat('_',arrLen)
|
||||
#result[0] = '_'
|
||||
for i in 1 .. q.len-1:
|
||||
result[i] = life(substr(q & '_',i-1,i+1))
|
||||
|
||||
echo(q1)
|
||||
q1 = evolve(q0)
|
||||
echo(q1)
|
||||
while q1 != q0:
|
||||
q0 = q1
|
||||
q1 = evolve(q0)
|
||||
echo(q1)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
proc cellAutomata =
|
||||
proc evolveInto(x, t : var string) =
|
||||
for i in x.low..x.high:
|
||||
let
|
||||
alive = x[i] == 'o'
|
||||
left = if i == x.low: false else: x[i - 1] == 'o'
|
||||
right = if i == x.high: false else: x[i + 1] == 'o'
|
||||
t[i] =
|
||||
if alive: (if left xor right: 'o' else: '.')
|
||||
else: (if left and right: 'o' else: '.')
|
||||
|
||||
var
|
||||
x = ".ooo.oo.o.o.o.o..o.."
|
||||
t = x
|
||||
|
||||
for i in 1..10:
|
||||
x.echo
|
||||
x.evolveInto t
|
||||
swap t, x
|
||||
|
||||
cellAutomata()
|
||||
Loading…
Add table
Add a link
Reference in a new issue