Elementary cellular automaton/Infinite length: Difference between revisions

=={{header|Racket}}== implementation added
(→‎Tcl: Added implementation)
(=={{header|Racket}}== implementation added)
Line 164:
23: ##.####..##.#..###.#...#..####...#..#.##.######
24: ##..#...###..####...##.#####...#.#####.#..#.....#</pre>
 
=={{header|Racket}}==
 
Uses solution to [[Elementary cellular automata]] saved in file "Elementary_cellular_automata.rkt"
 
<lang racket>#lang racket
; below is the code from the parent task
(require "Elementary_cellular_automata.rkt")
(require racket/fixnum)
 
(define (wrap-rule-infinite v-in vl-1 offset)
(define l-bit-set? (bitwise-bit-set? (fxvector-ref v-in 0) usable-bits/fixnum-1))
(define r-bit-set? (bitwise-bit-set? (fxvector-ref v-in vl-1) 0))
;; if we need to extend left offset is reduced by 1
(define l-pad-words (if l-bit-set? 1 0))
(define r-pad-words (if r-bit-set? 1 0))
(define new-words (fx+ l-pad-words r-pad-words))
(cond
[(fx= 0 new-words) (values v-in vl-1 offset)] ; nothing changes
[else
(define offset- (if l-bit-set? (fx- offset 1) offset))
(define l-sequence (if l-bit-set? (in-value 0) (in-sequences)))
(define vl-1+ (fx+ vl-1 (fx+ l-pad-words r-pad-words)))
(define v-out
(for/fxvector
#:length (fx+ vl-1+ 1) #:fill 0 ; this provides suitable right padding
((f (in-sequences l-sequence (in-fxvector v-in)))) f))
(values v-out vl-1+ offset-)]))
 
(module+ main
(define ng/90/infinite (CA-next-generation 90 #:wrap-rule wrap-rule-infinite))
(for/fold ((v (fxvector #b10000000000000000000))
(o 1)) ; start by pushing output right by one
((step (in-range 40)))
(show-automaton v #:step step #:push-right o)
(newline)
(ng/90/infinite v o)))</lang>
 
{{out}}
 
<pre>[ 0] ..............................000000000010000000000000000000
[ 1] ..............................000000000101000000000000000000
[ 2] ..............................000000001000100000000000000000
[ 3] ..............................000000010101010000000000000000
[ 4] ..............................000000100000001000000000000000
[ 5] ..............................000001010000010100000000000000
[ 6] ..............................000010001000100010000000000000
[ 7] ..............................000101010101010101000000000000
[ 8] ..............................001000000000000000100000000000
[ 9] ..............................010100000000000001010000000000
[ 10] ..............................100010000000000010001000000000
[ 11] 000000000000000000000000000001010101000000000101010100000000
[ 12] 000000000000000000000000000010000000100000001000000010000000
[ 13] 000000000000000000000000000101000001010000010100000101000000
[ 14] 000000000000000000000000001000100010001000100010001000100000
[ 15] 000000000000000000000000010101010101010101010101010101010000
[ 16] 000000000000000000000000100000000000000000000000000000001000
[ 17] 000000000000000000000001010000000000000000000000000000010100
[ 18] 000000000000000000000010001000000000000000000000000000100010
[ 19] 000000000000000000000101010100000000000000000000000001010101
[ 20] 000000000000000000001000000010000000000000000000000010000000100000000000000000000000000000
[ 21] 000000000000000000010100000101000000000000000000000101000001010000000000000000000000000000
[ 22] 000000000000000000100010001000100000000000000000001000100010001000000000000000000000000000
[ 23] 000000000000000001010101010101010000000000000000010101010101010100000000000000000000000000
[ 24] 000000000000000010000000000000001000000000000000100000000000000010000000000000000000000000
[ 25] 000000000000000101000000000000010100000000000001010000000000000101000000000000000000000000
[ 26] 000000000000001000100000000000100010000000000010001000000000001000100000000000000000000000
[ 27] 000000000000010101010000000001010101000000000101010100000000010101010000000000000000000000
[ 28] 000000000000100000001000000010000000100000001000000010000000100000001000000000000000000000
[ 29] 000000000001010000010100000101000001010000010100000101000001010000010100000000000000000000
[ 30] 000000000010001000100010001000100010001000100010001000100010001000100010000000000000000000
[ 31] 000000000101010101010101010101010101010101010101010101010101010101010101000000000000000000
[ 32] 000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000
[ 33] 000000010100000000000000000000000000000000000000000000000000000000000001010000000000000000
[ 34] 000000100010000000000000000000000000000000000000000000000000000000000010001000000000000000
[ 35] 000001010101000000000000000000000000000000000000000000000000000000000101010100000000000000
[ 36] 000010000000100000000000000000000000000000000000000000000000000000001000000010000000000000
[ 37] 000101000001010000000000000000000000000000000000000000000000000000010100000101000000000000
[ 38] 001000100010001000000000000000000000000000000000000000000000000000100010001000100000000000
[ 39] 010101010101010100000000000000000000000000000000000000000000000001010101010101010000000000
#fx(536879104 0 33554944)
0</pre>
 
=={{header|Ruby}}==
569

edits