2019-11-23 16:33:25 +03:00
|
|
|
module automaton
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct A2D {
|
|
|
|
pub mut:
|
|
|
|
maxx int
|
|
|
|
maxy int
|
|
|
|
data &int
|
|
|
|
}
|
|
|
|
[inline] pub fn (a &A2D) set(x,y int, newval int) {
|
|
|
|
unsafe {
|
|
|
|
mut e := &int(0)
|
|
|
|
e = a.data + y*a.maxx + x
|
2020-04-25 11:14:01 +03:00
|
|
|
*e = newval
|
2019-11-23 16:33:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
[inline] pub fn (a &A2D) get(x,y int) int {
|
|
|
|
unsafe {
|
|
|
|
mut e := &int(0)
|
|
|
|
e = a.data + y*a.maxx + x
|
|
|
|
return *e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[inline] pub fn (a &A2D) clear() {
|
|
|
|
for y := 0; y<a.maxy; y++ {
|
|
|
|
for x := 0; x<a.maxx; x++ {
|
|
|
|
a.set(x,y,0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct Automaton {
|
|
|
|
pub mut:
|
|
|
|
field &A2D
|
|
|
|
new_field &A2D
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_automaton(f [][]int) Automaton {
|
2019-12-06 19:23:24 +03:00
|
|
|
maxy := f.len
|
2019-11-23 16:33:25 +03:00
|
|
|
mut maxx := 0
|
|
|
|
for y := 0; y<f.len; y++ {
|
|
|
|
if maxx < f[y].len {
|
|
|
|
maxx = f[y].len
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 19:28:05 +03:00
|
|
|
field := &A2D{ maxx: maxx maxy: maxy data: &int( vcalloc( sizeof(int) * maxy * maxx ) ) }
|
|
|
|
new_field := &A2D{ maxx: maxx maxy: maxy data: &int( vcalloc( sizeof(int) * maxy * maxx ) ) }
|
2020-02-24 19:55:16 +03:00
|
|
|
for y in 0..field.maxy {
|
|
|
|
for x in 0..field.maxx {
|
2019-11-23 16:33:25 +03:00
|
|
|
field.set( x, y, f[y][x] )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Automaton{ field: field new_field: new_field }
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:00:46 +03:00
|
|
|
pub fn (mut aa Automaton) update() {
|
2019-11-23 16:33:25 +03:00
|
|
|
aa.new_field.clear()
|
|
|
|
for y := 1; y<aa.field.maxy; y++ {
|
|
|
|
for x := 1; x<aa.field.maxx; x++ {
|
|
|
|
moore_sum := ( 0 +
|
2019-12-06 19:23:24 +03:00
|
|
|
aa.field.get(x-1,y-1) + aa.field.get(x,y-1) + aa.field.get(x+1,y-1) +
|
2019-11-23 16:33:25 +03:00
|
|
|
aa.field.get(x-1,y ) + 0 + aa.field.get(x+1,y ) +
|
|
|
|
aa.field.get(x-1,y+1) + aa.field.get(x,y+1) + aa.field.get(x+1,y+1)
|
|
|
|
)
|
|
|
|
cell := aa.field.get(x,y)
|
|
|
|
v := if cell == 1 {
|
2019-11-23 20:40:54 +03:00
|
|
|
moore_sum in [2, 3]
|
2019-11-23 16:33:25 +03:00
|
|
|
} else {
|
2019-11-23 20:40:54 +03:00
|
|
|
moore_sum == 3
|
2019-11-23 16:33:25 +03:00
|
|
|
}
|
2019-11-23 20:40:54 +03:00
|
|
|
aa.new_field.set(x,y, if v { 1 } else { 0 })
|
2019-11-23 16:33:25 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-06 23:02:09 +03:00
|
|
|
tmp := aa.field
|
2019-11-23 16:33:25 +03:00
|
|
|
aa.field = aa.new_field
|
|
|
|
aa.new_field = tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gun() Automaton {
|
2020-05-13 20:25:40 +03:00
|
|
|
mut field := [
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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