diff options
-rw-r--r-- | day17.fsx | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/day17.fsx b/day17.fsx new file mode 100644 index 0000000..379d0d0 --- /dev/null +++ b/day17.fsx @@ -0,0 +1,29 @@ +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<string>() + +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" \ No newline at end of file |