From 3ea8967571a201a5f6fb397afa3e2fbb65a5dd0e Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 21:57:42 +0000 Subject: [PATCH] ocaml: phase 5.1 flood_fill.ml baseline (largest grid component = 7) Recursive 4-way flood fill from every unvisited 1-cell: let rec flood visited r c = if r < 0 || r >= h || c < 0 || c >= w then 0 else if visited.(r).(c) || grid.(r).(c) = 0 then 0 else begin visited.(r).(c) <- true; 1 + flood visited (r - 1) c + flood visited (r + 1) c + flood visited r (c - 1) + flood visited r (c + 1) end Grid (1s shown as #, 0s as .): # # . # # # . . . # . . # . . # # # # . . . . # # Largest component: {(2,2),(3,0),(3,1),(3,2),(3,3),(4,3),(4,4)} = 7. Bounds check r >= 0 must short-circuit before visited/grid reads; relies on the && / || fix from iter 242. 157 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/flood_fill.ml | 38 ++++++++++++++++++++++++++++++++ plans/ocaml-on-sx.md | 8 +++++++ 3 files changed, 47 insertions(+) create mode 100644 lib/ocaml/baseline/flood_fill.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index d1aa3488..26f54877 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -73,6 +73,7 @@ "fizz_classifier.ml": 540, "fizzbuzz.ml": 57, "flatten_tree.ml": 28, + "flood_fill.ml": 7, "floyd_warshall.ml": 9, "lis.ml": 6, "list_ops.ml": 30, diff --git a/lib/ocaml/baseline/flood_fill.ml b/lib/ocaml/baseline/flood_fill.ml new file mode 100644 index 00000000..bc2a7e25 --- /dev/null +++ b/lib/ocaml/baseline/flood_fill.ml @@ -0,0 +1,38 @@ +let h = 5 +let w = 5 + +let grid = [| + [| 1; 1; 0; 1; 1 |]; + [| 1; 0; 0; 0; 1 |]; + [| 0; 0; 1; 0; 0 |]; + [| 1; 1; 1; 1; 0 |]; + [| 0; 0; 0; 1; 1 |] +|] + +let rec flood visited r c = + if r < 0 || r >= h || c < 0 || c >= w then 0 + else if visited.(r).(c) || grid.(r).(c) = 0 then 0 + else begin + visited.(r).(c) <- true; + 1 + flood visited (r - 1) c + + flood visited (r + 1) c + + flood visited r (c - 1) + + flood visited r (c + 1) + end + +let largest_component () = + let visited = Array.init h (fun _ -> Array.make w false) in + let best = ref 0 in + for r = 0 to h - 1 do + for c = 0 to w - 1 do + if grid.(r).(c) = 1 && not visited.(r).(c) then begin + let s = flood visited r c in + if s > !best then best := s + end + done + done; + !best + +;; + +largest_component () diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 0646c2ec..e2a86a11 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-10 Phase 5.1 — flood_fill.ml baseline (largest connected + component in 5×5 grid = 7). Recursive 4-direction flood from + every unvisited 1-cell; bounds check short-circuits before the + visited/grid reads (relies on iter-242 fix). The maximal + component in the test grid spans {(2,2), (3,0), (3,1), (3,2), + (3,3), (4,3), (4,4)} = 7 cells. Tests recursive function with + 4-way self-call, in-place visited array mutation, nested + short-circuited bounds + content guards. 157 baseline programs total. - 2026-05-10 Phase 5.1 — next_permutation.ml baseline (count permutations of [1;2;3;4;5] via Narayana's algorithm = 119). Standard in-place algorithm: find largest i with a.(i) < a.(i+1),