-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.fs
67 lines (59 loc) · 1.88 KB
/
23.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
66
67
open System
open System.IO
let stopWatch = System.Diagnostics.Stopwatch.StartNew()
let DEBUG = false
let dbg v =
if DEBUG then
printfn "%A" v
v
let lines =
File.ReadAllText(@"input").Trim().Split("\n")
|> Array.map (fun x -> x.Split("-") |> fun i -> (i[0], i[1]))
let ans1 =
Array.append lines (lines |> Array.map (fun x -> (snd x, fst x)))
|> Array.groupBy fst
|> Array.map (fun (a, b) -> (a, b |> Array.map snd |> Set.ofSeq))
|> Map
|> fun M ->
M
|> Map.keys
|> Seq.filter (fun x -> x.StartsWith("t"))
|> Array.ofSeq
|> Array.map (fun x ->
M[x]
|> fun m ->
m
|> Set.map (fun y -> M[y] |> Set.intersect m |> Set.map (fun z -> [ x; y; z ] |> Set))
|> Set.unionMany)
|> Set.unionMany
|> Set.count
let ans2 =
Array.append lines (lines |> Array.map (fun x -> (snd x, fst x)))
|> Array.groupBy fst
|> Array.map (fun (a, b) -> (a, b |> Array.map snd |> Set.ofSeq))
|> Map
|> fun M ->
M
|> Map.keys
|> Array.ofSeq
|> Array.map (fun x ->
[ (Set.singleton x, M[x]) ]
|> Array.unfold (fun q ->
match q.Length with
| 0 -> None
| _ ->
match q[0] with
| (n, c) ->
match c.Count with
| 0 -> Some(n, q.Tail)
| _ ->
match c |> Set.minElement with
| fc -> Some(Set.empty, [ (n.Add(fc), c.Remove(fc) |> Set.intersect (M[fc])) ] @ q.Tail))
|> Array.maxBy Set.count)
|> Array.maxBy Set.count
|> Array.ofSeq
|> Array.sort
|> String.concat ","
stopWatch.Stop()
printfn "%A %A" ans1 ans2
printfn "Time: %fms" stopWatch.Elapsed.TotalMilliseconds