-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day09.fs
61 lines (48 loc) · 1.68 KB
/
Day09.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
module Year2015Day09
open AdventOfCode.FSharp.Common
open GraphFS.Graph
type Edge =
{ P1 : string; P2 : string; Dist : int }
let parseEdge line =
let toks = splitBy " " id line
{ P1 = toks.[0]; P2 = toks.[2]; Dist = int toks.[4]}
let parse = parseEachLine parseEdge
let solvePart1 input =
let G =
input
|> Seq.map (fun e -> [(e.P1, e.P2), e.Dist; (e.P2, e.P1), e.Dist])
|> Seq.collect id
|> Graph.fromEdgesWithData
let locations = Graph.verts G |> Set.ofSeq
let rec getShortest current remainingLocs =
if Set.isEmpty remainingLocs then 0
else
remainingLocs
|> Seq.map (fun l ->
let dist = Graph.getEdgeData (current, l) G
let remPath = getShortest l (Set.remove l remainingLocs)
dist + remPath)
|> Seq.min
locations
|> Seq.map (fun l -> getShortest l (Set.remove l locations))
|> Seq.min
let solvePart2 input =
let G =
input
|> Seq.map (fun e -> [(e.P1, e.P2), e.Dist; (e.P2, e.P1), e.Dist])
|> Seq.collect id
|> Graph.fromEdgesWithData
let locations = Graph.verts G |> Set.ofSeq
let rec getShortest current remainingLocs =
if Set.isEmpty remainingLocs then 0
else
remainingLocs
|> Seq.map (fun l ->
let dist = Graph.getEdgeData (current, l) G
let remPath = getShortest l (Set.remove l remainingLocs)
dist + remPath)
|> Seq.max
locations
|> Seq.map (fun l -> getShortest l (Set.remove l locations))
|> Seq.max
let solver = { parse = parse; part1 = solvePart1; part2 = solvePart2 }