-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24_part01.fs
65 lines (53 loc) · 1.74 KB
/
day24_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module day24_part01
open AdventOfCode_2017.Modules
open System.Collections.Generic
type Port = {
Name: int
}
type Connection = {
From: Port
To: Port
}
let parseContent (lines: string array) =
let connections = HashSet<Connection>()
let ports = ResizeArray<Port>()
lines
|> Array.iter(fun l ->
let (pfrom, pto) =
(
{ Name = (int)(l.Split("/")[0]) },
{ Name = (int)(l.Split("/")[1]) }
)
connections.Add({ From = pfrom; To = pto }) |> ignore
if not (ports.Contains(pfrom)) then
ports.Add(pfrom)
if not (ports.Contains(pto)) then
ports.Add(pto)
)
(ports, connections |> List.ofSeq)
let strength(connections: Connection list) =
connections
|> List.sumBy(fun c -> c.From.Name + c.To.Name)
let maxStrength(bridges: Connection list seq) =
bridges
|> Seq.map strength
|> Seq.max
let rec buildBridges (bridge: Connection list) (nextPort: Port) (connections: Connection Set) =
seq {
yield bridge
let bridgeable = Set.filter (fun c -> c.From = nextPort || c.To = nextPort) connections
for comp in bridgeable do
let newNextPort =
if comp.To = nextPort then
comp.From
else comp.To
yield! buildBridges (comp :: bridge) newNextPort (Set.remove comp connections)
}
let findMaxStrength(connections: Connection list) =
let bridges = buildBridges [] { Name = 0 } (connections |> Set.ofList)
maxStrength bridges
let execute() =
let path = "day24/day24_input.txt"
let content = LocalHelper.GetLinesFromFile path
let (_, connections) = parseContent content
findMaxStrength connections