2019-11-23 14:33:25 +01: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-23 11:19:04 +02:00
|
|
|
(*e) = newval
|
2019-11-23 14:33:25 +01: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 17:23:24 +01:00
|
|
|
maxy := f.len
|
2019-11-23 14:33:25 +01:00
|
|
|
mut maxx := 0
|
|
|
|
for y := 0; y<f.len; y++ {
|
|
|
|
if maxx < f[y].len {
|
|
|
|
maxx = f[y].len
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 17:28:05 +01: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 17:55:16 +01:00
|
|
|
for y in 0..field.maxy {
|
|
|
|
for x in 0..field.maxx {
|
2019-11-23 14:33:25 +01:00
|
|
|
field.set( x, y, f[y][x] )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Automaton{ field: field new_field: new_field }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (aa mut Automaton) update() {
|
|
|
|
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 17:23:24 +01:00
|
|
|
aa.field.get(x-1,y-1) + aa.field.get(x,y-1) + aa.field.get(x+1,y-1) +
|
2019-11-23 14:33:25 +01: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 18:40:54 +01:00
|
|
|
moore_sum in [2, 3]
|
2019-11-23 14:33:25 +01:00
|
|
|
} else {
|
2019-11-23 18:40:54 +01:00
|
|
|
moore_sum == 3
|
2019-11-23 14:33:25 +01:00
|
|
|
}
|
2019-11-23 18:40:54 +01:00
|
|
|
aa.new_field.set(x,y, if v { 1 } else { 0 })
|
2019-11-23 14:33:25 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-06 21:02:09 +01:00
|
|
|
tmp := aa.field
|
2019-11-23 14:33:25 +01:00
|
|
|
aa.field = aa.new_field
|
|
|
|
aa.new_field = tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gun() Automaton {
|
|
|
|
mut field := []array_int
|
|
|
|
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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