6 ms·
Here is my natural (without any extra effort thinking about speed) F# solution: (for calibration I have the C++, unchanged, times) | 217 | 255 | 206 | 0.5 s
by Dn_Ab 12y ago
Here is my natural (without any extra effort thinking about speed) F# solution: (for calibration I have the C++, unchanged, times)
| 217 | 255 | 206 | 0.5 s (C++ 1.6s)
| 317 | 355 | 306 | 1 s (C++ 4.8s)
| 617 | 655 | 606 | 3.5 s (C++ 35s)
| 917 | 955 | 906 | 10s (C++ 117.5)
code:
let actions = [|[|-1; -1; 1|]; [|-1;1;-1|]; [|1; -1;-1|]|]
let stable [| g ; w; l|] = (g = 0 && (w = 0 || l = 0)) || (w = 0 && l = 0)
let isSound = function | [| x ; _; _|] | [|_; x; _|] | [|_; _; x|] when x < 0 -> false | _ -> true
let stateChange state = Array.map (Array.map2 (+) state) actions
let deduplicate sequence = sequence |> Seq.groupBy hash |> Seq.map (snd >> Seq.head)
let forest start =
let rec search curforest =
let nextforest = Array.collect stateChange curforest
|> Array.filter isSound
|> deduplicate
|> Seq.toArray
if nextforest.Length = 0 then curforest
else match Array.tryFind stable nextforest with
| Some _ -> nextforest
| None -> search nextforest
search (stateChange start) |> Array.filter stable