open System.Security.Cryptography open System.Text open System.Collections.Generic let md5 (data: string): string = use md5 = MD5.Create() (StringBuilder (), md5.ComputeHash(Encoding.ASCII.GetBytes data)) ||> Array.fold (fun sb b -> sb.Append(b.ToString("x2"))) |> string let input = "pgflpeqp" let openDoors = "bcdef" let solutions = new List() let rec explore str x y count = if x = 3 && y = 3 then solutions.Add(str) else let doors = (md5 str)[0..3] if y > 0 && openDoors.Contains doors[0] then explore (str + "U") x (y-1) (count + 1) if y < 3 && openDoors.Contains doors[1] then explore (str + "D") x (y+1) (count + 1) if x > 0 && openDoors.Contains doors[2] then explore (str + "L") (x-1) y (count + 1) if x < 3 && openDoors.Contains doors[3] then explore (str + "R") (x+1) y (count + 1) explore input 0 0 0 let sorted = solutions.ToArray() |> Array.sortBy (fun x -> x.Length) Seq.head sorted |> printfn "%s" Seq.last sorted |> fun x -> x[8..] |> String.length |> printfn "%d"