-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15_part02.fs
49 lines (41 loc) · 1.49 KB
/
day15_part02.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
module day15_part02
open System.Collections.Generic
open AdventOfCode_2020.Modules
let AddOrUpdate (d: Dictionary<int,(int option * int)>) (key: int) (value: int) =
match d.TryGetValue(key) with
| (true, (old, recent)) ->
d[key] <- (Some(recent), value)
| false, _ ->
d.Add(key, (None, value))
let parseContent(lines: string) =
lines.Split(",") |> Array.map int
let playturns (bespoken: int array) (numOfTurns: int) =
let seen = Dictionary<int, (int option * int)>()
let rec speak (last: int) (turn: int) =
if turn = numOfTurns then
last
else
if not (seen.ContainsKey(last)) then
let spoken = 0
AddOrUpdate seen spoken (turn + 1)
speak spoken (turn + 1)
else
match seen[last] with
| Some(o), recent ->
let spoken = recent - o
AddOrUpdate seen spoken (turn + 1)
speak spoken (turn + 1)
| None, recent ->
let spoken = 0
AddOrUpdate seen spoken (turn + 1)
speak spoken (turn + 1)
bespoken
|> Array.iteri(fun i v ->
AddOrUpdate seen v (i + 1)
)
speak bespoken[bespoken.Length - 1] bespoken.Length
let execute =
let path = "day15/day15_input.txt"
let content = LocalHelper.GetContentFromFile path
let bespokennumbers = parseContent content
playturns bespokennumbers 30000000