diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-28 19:32:40 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-28 19:32:40 +0100 |
commit | 8e71f049f5ffce904fcd477dbb019e794391c595 (patch) | |
tree | 15fbd4238cc1677dd08fd6fe3e6f29bbcd8b55b8 /aoc-2022-dotnet/Common | |
parent | 68381d1c8bdd3d74f0924a47cf72809e40b34708 (diff) | |
download | gleam_aoc2020-8e71f049f5ffce904fcd477dbb019e794391c595.tar.gz gleam_aoc2020-8e71f049f5ffce904fcd477dbb019e794391c595.zip |
Finish part 1 of day 21
Diffstat (limited to 'aoc-2022-dotnet/Common')
-rw-r--r-- | aoc-2022-dotnet/Common/Util.fs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Common/Util.fs b/aoc-2022-dotnet/Common/Util.fs index 568f2c2..ac5d981 100644 --- a/aoc-2022-dotnet/Common/Util.fs +++ b/aoc-2022-dotnet/Common/Util.fs @@ -65,6 +65,21 @@ module Util = | h :: t -> h, t @ [ h ] | [] -> failwith "Empty list!" + let tsort graph = + let dfs = + let rec explore path result node = + if List.contains node path then + failwith "Cycle found!" + elif List.contains node result then + result + else + node + :: List.fold (explore (node :: path)) result (Map.find node graph) + + explore [] + + graph |> Map.keys |> Seq.fold dfs [] |> List.rev + let topN n xs = Seq.fold (fun acc x -> |