aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Common/Util.fs
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-28 19:32:40 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-28 19:32:40 +0100
commit8e71f049f5ffce904fcd477dbb019e794391c595 (patch)
tree15fbd4238cc1677dd08fd6fe3e6f29bbcd8b55b8 /aoc-2022-dotnet/Common/Util.fs
parent68381d1c8bdd3d74f0924a47cf72809e40b34708 (diff)
downloadgleam_aoc2020-8e71f049f5ffce904fcd477dbb019e794391c595.tar.gz
gleam_aoc2020-8e71f049f5ffce904fcd477dbb019e794391c595.zip
Finish part 1 of day 21
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 ->