diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-21 11:45:04 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-21 11:45:04 +0100 |
commit | 9e378a9f80260c2f729daaf83d8e74f7bad70b16 (patch) | |
tree | db61e67a7222cd0666ea7fe4976e2fded327599a /aoc-2022-dotnet/Common | |
parent | a8c844a12fa2d91410fda7b37f08c58f5be34ed9 (diff) | |
download | gleam_aoc2020-9e378a9f80260c2f729daaf83d8e74f7bad70b16.tar.gz gleam_aoc2020-9e378a9f80260c2f729daaf83d8e74f7bad70b16.zip |
Finish day 17
Diffstat (limited to 'aoc-2022-dotnet/Common')
-rw-r--r-- | aoc-2022-dotnet/Common/Util.fs | 16 | ||||
-rw-r--r-- | aoc-2022-dotnet/Common/Vec2.fs | 13 |
2 files changed, 28 insertions, 1 deletions
diff --git a/aoc-2022-dotnet/Common/Util.fs b/aoc-2022-dotnet/Common/Util.fs index acf89af..568f2c2 100644 --- a/aoc-2022-dotnet/Common/Util.fs +++ b/aoc-2022-dotnet/Common/Util.fs @@ -38,7 +38,11 @@ module Util = let mAt matrix (Vec2 (col, row)) = Array2D.get matrix row col - let composition n f = List.replicate n f |> List.reduce (>>) + let rec composition n f x = + if n = 0 then + x + else + composition (n - 1) f (f x) let notIn set element = not <| Set.contains element set @@ -51,6 +55,16 @@ module Util = | h :: t -> min h x :: (insertSorted (max h x) t) | [] -> [ x ] + let liftList xs = + match List.forall Option.isSome xs with + | true -> Some(List.map Option.get xs) + | false -> None + + let cycle = + function + | h :: t -> h, t @ [ h ] + | [] -> failwith "Empty list!" + let topN n xs = Seq.fold (fun acc x -> diff --git a/aoc-2022-dotnet/Common/Vec2.fs b/aoc-2022-dotnet/Common/Vec2.fs index 8835a5a..61b313d 100644 --- a/aoc-2022-dotnet/Common/Vec2.fs +++ b/aoc-2022-dotnet/Common/Vec2.fs @@ -13,6 +13,8 @@ type Vec2 = static member down = Vec2(0, -1) static member left = Vec2(-1, 0) + static member upRight = Vec2(1, 1) + static member upLeft = Vec2(-1, 1) static member downLeft = Vec2(-1, -1) static member downRight = Vec2(1, -1) @@ -22,6 +24,16 @@ type Vec2 = Vec2.down Vec2.left ] + static member directions8 = + [ Vec2.up + Vec2.upRight + Vec2.right + Vec2.downRight + Vec2.down + Vec2.downLeft + Vec2.left + Vec2.upLeft ] + static member inline (~-) = Vec2.map (~-) static member inline (+)(Vec2 (x1, y1), Vec2 (x2, y2)) = Vec2(x1 + x2, y1 + y2) static member inline (-)(v1, v2) = v1 + Vec2.op_UnaryNegation (v2) @@ -34,6 +46,7 @@ type Vec2 = 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 neighbours8 v = List.map ((+) v) Vec2.directions8 static member lineBetween(Vec2 (x1, y1), Vec2 (x2, y2)) = if x1 = x2 then |