diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-08 16:00:49 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-08 16:00:49 +0100 |
commit | 0557a772d6664339aecec85fefcbf034d82209cb (patch) | |
tree | ab70cce9318081e62962cec91475c3f92c6ba514 /aoc-2022-dotnet | |
parent | 8d9513e9df67c5ec0abc2d94152f350274233643 (diff) | |
download | gleam_aoc2020-0557a772d6664339aecec85fefcbf034d82209cb.tar.gz gleam_aoc2020-0557a772d6664339aecec85fefcbf034d82209cb.zip |
Finish day 8
Diffstat (limited to 'aoc-2022-dotnet')
-rw-r--r-- | aoc-2022-dotnet/Day08/Day08.fsproj | 18 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day08/Program.fs | 49 | ||||
-rw-r--r-- | aoc-2022-dotnet/README.md | 6 | ||||
-rw-r--r-- | aoc-2022-dotnet/aoc-2022-dotnet.sln | 8 |
4 files changed, 77 insertions, 4 deletions
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 @@ +ο»Ώ<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net7.0</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <Content Include="test.txt"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </Content> + <Content Include="input.txt"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </Content> + <Compile Include="Program.fs" /> + </ItemGroup> + +</Project> 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 diff --git a/aoc-2022-dotnet/README.md b/aoc-2022-dotnet/README.md index 7facc6a..9ab58da 100644 --- a/aoc-2022-dotnet/README.md +++ b/aoc-2022-dotnet/README.md @@ -1,6 +1,6 @@ # Advent of Code 2022 in .NET  - + ## Progress | Day | Part 1 | Part 2 | @@ -11,8 +11,8 @@ | Day 4: Camp Cleanup | π | π | | Day 5: Supply Stacks | π | π | | Day 6: Tuning Trouble | π | π | -| Day 7: ??? | π | π | -| Day 8: ??? | | | +| Day 7: No Space Left On Device | π | π | +| Day 8: Treetop Tree House | π | π | | Day 9: ??? | | | | Day 10: ??? | | | | Day 11: ??? | | | diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln index 0a2fc9a..6b9452d 100644 --- a/aoc-2022-dotnet/aoc-2022-dotnet.sln +++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln @@ -24,7 +24,9 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day06", "Day06\Day06.fsproj EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day07", "Day07\Day07.fsproj", "{5816A6D4-F70A-419E-AB40-C41C0AB5364F}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Common", "Common\Common.fsproj", "{3148A67C-BDDB-4660-83E6-C2C6D6016A47}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Common", "Common\Common.fsproj", "{3148A67C-BDDB-4660-83E6-C2C6D6016A47}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day08", "Day08\Day08.fsproj", "{F33E64B7-EC50-4017-B735-C8900684BF0D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -64,6 +66,10 @@ Global {3148A67C-BDDB-4660-83E6-C2C6D6016A47}.Debug|Any CPU.Build.0 = Debug|Any CPU {3148A67C-BDDB-4660-83E6-C2C6D6016A47}.Release|Any CPU.ActiveCfg = Release|Any CPU {3148A67C-BDDB-4660-83E6-C2C6D6016A47}.Release|Any CPU.Build.0 = Release|Any CPU + {F33E64B7-EC50-4017-B735-C8900684BF0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F33E64B7-EC50-4017-B735-C8900684BF0D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F33E64B7-EC50-4017-B735-C8900684BF0D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F33E64B7-EC50-4017-B735-C8900684BF0D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE |