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