aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Common
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-09 19:31:24 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-09 19:31:24 +0100
commit967e964f453fc031816270b90d6eab38410769a3 (patch)
tree99ca5504c1be629eb35cda6514df401965d232fd /aoc-2022-dotnet/Common
parentfd04f2ebd1a4ade35a3e218b7737311ac631fce8 (diff)
downloadgleam_aoc2020-967e964f453fc031816270b90d6eab38410769a3.tar.gz
gleam_aoc2020-967e964f453fc031816270b90d6eab38410769a3.zip
Extract common functions to util module
Diffstat (limited to 'aoc-2022-dotnet/Common')
-rw-r--r--aoc-2022-dotnet/Common/Common.fsproj4
-rw-r--r--aoc-2022-dotnet/Common/Library.fs8
-rw-r--r--aoc-2022-dotnet/Common/Util.fs43
-rw-r--r--aoc-2022-dotnet/Common/Vec2.fs13
4 files changed, 59 insertions, 9 deletions
diff --git a/aoc-2022-dotnet/Common/Common.fsproj b/aoc-2022-dotnet/Common/Common.fsproj
index c8a185a..32057cf 100644
--- a/aoc-2022-dotnet/Common/Common.fsproj
+++ b/aoc-2022-dotnet/Common/Common.fsproj
@@ -6,11 +6,13 @@
</PropertyGroup>
<ItemGroup>
- <Compile Include="Library.fs" />
+ <Compile Include="Vec2.fs" />
+ <Compile Include="Util.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FParsec" Version="1.1.1" />
+ <PackageReference Include="FSharpPlus" Version="1.3.2" />
</ItemGroup>
</Project>
diff --git a/aoc-2022-dotnet/Common/Library.fs b/aoc-2022-dotnet/Common/Library.fs
deleted file mode 100644
index 7354509..0000000
--- a/aoc-2022-dotnet/Common/Library.fs
+++ /dev/null
@@ -1,8 +0,0 @@
-module Common
-
-open FParsec
-
-let parse parser input =
- match run parser input with
- | Success (result, _, _) -> result
- | _ -> failwith "Invalid input format!"
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
diff --git a/aoc-2022-dotnet/Common/Vec2.fs b/aoc-2022-dotnet/Common/Vec2.fs
new file mode 100644
index 0000000..20b9956
--- /dev/null
+++ b/aoc-2022-dotnet/Common/Vec2.fs
@@ -0,0 +1,13 @@
+namespace Common
+
+type Vec2 =
+ | Vec2 of int * int
+
+ static member zero = Vec2(0, 0)
+
+ static member (+)(Vec2 (x1, y1), Vec2 (x2, y2)) = Vec2(x1 + x2, y1 + y2)
+ static member (-)(Vec2 (x1, y1), Vec2 (x2, y2)) = Vec2(x1 - x2, y1 - y2)
+
+ static member apply f (Vec2 (x, y)) = Vec2(f x, f y)
+ static member sign = Vec2.apply sign
+ static member chebyshevDist (Vec2 (x1, y1)) (Vec2 (x2, y2)) = max (abs <| x2 - x1) (abs <| y2 - y1)