From 0557a772d6664339aecec85fefcbf034d82209cb Mon Sep 17 00:00:00 2001 From: Tomasz Chojnacki Date: Thu, 8 Dec 2022 16:00:49 +0100 Subject: Finish day 8 --- aoc-2022-dotnet/Day08/Day08.fsproj | 18 ++++++++++++++ aoc-2022-dotnet/Day08/Program.fs | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 aoc-2022-dotnet/Day08/Day08.fsproj create mode 100644 aoc-2022-dotnet/Day08/Program.fs (limited to 'aoc-2022-dotnet/Day08') diff --git a/aoc-2022-dotnet/Day08/Day08.fsproj b/aoc-2022-dotnet/Day08/Day08.fsproj new file mode 100644 index 0000000..e337a41 --- /dev/null +++ b/aoc-2022-dotnet/Day08/Day08.fsproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + + + + + Always + + + Always + + + + + diff --git a/aoc-2022-dotnet/Day08/Program.fs b/aoc-2022-dotnet/Day08/Program.fs new file mode 100644 index 0000000..1a98c6d --- /dev/null +++ b/aoc-2022-dotnet/Day08/Program.fs @@ -0,0 +1,49 @@ +module Day08 + +open System.IO +open System.Globalization + +let parseMatrix = + array2D + >> Array2D.map CharUnicodeInfo.GetDigitValue + +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 = + [ 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 = + not + <| List.forall (Array.exists ((<=) m[r, c])) (sideViews m r c) + +let scenicScore (m: 'a [,]) r c = + sideViews m r c + |> List.map (fun s -> + s + |> Seq.tryFindIndex ((<=) m[r, c]) + |> Option.map ((+) 1) + |> Option.defaultValue (Array.length s)) + |> List.reduce (*) + +let solution1 = + parseMatrix + >> mapEachToSeq isVisible + >> Seq.filter id + >> Seq.length + +let solution2 = parseMatrix >> mapEachToSeq scenicScore >> Seq.max + +let test = File.ReadLines("test.txt") +assert (solution1 test = 21) +assert (solution2 test = 8) + +let input = File.ReadLines("input.txt") +printfn "%d" <| solution1 input +printfn "%d" <| solution2 input -- cgit v1.2.3