From 32f6c4ee0c6108b9bbb8b5896d810cadfd7c94df Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 22:58:13 +0000 Subject: [PATCH] ocaml: phase 5.1 egg_drop.ml baseline (2 eggs, 36 floors -> 8 trials) Classic egg-drop puzzle DP: dp[e][f] = 1 + min over k in [1, f] of max(dp[e-1][k-1], dp[e][f-k]) For 2 eggs over 36 floors, the optimal worst-case is 8 trials (closed form: triangular number bound). Tests 2D DP with triple-nested for-loops, max-of-two via inline if, large sentinel constant (100000000), mixed shifted indexing (e-1) and (f-k) where both shift independently. 163 baseline programs total. --- lib/ocaml/baseline/egg_drop.ml | 26 ++++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 35 insertions(+) create mode 100644 lib/ocaml/baseline/egg_drop.ml diff --git a/lib/ocaml/baseline/egg_drop.ml b/lib/ocaml/baseline/egg_drop.ml new file mode 100644 index 00000000..756300ec --- /dev/null +++ b/lib/ocaml/baseline/egg_drop.ml @@ -0,0 +1,26 @@ +let egg_drop eggs floors = + let dp = Array.init (eggs + 1) (fun _ -> Array.make (floors + 1) 0) in + for f = 1 to floors do + dp.(1).(f) <- f + done; + for e = 1 to eggs do + dp.(e).(0) <- 0; + dp.(e).(1) <- 1 + done; + for e = 2 to eggs do + for f = 2 to floors do + let best = ref 100000000 in + for k = 1 to f do + let bre = dp.(e - 1).(k - 1) in + let sur = dp.(e).(f - k) in + let cand = 1 + (if bre > sur then bre else sur) in + if cand < !best then best := cand + done; + dp.(e).(f) <- !best + done + done; + dp.(eggs).(floors) + +;; + +egg_drop 2 36 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 4dc27692..84cee1ff 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -29,6 +29,7 @@ "count_change.ml": 406, "count_inversions.ml": 12, "csv.ml": 10, + "egg_drop.ml": 8, "dijkstra.ml": 7, "exception_handle.ml": 4, "exception_user.ml": 26, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index b607b3c5..c49c7565 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 — egg_drop.ml baseline (worst-case trials + for 2 eggs, 36 floors = 8). Classic O(e·f²) DP: dp[e][f] = 1 + + min over k of max(dp[e-1][k-1], dp[e][f-k]). Closed form via + triangular numbers gives ⌈(√(1+8·36)−1)/2⌉ = 8, matching the + DP answer. Tests 2D DP with triple-nested for-loops, max-of-two + via inline if, large sentinel constant, mixed indexing (e-1) + and (f-k) where both shift independently. 163 baseline programs + total. - 2026-05-10 Phase 5.1 — polygon_area.ml baseline (shoelace formula on pentagon, returns 2× area = 32). Vertices (0,0), (4,0), (4,3), (2,5), (0,3); shoelace sum |Σ(x_i·y_{i+1} − x_{i+1}·y_i)| = 32 so