summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-01-07 23:26:05 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-01-07 23:26:05 -0800
commitff30ea3a9cde461aa8062a59457b3f17bfac38a6 (patch)
tree670a0f07dd6c0987a4aea54d017f70cae8b8f3c6
parent32d788df81ea1d7b4b52ee8d97eff54c2134cf05 (diff)
downloadAdventOfCode2016-ff30ea3a9cde461aa8062a59457b3f17bfac38a6.tar.gz
solution for day 17
-rw-r--r--day17.fsx29
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