-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13_part01.fs
40 lines (35 loc) · 1.27 KB
/
day13_part01.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
module day13_part01
open AdventOfCode_2015.Modules
open AdventOfCode_Utilities
type HappyDelta = {
From: string;
To: string;
Value: int;
}
let parseContent(lines: string array) : (string seq*Map<string*string, int>) =
let result = seq {
for line in lines do
let parts = line.Split(" ")
let from = parts[0]
let to' = parts[parts.Length - 1].Replace(".", "")
let value = if parts[2] = "gain" then (int)parts[3] else -(int)parts[3]
yield { From = from; To = to'; Value = value }
}
let mapping =
result
|> Seq.map(fun r -> (r.From, r.To), r.Value) |> Map.ofSeq
((result |> Seq.map _.From |> Seq.distinct), mapping)
let calculateHappiness(elements: string list) (mapping: Map<string*string, int>) =
(elements
|> List.pairwise
|> List.sumBy(fun (from,to') -> mapping.Item(from, to') + mapping.Item(to', from)))
let execute =
let input = "./day13/day13_input.txt"
let content = LocalHelper.GetLinesFromFile input
let (elements, mapping) = parseContent content
let possibleComb =
Utilities.perms (elements |> List.ofSeq)
|> Seq.map(fun l -> l @ [l.Head])
possibleComb
|> Seq.map(fun c -> calculateHappiness c mapping)
|> Seq.max