summary refs log tree commit diff stats
path: root/day20.fsx
blob: 595a1eb57d3a6e0160b77eeccf91fe7ec56bb135 (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
open System.IO

let lines = File.ReadAllLines "day20.txt"
            |> Array.map (fun x -> x.Split "-")
            |> Array.map (fun x -> (int64 x[0], int64 x[1]))
            |> Array.sortBy fst
            |> List.ofArray

let rec minIP  blocked inputs=
    match inputs with
    | (lo, hi)::rest ->
        if lo > blocked + 1L then blocked + 1L
        else minIP (max blocked hi) rest
    | _ -> 0L

let getAllowed blockedRanges =
    let rec mergeRanges n ranges = seq {
        match ranges with
        | [] -> yield (n,4294967295L)
        | (lo,hi)::tail ->
            if n < lo then yield (n,lo-1L)
            yield! mergeRanges (max n (hi+1L)) tail
    }
    mergeRanges 0L blockedRanges

minIP 0L lines |> printfn "%A"
getAllowed lines |> Seq.sumBy (fun (lo, hi) -> hi - lo + 1L) |> printfn "%d"