diff options
Diffstat (limited to 'aoc-2022-dotnet/Day04')
-rw-r--r-- | aoc-2022-dotnet/Day04/Day04.fsproj | 22 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day04/Program.fs | 28 |
2 files changed, 50 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Day04/Day04.fsproj b/aoc-2022-dotnet/Day04/Day04.fsproj new file mode 100644 index 0000000..44c3fba --- /dev/null +++ b/aoc-2022-dotnet/Day04/Day04.fsproj @@ -0,0 +1,22 @@ +<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> + + <ItemGroup> + <PackageReference Include="FParsec" Version="1.1.1" /> + </ItemGroup> + +</Project> diff --git a/aoc-2022-dotnet/Day04/Program.fs b/aoc-2022-dotnet/Day04/Program.fs new file mode 100644 index 0000000..25cc3b6 --- /dev/null +++ b/aoc-2022-dotnet/Day04/Program.fs @@ -0,0 +1,28 @@ +module Day04 + +open System.IO +open FParsec + +let parseLine line = + let prange = pint32 .>> pstring "-" .>>. pint32 + let ppair = prange .>> pstring "," .>>. prange .>> eof + + match run ppair line with + | Success (result, _, _) -> result + | _ -> failwith "Invalid line format!" + +let fullyOverlap ((a, b), (c, d)) = + (a <= c && d <= b) || (c <= a && b <= d) + +let overlapAtAll ((a, b), (c, d)) = a <= d && b >= c + +let solution pred = + Seq.map parseLine >> Seq.filter pred >> Seq.length + +let test = File.ReadLines "test.txt" +assert (solution fullyOverlap test = 2) +assert (solution overlapAtAll test = 4) + +let input = File.ReadAllLines "input.txt" +printfn "%d" <| solution fullyOverlap input +printfn "%d" <| solution overlapAtAll input |