-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day15.fs
53 lines (42 loc) · 972 Bytes
/
Day15.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
46
47
48
49
50
51
52
53
module Day15
type State = {
Turn: int
LastSpoken: int
Log: (int, int) Map
}
let speak state =
state.Log
|> Map.tryFind state.LastSpoken
|> Option.map (fun turn -> state.Turn - turn)
|> Option.defaultValue 0
let update state lastSpoken =
{ Turn = state.Turn + 1
LastSpoken = lastSpoken
Log = state.Log |> Map.add state.LastSpoken (state.Turn)
}
let turn state =
speak state |> update state
let init =
List.scan update { Turn = 0; LastSpoken = 0; Log = Map.empty }
let play nums =
let starting = init nums
List.last starting
|> Seq.unfold (fun state ->
let state = speak state |> update state
Some (state, state))
|> Seq.append starting
|> Seq.skip 1
let solve turn =
play
>> Seq.skip (turn - 1)
>> Seq.head
>> (fun state -> state.LastSpoken)
let input = [ 20;0;1;11;6;3 ]
let part1 () =
solve 2020 input
let part2 () =
solve 30000000 input
open System
let run () =
part2 ()
|> Console.WriteLine