From c8327823eed1ad8bfb315e64c3b74ac21a9eca20 Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 05:34:46 +0000 Subject: [PATCH] ocaml: phase 5.1 min_jumps.ml baseline (greedy BFS-like min jumps = 4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Greedy BFS-frontier style — track the farthest reach within the current jump's reachable range, and bump the jump counter when i runs into the current frontier: while !i < n - 1 do farthest := max(farthest, i + arr.(i)); if !i = !cur_end then begin jumps := !jumps + 1; cur_end := !farthest end; i := !i + 1 done For [2; 3; 1; 1; 2; 4; 2; 0; 1; 1] (n = 10), the optimal jump sequence 0 -> 1 -> 4 -> 5 -> 9 uses 4 jumps. Tests greedy-with-frontier pattern, three parallel refs (jumps, cur_end, farthest), mixed for-style index loop using ref. 201 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/min_jumps.ml | 22 ++++++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 31 insertions(+) create mode 100644 lib/ocaml/baseline/min_jumps.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index ed41cf21..255be66a 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -129,6 +129,7 @@ "merge_sort.ml": 44, "merge_two.ml": 441, "min_cost_path.ml": 12, + "min_jumps.ml": 4, "min_subarr_target.ml": 2, "module_use.ml": 3, "monotonic.ml": 4, diff --git a/lib/ocaml/baseline/min_jumps.ml b/lib/ocaml/baseline/min_jumps.ml new file mode 100644 index 00000000..5bbef99a --- /dev/null +++ b/lib/ocaml/baseline/min_jumps.ml @@ -0,0 +1,22 @@ +let min_jumps arr = + let n = Array.length arr in + if n <= 1 then 0 + else begin + let jumps = ref 0 in + let cur_end = ref 0 in + let farthest = ref 0 in + let i = ref 0 in + while !i < n - 1 do + if !i + arr.(!i) > !farthest then farthest := !i + arr.(!i); + if !i = !cur_end then begin + jumps := !jumps + 1; + cur_end := !farthest + end; + i := !i + 1 + done; + !jumps + end + +;; + +min_jumps [| 2; 3; 1; 1; 2; 4; 2; 0; 1; 1 |] diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 5a5fe298..2f2ff25a 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-11 Phase 5.1 — min_jumps.ml baseline (greedy BFS-like + min jumps to reach end of [2;3;1;1;2;4;2;0;1;1] = 4). At each + position track the farthest reach within the current "BFS + layer"; when i reaches the layer end, bump jumps and extend to + farthest. Optimal jump sequence 0→1→4→5→9 = 4 jumps. Tests + greedy-with-frontier idiom, three parallel refs (jumps, cur_end, + farthest), mixed for-style index loop using ref. 201 baseline + programs total. - 2026-05-11 Phase 5.1 — combinations.ml baseline (C(9, 4) = 126 enumerated). Pascal-style recursive split: with first element h, combinations either include h (recurse with k−1 on rest) or