From 7ac5d90cf1d06fe7a7d9370214ee55e0b12c14c4 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= <git@ameliathe1st.gay>
Date: Thu, 19 Dec 2024 09:43:32 +0100
Subject: [PATCH] =?utf8?q?Simplification/g=C3=A9n=C3=A9ralisation=20des=20?=
 =?utf8?q?voisinages?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

---
 automata/automata.ml           | 14 +++++++++-----
 automata/automata.mli          |  6 +++++-
 automata/automata/brain.ml     | 17 ++++++++++++-----
 automata/automata/cont.ml      |  8 ++++++--
 automata/automata/life.ml      | 16 +++++++++++-----
 automata/automata/maze.ml      | 16 +++++++++++-----
 automata/automata/wireworld.ml | 17 ++++++++++++-----
 7 files changed, 66 insertions(+), 28 deletions(-)

diff --git a/automata/automata.ml b/automata/automata.ml
index 8ad1fc4..8aac1c6 100644
--- a/automata/automata.ml
+++ b/automata/automata.ml
@@ -24,7 +24,11 @@ module type Automaton = sig
 
   val map : ('a -> 'b) -> 'a neighbours -> 'b neighbours
 
-  val transition : t neighbours -> t -> t
+  val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b neighbours -> 'a
+
+  val iter : ('a -> unit) -> 'a neighbours -> unit
+
+  val transition : t neighbours -> t
 
   val default : t
 
@@ -52,12 +56,12 @@ let set x y c ((d, cells) as board) =
           res.(x).(y) <- c;
           (d, res)
 
-let update (type t) (m : (module Automaton with type t = t)) ((d, cells) as board) =
+let update (type t) (m : (module Automaton with type t = t)) board =
         let module M = (val m) in
-        ( M.transition (M.map (Fun.const d) M.neighbours) d,
+        ( M.transition (M.map (Fun.const (fst board)) M.neighbours),
           Array.init 64 (fun x ->
-              Array.init 64 (fun y ->
-                  M.transition (M.map (fun (dx, dy) -> get (x + dx) (y + dy) board) M.neighbours) cells.(x).(y))) )
+              Array.init 64 (fun y -> M.transition (M.map (fun (dx, dy) -> get (x + dx) (y + dy) board) M.neighbours)))
+        )
 
 let automata =
         [
diff --git a/automata/automata.mli b/automata/automata.mli
index 073e67c..69aeab8 100644
--- a/automata/automata.mli
+++ b/automata/automata.mli
@@ -24,7 +24,11 @@ module type Automaton = sig
 
   val map : ('a -> 'b) -> 'a neighbours -> 'b neighbours
 
-  val transition : t neighbours -> t -> t
+  val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b neighbours -> 'a
+
+  val iter : ('a -> unit) -> 'a neighbours -> unit
+
+  val transition : t neighbours -> t
 
   val default : t
 
diff --git a/automata/automata/brain.ml b/automata/automata/brain.ml
index 1e46cb1..15c6e20 100644
--- a/automata/automata/brain.ml
+++ b/automata/automata/brain.ml
@@ -17,15 +17,22 @@ let name = "Le Cerveau de Brian"
 
 type t = Off | On | Dying
 
-type 'a neighbours = 'a list
+type 'a neighbours = 'a * 'a list
 
-let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)]
+let neighbours = ((0, 0), [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)])
 
-let map = List.map
+let map f (c, cs) = (f c, List.map f cs)
 
-let transition l = function
+let fold_left f a (c, cs) = List.fold_left f (f a c) cs
+
+let iter f (c, cs) =
+        f c;
+        List.iter f cs
+
+let transition (c, cs) =
+        match c with
         | Off ->
-                let n = List.length (List.filter (( = ) On) l) in
+                let n = List.length (List.filter (( = ) On) cs) in
                 if n = 2 then On else Off
         | On -> Dying
         | Dying -> Off
