aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Day08/Program.fs
diff options
context:
space:
mode:
Diffstat (limited to 'aoc-2022-dotnet/Day08/Program.fs')
-rw-r--r--aoc-2022-dotnet/Day08/Program.fs25
1 files changed, 11 insertions, 14 deletions
diff --git a/aoc-2022-dotnet/Day08/Program.fs b/aoc-2022-dotnet/Day08/Program.fs
index 23f93c0..881ac72 100644
--- a/aoc-2022-dotnet/Day08/Program.fs
+++ b/aoc-2022-dotnet/Day08/Program.fs
@@ -5,37 +5,34 @@ open Common
let parseMatrix = array2D >> Array2D.map Util.charToInt
-let mapEachToSeq mapping m =
- seq {
- for r in 0 .. Array2D.length1 m - 1 do
- for c in 0 .. Array2D.length2 m - 1 -> mapping m r c
- }
-
-let sideViews (m: 'a [,]) r c =
+let sideViews (m: 'a [,]) (Vec2 (c, r)) =
[ m[0 .. r - 1, c] |> Array.rev
m[r, c + 1 ..]
m[r + 1 .., c]
m[r, 0 .. c - 1] |> Array.rev ]
-let isVisible (m: 'a [,]) r c =
+let isVisible (matrix: 'a [,]) pos height =
not
- <| List.forall (Array.exists ((<=) m[r, c])) (sideViews m r c)
+ <| List.forall (Array.exists ((<=) height)) (sideViews matrix pos)
-let scenicScore (m: 'a [,]) r c =
- sideViews m r c
+let scenicScore (matrix: 'a [,]) pos height =
+ sideViews matrix pos
|> List.map (fun s ->
s
- |> Seq.tryFindIndex ((<=) m[r, c])
+ |> Seq.tryFindIndex ((<=) height)
|> Option.map ((+) 1)
|> Option.defaultValue (Array.length s))
|> List.reduce (*)
let solution1 =
parseMatrix
- >> mapEachToSeq isVisible
+ >> Util.mapEachToSeq isVisible
>> Util.countWhere id
-let solution2 = parseMatrix >> mapEachToSeq scenicScore >> Seq.max
+let solution2 =
+ parseMatrix
+ >> Util.mapEachToSeq scenicScore
+ >> Seq.max
let test = File.ReadLines("test.txt")
assert (solution1 test = 21)