-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day20.fs
29 lines (20 loc) · 927 Bytes
/
Day20.fs
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
module Year2016Day20
open AdventOfCode.FSharp.Common
let parse = parseEachLine (splitBy "-" (fun i -> int64 i.[0], int64 i.[1])) >> Seq.toArray
let getValidIpRanges lines =
let joins = lines |> Array.map (fun (s, _) -> (s, 1))
let leaves = lines |> Array.map (fun (_, e) -> (e, -1))
let together =
Array.append joins leaves
|> Array.sort
let step count (value, diff) =
let newCount = count + diff
(value, newCount), newCount
let temps, _ = Array.mapFold step 0 together
temps
|> Seq.pairwise
|> Seq.where (fun ((v1, c1), (v2, c2)) -> c1 = 0 && c2 = 1 && v2 - v1 > 1L)
|> Seq.map (fun ((v1, _), (v2, _)) -> (v1 + 1L, v2 - 1L))
let solvePart1 lines = getValidIpRanges lines |> Seq.head |> fst
let solvePart2 lines = getValidIpRanges lines |> Seq.sumBy (fun (s, e) -> e - s + 1L)
let solver = { parse = parse; part1 = solvePart1; part2 = solvePart2 }