Post

Advent of OCaml, day 5 done Whew. Getting the details right on this one took forever. Happy that the naive brute force solution (just construct a set in memory of the ingredients) couldn't be done and it required a bit more thinking.

At the risk of getting myself excommunicated from the OCaml community, day 5 is an example where imperative solution is easier to grok because there's no ready-made higher order function for the pattern you're employing. That is, IMO, this imperative C++ solution is clearer 🦆

1 repost4 likes

The humble fold: Array.sort compare ranges ; Array.fold_left (fun a (min1,max1) -> match a with [] -> (min1,max1)::a | (min0,max0)::tl -> if min1 > max0 + 1 then (min1,max1)::a else (min0, max max0 max1)::tl) [] ranges |> List.fold_left (fun a (rmin,rmax) -> a + rmax - rmin + 1) 0

I had a similar merge to you - I agree the stack is definitely easier to grok... I feel that way about a lot of the problems where a 'work queue' approach makes sense. Even the rolls one, I wanted to be able to enqueue any that became "accessible" when a cell was flipped, but I did passes instead

right, we can write imperative code in ocaml that's just... not what we're here for :P also I just realized the deque/dynarray isn't even needed, could just be incrementing an index into the sorted ranges (as a regular array/vector) /facepalm