diff options
Diffstat (limited to 'aoc-2022-dotnet/Day03')
-rw-r--r-- | aoc-2022-dotnet/Day03/Day03.fsproj | 18 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day03/Program.fs | 41 |
2 files changed, 59 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Day03/Day03.fsproj b/aoc-2022-dotnet/Day03/Day03.fsproj new file mode 100644 index 0000000..e337a41 --- /dev/null +++ b/aoc-2022-dotnet/Day03/Day03.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/Day03/Program.fs b/aoc-2022-dotnet/Day03/Program.fs new file mode 100644 index 0000000..1bf2235 --- /dev/null +++ b/aoc-2022-dotnet/Day03/Program.fs @@ -0,0 +1,41 @@ +open System.IO + +let priority item = + if 'a' <= item && item <= 'z' then + int item - int 'a' + 1 + elif 'A' <= item && item <= 'Z' then + int item - int 'A' + 27 + else + failwithf "Invalid item: %c" item + +let cutInHalf xs = + let half = Seq.length xs / 2 + [ Seq.take half xs; Seq.skip half xs ] + +let solution1 input = + input + |> Seq.sumBy ( + cutInHalf + >> Seq.map Set + >> Set.intersectMany + >> Seq.exactlyOne + >> priority + ) + +let solution2 input = + input + |> Seq.chunkBySize 3 + |> Seq.sumBy ( + Seq.map Set + >> Set.intersectMany + >> Seq.exactlyOne + >> priority + ) + +let test = File.ReadLines "test.txt" +assert (solution1 test = 157) +assert (solution2 test = 70) + +let input = File.ReadAllLines "input.txt" +printfn "%d" <| solution1 input +printfn "%d" <| solution2 input |