From ff30ea3a9cde461aa8062a59457b3f17bfac38a6 Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Fri, 7 Jan 2022 23:26:05 -0800 Subject: solution for day 17 --- day17.fsx | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 day17.fsx 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() + +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 -- cgit 1.4.1-2-gfad0