33 lines
1.0 KiB
FSharp
33 lines
1.0 KiB
FSharp
module _79
|
|
|
|
open System.IO
|
|
|
|
let findPasscode =
|
|
let logins =
|
|
File.ReadAllLines(@"79_keylog.txt")
|
|
let preceeds =
|
|
logins
|
|
|> Seq.collect (fun login -> seq { yield (login.[0], login.[1]); yield (login.[1], login.[2]) })
|
|
|> Seq.distinct
|
|
|
|
let collectTuple (a,b) =
|
|
seq { yield a; yield b }
|
|
|
|
// topographical sort kinda
|
|
let rec orderGraph g =
|
|
seq {
|
|
if Seq.length g = 1 then
|
|
yield (collectTuple (Seq.nth 0 g)) |> Seq.toList
|
|
else
|
|
let allNodes = g |> Seq.collect collectTuple |> Seq.distinct |> Set.ofSeq
|
|
let sndNodes = g |> Seq.map snd |> Seq.distinct |> Set.ofSeq
|
|
let noPreceedingNodes = allNodes - sndNodes
|
|
for n in noPreceedingNodes do
|
|
let gMinusN = g |> Seq.filter (fun gi -> not(fst gi = n))
|
|
for s in orderGraph gMinusN do
|
|
yield n :: s
|
|
}
|
|
|
|
preceeds
|
|
|> orderGraph
|
|
|> Seq.toArray |