From 1970ffa5aa8b604638c00b747873dc1fefd5ceb6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= Date: Wed, 7 Jan 2026 14:38:52 +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 470c878..79ac2cb 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 fc98357..025be56 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 ad5ab1b..ac87db4 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 4605dd2..6c722cb 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 b2befcb..cc7686a 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 8fba68f..59a36c7 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 d757b1e..ba2f5f6 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.51.0