summary refs log tree commit diff stats
path: root/solutions/day4.fs
blob: 82b523c13b1df8d28fe5b35816a43ea0d7a5e31e (plain) (blame)
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
29
30
31
32
33
module Solutions.Day4
open System.IO
open System.Text.RegularExpressions

type range = {
    startID: int;
    endID: int
}

let getRanges input =
    let matches = Regex.Matches(input, "(\d+)-(\d+),(\d+)-(\d+)") 
                    |> Seq.head 
                    |> fun x -> x.Groups 
                    |> Seq.map (fun x -> x.Value) 
                    |> List.ofSeq 
                    |> List.tail
    assert (Seq.length matches = 4)
    {startID = int matches[0]; endID = int matches[1]}, {startID = int matches[2]; endID = int matches[3]}
    
let ranges = File.ReadLines("inputs/day4.txt") |> Seq.map getRanges

let contains (rangeA, rangeB) =
    (rangeA.startID <= rangeB.startID && rangeA.endID >= rangeB.endID) || (rangeA.startID >= rangeB.startID && rangeA.endID <= rangeB.endID)

let part1 () = ranges |> Seq.filter contains |> Seq.length

let overlaps (rangeA, rangeB) =
    (rangeA.startID >= rangeB.startID && rangeA.startID <= rangeB.endID) ||
    (rangeA.endID >= rangeB.startID && rangeA.endID <= rangeB.endID)     ||
    (rangeB.startID >= rangeA.startID && rangeB.startID <= rangeA.endID) ||
    (rangeB.endID >= rangeA.startID && rangeB.endID <= rangeA.endID)

let part2 () = ranges |> Seq.filter overlaps |> Seq.length