From 3ccce58e0a207a570050b3583c609633688104b7 Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 05:14:47 +0000 Subject: [PATCH] ocaml: phase 5.1 unique_paths_obs.ml baseline (4x4 grid w/ obstacles = 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Standard 2D unique-paths DP with obstacles gating each cell: dp[i][j] = if grid[i][j] = 1 then 0 else dp[i-1][j] + dp[i][j-1] Grid (1s are obstacles): . . . . . # . . . . . # # . . . dp: 1 1 1 1 1 0 1 2 1 1 2 0 0 1 3 3 Returns dp[3][3] = 3. Complements grid_paths.ml (no-obstacles version) — same DP shape but obstacles zero out cells and reshape the path count. 199 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/unique_paths_obs.ml | 28 ++++++++++++++++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 35 insertions(+) create mode 100644 lib/ocaml/baseline/unique_paths_obs.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 33932e5d..025653bd 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -196,6 +196,7 @@ "triangle_div.ml": 120, "twosum.ml": 5, "union_find.ml": 4, + "unique_paths_obs.ml": 3, "unique_set.ml": 9, "validate.ml": 417, "word_count.ml": 3 diff --git a/lib/ocaml/baseline/unique_paths_obs.ml b/lib/ocaml/baseline/unique_paths_obs.ml new file mode 100644 index 00000000..1a3db496 --- /dev/null +++ b/lib/ocaml/baseline/unique_paths_obs.ml @@ -0,0 +1,28 @@ +let h = 4 +let w = 4 +let grid = [| + [| 0; 0; 0; 0 |]; + [| 0; 1; 0; 0 |]; + [| 0; 0; 0; 1 |]; + [| 1; 0; 0; 0 |] +|] + +let paths_with_obs () = + let dp = Array.init h (fun _ -> Array.make w 0) in + if grid.(0).(0) = 0 then dp.(0).(0) <- 1; + for j = 1 to w - 1 do + if grid.(0).(j) = 0 then dp.(0).(j) <- dp.(0).(j - 1) + done; + for i = 1 to h - 1 do + if grid.(i).(0) = 0 then dp.(i).(0) <- dp.(i - 1).(0) + done; + for i = 1 to h - 1 do + for j = 1 to w - 1 do + if grid.(i).(j) = 0 then + dp.(i).(j) <- dp.(i - 1).(j) + dp.(i).(j - 1) + done + done; + dp.(h - 1).(w - 1) +;; + +paths_with_obs () diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index defa645b..50b4dedc 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _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 — unique_paths_obs.ml baseline (count + monotone paths in 4×4 grid with obstacles at (1,1),(2,3),(3,0) + = 3). Standard 2D DP with obstacle gating: dp[i][j] = dp[i-1][j] + + dp[i][j-1] when grid[i][j]=0, else 0. Complements grid_paths.ml + (no-obstacles version): the same DP but obstacles zero out + cells, reshaping the path count. 199 baseline programs total. - 2026-05-11 Phase 5.1 — daily_temperatures.ml baseline (monotonic decreasing stack of waiting days until warmer; sum over [73;74;75;71;69;72;76;73] = 10). For each day i, pop all stack