-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14.fs
More file actions
56 lines (44 loc) · 1.5 KB
/
Day14.fs
File metadata and controls
56 lines (44 loc) · 1.5 KB
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
46
47
48
49
50
51
52
53
54
55
56
module Day14
let inline (|+) f (a, b) = f a, f b
let parse =
Seq.map (
parseRegex
".?(\d+),(\d+).*?(-?\d+),(-?\d+)"
(Array.map int
>> fun a -> (a[0], a[2]), (a[1], a[3]))
)
let move (mx, my) t (x, y) =
let norm m p = if p < 0 then m + p else p
let f w t (p, s) = (p + (t * s)) % w |> norm w
f mx t x, f my t y
let quads (w, t) xs =
let q (w, t) =
[ fun (x, y) -> x < w / 2 && y < t / 2
fun (x, y) -> x > w / 2 && y < t / 2
fun (x, y) -> x < w / 2 && y > t / 2
fun (x, y) -> x > w / 2 && y > t / 2 ]
q (w, t) |> Seq.map (fun f -> Seq.filter f xs)
let limit = (101, 103)
let part1 =
Seq.map (move limit 100)
>> quads limit
>> Seq.map Seq.length
>> Seq.product
let variance xs =
let positions = xs |> Seq.toArray
let n = float positions.Length
let avgX = positions |> Array.averageBy (fun (x, _) -> float x)
let avgY = positions |> Array.averageBy (fun (_, y) -> float y)
let varX = (positions |> Array.sumBy (fun (x, _) -> (float x - avgX) ** 2.0)) / n
let varY = (positions |> Array.sumBy (fun (_, y) -> (float y - avgY) ** 2.0)) / n
varX + varY
let part2 robots =
let maxTime = 101 * 103
seq { 0 .. maxTime - 1 }
|> Seq.map (fun t ->
let positions = robots |> Seq.map (fun (p, v) -> move limit t (p, v))
t, variance positions)
|> Seq.minBy snd
|> fst
|> Some
let Solve: string seq -> int * option<int> = parse >> both part1 part2