- Random.int 3 |> state_of_int |> cell_of_state
-
- let f = 0.000001 (* Probability of spontaneous ignition *)
- let p = 0.1 (* Probability of spontaneous growth *)
-
- let is_probable p =
- (Random.float 1.0) <= p
-
- let next state ~burning_neighbors =
- match state, burning_neighbors with
- | E, _ when is_probable p -> T
- | E, _ -> E
- | T, 0 when is_probable f -> B
- | T, _ when burning_neighbors > 0 -> B
- | T, _ -> T
- | B, _ -> E
-
- let burning_neighbors inputs =
- inputs
- |> List.filter_map ~f:(function "B" -> Some 1 | _ -> None)
- |> List.fold_left ~init:0 ~f:(+)
-
- let transition ~state ~inputs =
- state
- |> state_of_string
- |> next ~burning_neighbors:(burning_neighbors inputs)
- |> cell_of_state
+ Random.int 3 |> State.of_int |> State.to_cell
+
+ let count_of_burning =
+ List.map ~f:State.of_cell_state
+ |- List.filter ~f:State.is_burning
+ |- List.length
+
+ let transition ~self ~neighbors =
+ self |> State.of_cell_state
+ |> State.next ~burning_neighbors:(count_of_burning neighbors)
+ |> State.to_cell