aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Common/Util.fs
diff options
context:
space:
mode:
Diffstat (limited to 'aoc-2022-dotnet/Common/Util.fs')
-rw-r--r--aoc-2022-dotnet/Common/Util.fs15
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 ->