diff --git a/automata/automata/cont.ml b/automata/automata/cont.ml
index 58ec814..12ec040 100644
--- a/automata/automata/cont.ml
+++ b/automata/automata/cont.ml
@@ -19,11 +19,15 @@ type t = float
 
 type 'a neighbours = 'a list
 
-let neighbours = [(-1, 0); (0, -1); (0, 1); (1, 0)]
+let neighbours = [(0, 0); (-1, 0); (0, -1); (0, 1); (1, 0)]
 
 let map = List.map
 
-let transition l c = List.fold_left ( +. ) c l /. 5.
+let fold_left = List.fold_left
+
+let iter = List.iter
+
+let transition l = List.fold_left ( +. ) 0. l /. 5.
 
 let default = 0.
 
diff --git a/automata/automata/life.ml b/automata/automata/life.ml
index ba0b3c1..6876cae 100644
--- a/automata/automata/life.ml
+++ b/automata/automata/life.ml
@@ -17,14 +17,20 @@ let name = "Conway's Game of Life"
 
 type t = Dead | Alive
 
-type 'a neighbours = 'a list
+type 'a neighbours = 'a * 'a list
 
-let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)]
+let neighbours = ((0, 0), [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)])
 
-let map = List.map
+let map f (c, cs) = (f c, List.map f cs)
 
-let transition l c =
-        let n = List.length (List.filter (( = ) Alive) l) in
+let fold_left f a (c, cs) = List.fold_left f (f a c) cs
+
+let iter f (c, cs) =
+        f c;
+        List.iter f cs
+
+let transition (c, cs) =
+        let n = List.length (List.filter (( = ) Alive) cs) in
         if n < 2 || n > 3 then Dead else if n == 3 then Alive else c
 
 let default = Dead
diff --git a/automata/automata/maze.ml b/automata/automata/maze.ml
index f7bd101..4e9adda 100644
--- a/automata/automata/maze.ml
+++ b/automata/automata/maze.ml
@@ -17,14 +17,20 @@ let name = "Labyrinthe B3S12345"
 
 type t = Sol | Mur
 
-type 'a neighbours = 'a list
+type 'a neighbours = 'a * 'a list
 
-let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)]
+let neighbours = ((0, 0), [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)])
 
-let map = List.map
+let map f (c, cs) = (f c, List.map f cs)
 
-let transition l c =
-        let n = List.length (List.filter (( = ) Mur) l) in
+let fold_left f a (c, cs) = List.fold_left f (f a c) cs
+
+let iter f (c, cs) =
+        f c;
+        List.iter f cs
+
+let transition (c, cs) =
+        let n = List.length (List.filter (( = ) Mur) cs) in
         if n < 1 || n > 5 then Sol else if n == 3 then Mur else c
 
 let default = Sol
diff --git a/automata/automata/wireworld.ml b/automata/automata/wireworld.ml
index f8e5e01..895944d 100644
--- a/automata/automata/wireworld.ml
+++ b/automata/automata/wireworld.ml
@@ -17,16 +17,23 @@ let name = "Wireworld"
 
 type t = Empty | Conductor | Head | Tail
 
-type 'a neighbours = 'a list
+type 'a neighbours = 'a * 'a list
 
-let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)]
+let neighbours = ((0, 0), [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)])
 
-let map = List.map
+let map f (c, cs) = (f c, List.map f cs)
 
-let transition l = function
+let fold_left f a (c, cs) = List.fold_left f (f a c) cs
+
+let iter f (c, cs) =
+        f c;
+        List.iter f cs
+
+let transition (c, cs) =
+        match c with
         | Empty -> Empty
         | Conductor ->
-                let n = List.length (List.filter (( = ) Head) l) in
+                let n = List.length (List.filter (( = ) Head) cs) in
                 if n = 1 || n = 2 then Head else Conductor
         | Head -> Tail
         | Tail -> Conductor
-- 
2.46.0