-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.fs
45 lines (33 loc) · 1.1 KB
/
Day9.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module AoC2020.Day9
open AoC2020.Utils
let getWindow preample numbers =
let stream =
numbers |> Seq.windowed preample |> Seq.cache
Seq.zip stream stream
|> Seq.map (fun (a, b) ->
Seq.allPairs a b
|> Seq.filter (fun (a, b) -> a <> b)
|> Seq.map (fun (a, b) -> a + b))
let findNumber input preampleSize =
let toBeTested = input |> Seq.skip preampleSize
let preamples = getWindow preampleSize input
let stream = Seq.zip toBeTested preamples
stream
|> Seq.find (fun (n, preample) -> preample |> Seq.exists ((=) n) |> not)
|> fst
let day9 fn preampleSize () =
let input =
readInput fn |> Seq.map int64 |> Seq.cache
findNumber input preampleSize
let day9part2 fn preampleSize () =
let input =
readInput fn |> Seq.map int64 |> Seq.cache
let n = findNumber input preampleSize
let windows =
Seq.initInfinite (fun i -> input |> Seq.map int64 |> Seq.windowed (i + 2))
|> Seq.concat
let found =
windows
|> Seq.find (Seq.sum >> (=) n)
|> Seq.cache
Seq.min found + Seq.max found