-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (101 loc) · 2.84 KB
/
main.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"sort"
"strings"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.ReadFileLineByLine("input.txt")
lanMap := getLANMap(input)
fmt.Println("answer to part 1: ", findT(find3Connected(lanMap)))
fmt.Println("answer to part 2: ", findMaxCliques(lanMap))
}
func getLANMap(input []string) map[string]map[string]struct{} {
lanMap := make(map[string]map[string]struct{})
for _, line := range input {
comp1, comp2 := line[:2], line[3:]
if _, ok := lanMap[comp1]; !ok {
lanMap[comp1] = make(map[string]struct{})
}
if _, ok := lanMap[comp2]; !ok {
lanMap[comp2] = make(map[string]struct{})
}
lanMap[comp1][comp2] = struct{}{}
lanMap[comp2][comp1] = struct{}{}
}
return lanMap
}
type lan struct {
a, b, c string
}
func find3Connected(lanMap map[string]map[string]struct{}) map[lan]struct{} {
result := make(map[lan]struct{})
for key, value := range lanMap {
if len(value) <= 1 {
continue
}
for key2, _ := range value {
for key3, _ := range value {
if key2 == key3 {
continue
}
if _, ok := lanMap[key2][key3]; ok {
r := []string{key, key2, key3}
sort.Strings(r)
result[lan{r[0], r[1], r[2]}] = struct{}{}
}
}
}
}
return result
}
func findT(input map[lan]struct{}) int {
count := 0
for key, _ := range input {
if string(key.a[0]) == "t" || string(key.b[0]) == "t" || string(key.c[0]) == "t" {
count++
}
}
return count
}
func BronKerbosch(currentClique []string, yetToConsider []string, alreadyConsidered []string, lanMap map[string]map[string]struct{}, cliques [][]string) [][]string {
if len(yetToConsider) == 0 && len(alreadyConsidered) == 0 {
cliques = append(cliques, append([]string{}, currentClique...))
return cliques
}
for index := 0; index < len(yetToConsider); {
node := yetToConsider[index]
newYetToConsider := []string{}
newAlreadyConsidered := []string{}
for _, n := range yetToConsider {
if _, ok := lanMap[node][n]; ok {
newYetToConsider = append(newYetToConsider, n)
}
}
for _, n := range alreadyConsidered {
if _, ok := lanMap[node][n]; ok {
newAlreadyConsidered = append(newAlreadyConsidered, n)
}
}
cliques = BronKerbosch(append(currentClique, node), newYetToConsider, newAlreadyConsidered, lanMap, cliques)
yetToConsider = append(yetToConsider[:index], yetToConsider[index+1:]...)
alreadyConsidered = append(alreadyConsidered, node)
}
return cliques
}
func findMaxCliques(lanMap map[string]map[string]struct{}) string {
maxClique := []string{}
allComputers := []string{}
for key, _ := range lanMap {
allComputers = append(allComputers, key)
}
cliques := BronKerbosch([]string{}, allComputers, []string{}, lanMap, [][]string{})
for _, c := range cliques {
if len(c) > len(maxClique) {
maxClique = c
}
}
sort.Strings(maxClique)
return strings.Join(maxClique, ",")
}