From 92bb2921133ee464db53b20415f8d10a0bcc7bf1 Mon Sep 17 00:00:00 2001 From: Michele Zenoni Date: Fri, 17 Sep 2021 02:24:28 +0200 Subject: [PATCH] examples: add Rule 110 example (#11516) --- examples/rule110.v | 100 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 examples/rule110.v diff --git a/examples/rule110.v b/examples/rule110.v new file mode 100644 index 0000000000..10e439fee9 --- /dev/null +++ b/examples/rule110.v @@ -0,0 +1,100 @@ +import os +import rand + +fn main() { + mut arg := '31' + if os.args.len != 2 { + println('Usage: rule110 []') + println('Using default `n` value: 31') + } else { + arg = os.args[1] + } + + mut n := arg.int() + if n > 200 || n < 3 { + eprintln('`n` must be between 3 and 200!') + exit(1) + } + + print('\n') + title := ' Rule 110 V Implementation ' + title_len := title.len + if n > title_len { + for _ in 0 .. (n - title_len) / 2 { + print('=') + } + print(title) + for _ in 0 .. (n - title_len) / 2 { + print('=') + } + } else { + println(title[1..(title_len - 1)]) + } + + mut generation_bin := []int{len: n} + for i in 0 .. n { + generation_bin[i] = rand.intn(2) + } + print('\n') + + // println('Random generated first automaton content: $generation_bin') + for _ in 0 .. n { + print_generation(generation_bin) + next_generation(mut generation_bin) + } +} + +fn print_generation(arr []int) { + symbols := [' ', '*']! + for i in 0 .. arr.len { + print(symbols[arr[i]]) + } + print('\n') +} + +fn next_generation(mut gen []int) { + mut arr := gen.clone() + mut prev := 0 + mut curr := 0 + mut next := 0 + for i in 0 .. arr.len { + if (i - 1) % gen.len < 0 { + prev = gen[gen.len - 1] + } else { + prev = gen[(i - 1) % gen.len] + } + curr = gen[i] + next = gen[(i + 1) % gen.len] + + if prev == 1 { + if curr == 1 { + if next == 1 { // 111 + arr[i] = 0 + } else { // 110 + arr[i] = 1 + } + } else { + if next == 1 { // 101 + arr[i] = 1 + } else { // 100 + arr[i] = 0 + } + } + } else { + if curr == 1 { + if next == 1 { // 011 + arr[i] = 1 + } else { // 010 + arr[i] = 1 + } + } else { + if next == 1 { // 001 + arr[i] = 1 + } else { // 000 + arr[i] = 0 + } + } + } + } + gen = arr.clone() +}