Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,6 +1,8 @@
|
|||
Assume an array of cells with an initial distribution of live and dead cells, and imaginary cells off the end of the array having fixed values.
|
||||
Assume an array of cells with an initial distribution of live and dead cells,
|
||||
and imaginary cells off the end of the array having fixed values.
|
||||
|
||||
Cells in the next generation of the array are calculated based on the value of the cell and its left and right nearest neighbours in the current generation.
|
||||
|
||||
If, in the following table, a live cell is represented by 1 and a dead cell by 0 then to generate the value of the cell at a particular index in the array of cellular values you use the following table:
|
||||
|
||||
0'''0'''0 -> 0 #
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
# We could cheat and count the bits, but let's keep this general.
|
||||
# . = dead, # = alive, middle cells survives iff one of the configurations
|
||||
# below is satisified.
|
||||
survival_scenarios = [
|
||||
'.##' # happy neighbors
|
||||
'#.#' # birth
|
||||
'##.' # happy neighbors
|
||||
]
|
||||
|
||||
b2c = (b) -> if b then '#' else '.'
|
||||
|
||||
cell_next_gen = (left_alive, me_alive, right_alive) ->
|
||||
fingerprint = b2c(left_alive) + b2c(me_alive) + b2c(right_alive)
|
||||
fingerprint in survival_scenarios
|
||||
|
||||
cells_for_next_gen = (cells) ->
|
||||
# This function assumes a finite array, i.e. cells can't be born outside
|
||||
# the original array.
|
||||
(cell_next_gen(cells[i-1], cells[i], cells[i+1]) for i in [0...cells.length])
|
||||
|
||||
display = (cells) ->
|
||||
(b2c(is_alive) for is_alive in cells).join ''
|
||||
|
||||
simulate = (cells) ->
|
||||
while true
|
||||
console.log display cells
|
||||
new_cells = cells_for_next_gen cells
|
||||
break if display(cells) == display(new_cells)
|
||||
cells = new_cells
|
||||
console.log "equilibrium achieved"
|
||||
|
||||
simulate (c == '#' for c in ".###.##.#.#.#.#..#..")
|
||||
|
|
@ -12,9 +12,9 @@ void main() {
|
|||
//foreach (immutable i, immutable b; A) {
|
||||
foreach (immutable i; 1 .. A.length - 1) {
|
||||
"_#"[A[i]].write;
|
||||
immutable val = (cast(uint)A[i - 1] << 2) |
|
||||
(cast(uint)A[i] << 1) |
|
||||
cast(uint)A[i + 1];
|
||||
immutable val = (uint(A[i - 1]) << 2) |
|
||||
(uint(A[i]) << 1) |
|
||||
uint(A[i + 1]);
|
||||
B[i] = val == 3 || val == 5 || val == 6;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
#lang slideshow
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Simulation of cellular automata, as described by Stephen Wolfram in his 1983 paper.
|
||||
;; Uses Racket's inline image display capability for visual presentation
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(require racket/draw)
|
||||
(require slideshow)
|
||||
|
||||
(define *rules* '((1 1 1) (1 1 0) (1 0 1) (1 0 0)
|
||||
(0 1 1) (0 1 0) (0 0 1) (0 0 0)))
|
||||
|
||||
(define (bordered-square n)
|
||||
(filled-rectangle n n #:draw-border? #t))
|
||||
|
||||
(define (draw-row lst)
|
||||
(apply hc-append 2 (map (λ (x) (colorize (bordered-square 10) (cond ((= x 0) "gray")
|
||||
((= x 1) "red")
|
||||
(else "gray"))))
|
||||
lst)))
|
||||
|
||||
(define (extract-neighborhood nth prev-row)
|
||||
(take (drop (append '(0) prev-row '(0)) nth) 3))
|
||||
|
||||
(define (automaton-to-bits n)
|
||||
(reverse (map (λ (y) (if (zero? (bitwise-and y n)) 0 1))
|
||||
(map (λ (x) (expt 2 x)) (range 0 8)))))
|
||||
|
||||
(define (get-rules bits)
|
||||
(map cdr (filter (λ (x) (= (car x) 1)) (map cons bits *rules*))))
|
||||
|
||||
(define (advance-row old-row rules)
|
||||
(let ([new '()])
|
||||
(for ([i (in-range 0 (length old-row))])
|
||||
(set! new (cons (if (member (extract-neighborhood i old-row)
|
||||
rules) 1 0) new)))
|
||||
(reverse new)))
|
||||
|
||||
(define (draw-automaton automaton init-row row-number)
|
||||
(let* ([bit-representation (automaton-to-bits automaton)]
|
||||
[rules (get-rules bit-representation)]
|
||||
[rows (list init-row)])
|
||||
(for ([i (in-range 1 row-number)])
|
||||
(set! rows (cons (advance-row (car rows) rules)
|
||||
rows)))
|
||||
(apply vc-append 2 (map draw-row (reverse rows)))))
|
||||
|
||||
(draw-automaton 104 '(0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0) 10)
|
||||
Loading…
Add table
Add a link
Reference in a new issue