-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14_part02.fs
64 lines (57 loc) · 1.82 KB
/
day14_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module day14_part02
open System.Collections.Generic
open AdventOfCode_2015.Modules
type Reindeer = {
Name: string
Speed: int
Stamina: int
Resting: int
}
let parseContent(lines: string array) =
lines
|> Array.map(fun l ->
let who = l.Split(" ")[0]
let (speed, time, resting) =
(
(int)(l.Split(" ")[3]),
(int)(l.Split(" ")[6]),
(int)(l.Split(" ")[13])
)
{ Name = who; Speed = speed; Stamina = time; Resting = resting }
)
let calculatePosition ((reindeer, second): Reindeer * int) =
let cycleTime = reindeer.Stamina + reindeer.Resting
let fullCycles = second / cycleTime
let remainingTime = second % cycleTime
let activeTime = fullCycles * reindeer.Stamina + min remainingTime reindeer.Stamina
activeTime * reindeer.Speed
let calculateWinningPoints((reindeers, seconds): Reindeer array*int) =
let winningTable = Dictionary<string, int array>()
reindeers
|> Array.iter(fun r ->
winningTable.Add(r.Name, Array.zeroCreate seconds)
)
[1..seconds]
|> List.iter(fun s ->
let roundTimes =
reindeers
|> Array.map(fun r -> (r.Name, calculatePosition(r, s)))
let maxTime = roundTimes |> Array.maxBy snd
let winners = roundTimes |> Array.filter(fun (r, t) -> t = snd maxTime)
winners
|> Array.iter(fun (r, _) ->
winningTable[r][s-1] <- 1
)
)
let points =
[for kvp in winningTable do
yield (kvp.Key, kvp.Value |> Array.sum)
]
points
|> List.map snd
|> List.max
let execute =
let input = "./day14/day14_input.txt"
let content = LocalHelper.GetLinesFromFile input
let reindeers = parseContent content
calculateWinningPoints(reindeers, 2503)