aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Common
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-13 16:38:41 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-13 16:38:41 +0100
commit6fc820cd0cb61a4bacda5f5b40c98dda850d75da (patch)
tree7d08b0adbf8c1b60be906cab3ee7b76767b6908e /aoc-2022-dotnet/Common
parenteb7cdaab9b9c13b3594e0abdbbff6f72f8cb4807 (diff)
downloadgleam_aoc2020-6fc820cd0cb61a4bacda5f5b40c98dda850d75da.tar.gz
gleam_aoc2020-6fc820cd0cb61a4bacda5f5b40c98dda850d75da.zip
Upload days 12 and 13
Diffstat (limited to 'aoc-2022-dotnet/Common')
-rw-r--r--aoc-2022-dotnet/Common/Util.fs18
-rw-r--r--aoc-2022-dotnet/Common/Vec2.fs20
2 files changed, 32 insertions, 6 deletions
diff --git a/aoc-2022-dotnet/Common/Util.fs b/aoc-2022-dotnet/Common/Util.fs
index 34bf8ff..8505f58 100644
--- a/aoc-2022-dotnet/Common/Util.fs
+++ b/aoc-2022-dotnet/Common/Util.fs
@@ -10,7 +10,7 @@ module Util =
| Success (result, _, _) -> result
| _ -> failwith "Invalid input format!"
- let countDistinct seq = seq |> Set |> Set.count
+ let countDistinct xs = xs |> Set |> Set.count
let countWhere pred = Seq.filter pred >> Seq.length
@@ -20,16 +20,24 @@ module Util =
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
+ let splitStringToTuple sep string =
+ match Seq.toList <| String.split [ sep ] string with
| [ x; y ] -> x, y
| _ -> failwith "Invalid string format!"
- let matrixToString m =
- m
+ let matrixToString matrix =
+ matrix
|> Seq.map (Seq.map string >> String.concat "")
|> String.concat "\n"
+ let mapEachToSeq mapping matrix =
+ seq {
+ for row in 0 .. Array2D.length1 matrix - 1 do
+ for col in 0 .. Array2D.length2 matrix - 1 -> mapping matrix (Vec2(col, row)) matrix[row, col]
+ }
+
+ let mAt matrix (Vec2 (col, row)) = Array2D.get matrix row col
+
let composition n f = List.replicate n f |> List.reduce (>>)
let topN n xs =
diff --git a/aoc-2022-dotnet/Common/Vec2.fs b/aoc-2022-dotnet/Common/Vec2.fs
index bbe3ef2..11c94aa 100644
--- a/aoc-2022-dotnet/Common/Vec2.fs
+++ b/aoc-2022-dotnet/Common/Vec2.fs
@@ -4,6 +4,16 @@ type Vec2 =
| Vec2 of int * int
static member zero = Vec2(0, 0)
+ static member up = Vec2(0, 1)
+ static member right = Vec2(1, 0)
+ static member down = Vec2(0, -1)
+ static member left = Vec2(-1, 0)
+
+ static member directions4 =
+ [ Vec2.up
+ Vec2.right
+ Vec2.down
+ Vec2.left ]
static member inline (~-) = Vec2.map (~-)
static member inline (+)(Vec2 (x1, y1), Vec2 (x2, y2)) = Vec2(x1 + x2, y1 + y2)
@@ -11,9 +21,17 @@ type Vec2 =
static member inline (*)(v1, k) = Vec2.map ((*) k) v1
static member inline dot (Vec2 (x1, y1)) (Vec2 (x2, y2)) = x1 * x2 + y1 * y2
static member inline cross (Vec2 (x1, y1)) (Vec2 (x2, y2)) = x1 * y2 - y1 * x2
-
static member map f (Vec2 (x, y)) = Vec2(f x, f y)
static member inline sign = Vec2.map sign
static member inline lengthSquared(Vec2 (x, y)) = x * x + y * y
static member mahattanDist (Vec2 (x1, y1)) (Vec2 (x2, y2)) = abs (x2 - x1) + abs (y2 - y1)
static member chebyshevDist (Vec2 (x1, y1)) (Vec2 (x2, y2)) = max (abs <| x2 - x1) (abs <| y2 - y1)
+ static member neighbours4 v = List.map ((+) v) Vec2.directions4
+
+ static member inMatrix matrix (Vec2 (col, row)) =
+ col >= 0
+ && col < Array2D.length2 matrix
+ && row >= 0
+ && row < Array2D.length1 matrix
+
+ static member toIndexOf matrix (Vec2 (col, row)) = (Array2D.length2 matrix) * row + col