diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-09 19:31:24 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-09 19:31:24 +0100 |
commit | 967e964f453fc031816270b90d6eab38410769a3 (patch) | |
tree | 99ca5504c1be629eb35cda6514df401965d232fd /aoc-2022-dotnet/Common/Util.fs | |
parent | fd04f2ebd1a4ade35a3e218b7737311ac631fce8 (diff) | |
download | gleam_aoc2020-967e964f453fc031816270b90d6eab38410769a3.tar.gz gleam_aoc2020-967e964f453fc031816270b90d6eab38410769a3.zip |
Extract common functions to util module
Diffstat (limited to 'aoc-2022-dotnet/Common/Util.fs')
-rw-r--r-- | aoc-2022-dotnet/Common/Util.fs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Common/Util.fs b/aoc-2022-dotnet/Common/Util.fs new file mode 100644 index 0000000..a9f3a1e --- /dev/null +++ b/aoc-2022-dotnet/Common/Util.fs @@ -0,0 +1,43 @@ +namespace Common + +module Util = + open System.Globalization + open FParsec + open FSharpPlus + + let parse parser input = + match run parser input with + | Success (result, _, _) -> result + | _ -> failwith "Invalid input format!" + + let countDistinct seq = seq |> Set |> Set.count + + let countWhere pred = Seq.filter pred >> Seq.length + + let charToInt = CharUnicodeInfo.GetDigitValue + + let cutInHalf xs = + let half = Seq.length xs / 2 + [ Seq.take half xs; Seq.skip half xs ] + + let splitStringToTuple sep str = + match Seq.toList <| String.split [ sep ] str with + | [ x; y ] -> x, y + | _ -> failwith "Invalid string format!" + + let topN n xs = + let rec insertSorted x = + function + | h :: t -> min h x :: (insertSorted (max h x) t) + | _ -> [ x ] + + Seq.fold + (fun acc x -> + if List.length acc < n then + insertSorted x acc + elif List.head acc < x then + insertSorted x <| List.tail acc + else + acc) + List.empty + xs |