How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Racket programming language
How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Racket programming language
Table of Contents
Problem Statement
The purpose of this task is to create a version of an Elementary cellular automaton whose number of cells is only limited by the memory size of the computer. To be precise, consider the state of the automaton to be made of an infinite number of cells, but with a bounded support. In other words, to describe the state of the automaton, you need a finite number of adjacent cells, along with their individual state, and you then consider that the individual state of each of all other cells is the negation of the closest individual cell among the previously defined finite number of cells. Examples: More complex methods can be imagined, provided it is possible to somehow encode the infinite sections. But for this task we will stick to this simple version.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Racket programming language
Source code in the racket programming language
#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 ; 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)))
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Closest-pair problem step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Price fraction step by step in the Oz programming language
You may also check:How to resolve the algorithm User input/Text step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the ALGOL W programming language