aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Day04/Program.fs
blob: 25cc3b63fc966dd8d9d71d5126e611e8ef19b164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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