June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,10 @@
### ## # # # # #
# ##### # # #
## ## # #
## ### #
## # ##
## ###
## # #
## #
##
## ok

View file

@ -1,9 +1,9 @@
function next_gen(a::BitArray{1}, isperiodic=false)
b = copy(a)
if isperiodic
ncnt = [a[end], a[1:end-1]] + [a[2:end], a[1]]
ncnt = prepend!(a[1:end-1], [a[end]]) + append!(a[2:end], [a[1]])
else
ncnt = [false, a[1:end-1]] + [a[2:end], false]
ncnt = prepend!(a[1:end-1], [false]) + append!(a[2:end], [false])
end
b[ncnt .== 0] = false
b[ncnt .== 2] = ~b[ncnt .== 2]
@ -16,7 +16,7 @@ function show_gen(a::BitArray{1})
end
hi = 70
a = randbool(hi)
a = bitrand(hi)
b = falses(hi)
println("A 1D Cellular Atomaton with ", hi, " cells and empty bounds.")
while any(a) && any(a .!= b)
@ -24,7 +24,7 @@ while any(a) && any(a .!= b)
b = copy(a)
a = next_gen(a)
end
a = randbool(hi)
a = bitrand(hi)
b = falses(hi)
println()
println("A 1D Cellular Atomaton with ", hi, " cells and periodic bounds.")

View file

@ -0,0 +1,27 @@
// version 1.1.4-3
val trans = "___#_##_"
fun v(cell: StringBuilder, i: Int) = if (cell[i] != '_') 1 else 0
fun evolve(cell: StringBuilder, backup: StringBuilder): Boolean {
val len = cell.length - 2
var diff = 0
for (i in 1 until len) {
/* use left, self, right as binary number bits for table index */
backup[i] = trans[v(cell, i - 1) * 4 + v(cell, i) * 2 + v(cell, i + 1)]
diff += if (backup[i] != cell[i]) 1 else 0
}
cell.setLength(0)
cell.append(backup)
return diff != 0
}
fun main(args: Array<String>) {
val c = StringBuilder("_###_##_#_#_#_#__#__")
val b = StringBuilder("____________________")
do {
println(c.substring(1))
}
while (evolve(c,b))
}

View file

@ -1,68 +1,48 @@
import math
randomize()
import random
type
TBoolArray = array[0..30, bool] # an array that is indexed with 0..10
TSymbols = tuple[on: char , off: char]
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 = rand([true, false])
const
num_turns = 20
symbols:TSymbols = ('#','_')
num_turns = 20
symbols = ['_', '#']
proc `==` (x:TBoolArray,y:TBoolArray): bool =
if len(x) != len(y):
return False
for i in 0..(len(x)-1):
if x[i] != y[i]:
return False
return True
T = true
F = false
proc count_neighbours(map:TBoolArray , tile:int):int =
result = 0
if tile != len(map)-1 and map[tile+1]:
result += 1
if tile != 0 and map[tile-1]:
result += 1
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]
proc print_map(map:TBoolArray, symbols:TSymbols) =
for i in map:
if i:
write(stdout,symbols[0])
else:
write(stdout,symbols[1])
write(stdout,"\n")
# map = randomMap() # uncomment for random start
proc random_map(): TBoolArray =
var map = [False,False,False,False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,False]
for i in 0..(len(map)-1):
map[i] = bool(random(2))
return map
print(map, symbols)
proc fixed_map(): TBoolArray =
var map = [False,True,True,True,False,True,True,False,True,False,True,
False,True,False,True,False,False,True,False,False,False,False,
False,False,False,False,False,False,False,False,False]
return map
for _ in 0 ..< num_turns:
var map2 = map
#make the map
var map:TBoolArray
#map = random_map() # uncomment for random start
map = fixed_map()
print_map(map,symbols)
for i in 0..num_turns:
var new_map = map
for j in 0..(len(map)-1):
if map[j]:
if count_neighbours(map, j) == 2 or
count_neighbours(map, j) == 0:
new_map[j] = False
else:
if count_neighbours(map, j) == 2:
new_map[j] = True
if new_map == map:
print_map(map,symbols)
break
map = new_map
print_map(map,symbols)
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

View file

@ -0,0 +1 @@
cur = [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]; for(n=0, 9, print(cur); cur = step(cur));

View file

@ -0,0 +1,10 @@
[0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0]
[0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

View file

@ -0,0 +1,36 @@
class Automaton {
has $.rule;
has @.cells;
has @.code = $!rule.fmt('%08b').flip.comb».Int;
method gist { "|{ @!cells.map({+$_ ?? '#' !! ' '}).join }|" }
method succ {
self.new: :$!rule, :@!code, :cells()
@!code[
4 «*« @!cells.rotate(-1)
»+« 2 «*« @!cells
»+« @!cells.rotate(1)
]
)
}
}
# The rule proposed for this task is rule 0b01101000 = 104
my @padding = 0 xx 5;
my Automaton $a .= new:
rule => 104,
cells => flat @padding, '111011010101'.comb, @padding
;
say $a++ for ^10;
# Rule 104 is not particularly interesting so here is [[wp:Rule 90|Rule 90]],
# which shows a [[wp:Sierpinski Triangle|Sierpinski Triangle]].
say '';
@padding = 0 xx 25;
$a = Automaton.new: :rule(90), :cells(flat @padding, 1, @padding);
say $a++ for ^20;

View file

@ -0,0 +1,27 @@
# Project : One-dimensional cellular automata
# Date : 2017/10/21
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
rule = ["0", "0", "0", "1", "0", "1", "1", "0"]
now = "01110110101010100100"
for generation = 0 to 9
see "generation " + generation + ": " + now + nl
nxt = ""
for cell = 1 to len(now)
str = "bintodec(" + '"' +substr("0"+now+"0", cell, 3) + '"' + ")"
eval("p=" + str)
nxt = nxt + rule[p+1]
next
temp = nxt
nxt = now
now = temp
next
func bintodec(bin)
binsum = 0
for n=1 to len(bin)
binsum = binsum + number(bin[n]) *pow(2, len(bin)-n)
next
return binsum