From 72fb5738c5891974abacb3149bdd7e9db2cf6e70 Mon Sep 17 00:00:00 2001 From: Tomasz Chojnacki Date: Sun, 4 Dec 2022 11:36:08 +0100 Subject: Finish day 4 --- aoc-2022-dotnet/Day04/Day04.fsproj | 22 ++++++++++++++++++++++ aoc-2022-dotnet/Day04/Program.fs | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 aoc-2022-dotnet/Day04/Day04.fsproj create mode 100644 aoc-2022-dotnet/Day04/Program.fs (limited to 'aoc-2022-dotnet/Day04') 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 @@ + + + + Exe + net7.0 + + + + + Always + + + Always + + + + + + + + + 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 -- cgit v1.2.3