diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-01-08 12:00:10 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-01-08 12:03:44 -0800 |
commit | 50beae2167a705f694e4da4fa1ff63cfb1b2b16b (patch) | |
tree | 1799b7010a42c64dc4139035f02e2182b630caad | |
parent | 3194222de4a191baf4062a78a1da735be73da7d1 (diff) | |
download | AdventOfCode2016-50beae2167a705f694e4da4fa1ff63cfb1b2b16b.tar.gz |
solution for day 20
-rw-r--r-- | day20.fsx | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/day20.fsx b/day20.fsx new file mode 100644 index 0000000..595a1eb --- /dev/null +++ b/day20.fsx @@ -0,0 +1,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" \ No newline at end of file |