summary refs log tree commit diff stats
path: root/day17.fsx
blob: 379d0d07809e8736f8141d79398415240b8cb573 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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"