2019-11-23 16:33:25 +03:00
|
|
|
module automaton
|
|
|
|
|
2020-10-26 14:14:21 +03:00
|
|
|
// ///////////////////////////////////////////////////////////
|
2019-11-23 16:33:25 +03:00
|
|
|
pub struct A2D {
|
|
|
|
pub mut:
|
|
|
|
maxx int
|
|
|
|
maxy int
|
|
|
|
data &int
|
|
|
|
}
|
2020-10-26 14:14:21 +03:00
|
|
|
|
2020-11-04 11:00:59 +03:00
|
|
|
fn new_a2d(maxx int, maxy int) &A2D {
|
|
|
|
size := int(sizeof(int)) * (maxx * maxy)
|
|
|
|
return &A2D{
|
|
|
|
maxx: maxx
|
|
|
|
maxy: maxy
|
2021-04-04 20:14:51 +03:00
|
|
|
data: unsafe { &int(vcalloc(size)) }
|
2020-11-04 11:00:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 14:14:21 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (a &A2D) set(x int, y int, newval int) {
|
2019-11-23 16:33:25 +03:00
|
|
|
unsafe {
|
|
|
|
mut e := &int(0)
|
2020-10-26 14:14:21 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-10-26 14:14:21 +03:00
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn (a &A2D) get(x int, y int) int {
|
2019-11-23 16:33:25 +03:00
|
|
|
unsafe {
|
|
|
|
mut e := &int(0)
|
2020-10-26 14:14:21 +03:00
|
|
|
e = a.data + y * a.maxx + x
|
2021-04-23 14:48:04 +03:00
|
|
|
_ = e
|
2019-11-23 16:33:25 +03:00
|
|
|
return *e
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 14:14:21 +03:00
|
|
|
|
|
|
|
[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)
|
2019-11-23 16:33:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 14:14:21 +03:00
|
|
|
// ///////////////////////////////////////////////////////////
|
2019-11-23 16:33:25 +03:00
|
|
|
pub struct Automaton {
|
|
|
|
pub mut:
|
2020-10-26 14:14:21 +03:00
|
|
|
field &A2D
|
2019-11-23 16:33:25 +03:00
|
|
|
new_field &A2D
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:00:59 +03:00
|
|
|
fn new_automaton(ftext string) Automaton {
|
|
|
|
f := ftext.split('\n').map(it.trim_space()).filter(it.len > 0)
|
2019-12-06 19:23:24 +03:00
|
|
|
maxy := f.len
|
2019-11-23 16:33:25 +03:00
|
|
|
mut maxx := 0
|
2020-10-26 14:14:21 +03:00
|
|
|
for y := 0; y < f.len; y++ {
|
2019-11-23 16:33:25 +03:00
|
|
|
if maxx < f[y].len {
|
|
|
|
maxx = f[y].len
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 11:00:59 +03:00
|
|
|
field := new_a2d(maxx, maxy)
|
|
|
|
new_field := new_a2d(maxx, maxy)
|
2020-10-26 14:14:21 +03:00
|
|
|
for y in 0 .. field.maxy {
|
|
|
|
for x in 0 .. field.maxx {
|
2020-11-04 11:00:59 +03:00
|
|
|
val := if x < f[y].len && f[y][x] == `#` { 1 } else { 0 }
|
|
|
|
field.set(x, y, val)
|
2019-11-23 16:33:25 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-26 14:14:21 +03:00
|
|
|
return Automaton{
|
|
|
|
field: field
|
|
|
|
new_field: new_field
|
|
|
|
}
|
2019-11-23 16:33:25 +03:00
|
|
|
}
|
|
|
|
|
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()
|
2020-10-26 14:14:21 +03:00
|
|
|
for y := 1; y < aa.field.maxy; y++ {
|
|
|
|
for x := 1; x < aa.field.maxx; x++ {
|
|
|
|
moore_sum := (0 + aa.field.get(x - 1, y - 1) + aa.field.get(x, y - 1) + aa.field.get(x +
|
2021-04-20 17:16:35 +03:00
|
|
|
1, y - 1) + 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))
|
2020-10-26 14:14:21 +03:00
|
|
|
cell := aa.field.get(x, y)
|
|
|
|
v := if cell == 1 { moore_sum in [2, 3] } else { moore_sum == 3 }
|
2021-04-20 17:16:35 +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-11-04 11:00:59 +03:00
|
|
|
field := '
|
|
|
|
*******************************************
|
|
|
|
* *
|
|
|
|
* A shooting gun: *
|
|
|
|
* # *
|
|
|
|
* # # *
|
|
|
|
* ## ## ## *
|
|
|
|
* # # ## ## *
|
|
|
|
* ## # # ## *
|
|
|
|
* ## # # ## # # *
|
|
|
|
* # # # *
|
|
|
|
* # # *
|
|
|
|
* ## *
|
|
|
|
* *
|
|
|
|
* Tetris Life: *
|
|
|
|
* *
|
|
|
|
* ## #### *
|
|
|
|
* ## *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* # ## *
|
|
|
|
* ### ## *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* # *
|
|
|
|
* ### *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
*******************************************
|
|
|
|
'
|
2019-11-23 16:33:25 +03:00
|
|
|
return new_automaton(field)
|
|
|
|
}
|