aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Day04
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-04 11:36:08 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-04 11:36:08 +0100
commit72fb5738c5891974abacb3149bdd7e9db2cf6e70 (patch)
tree1dbecf32ed450163221264bb777e9e27a75ea837 /aoc-2022-dotnet/Day04
parentbcbf17aa7eb192508ac94287e4c6b9a11e998c8a (diff)
downloadgleam_aoc2020-72fb5738c5891974abacb3149bdd7e9db2cf6e70.tar.gz
gleam_aoc2020-72fb5738c5891974abacb3149bdd7e9db2cf6e70.zip
Finish day 4
Diffstat (limited to 'aoc-2022-dotnet/Day04')
-rw-r--r--aoc-2022-dotnet/Day04/Day04.fsproj22
-rw-r--r--aoc-2022-dotnet/Day04/Program.fs28
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