-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday23.go
73 lines (64 loc) · 1.48 KB
/
day23.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
package main
import (
"fmt"
"os"
"strings"
)
type Graph = map[string]map[string]bool
func threeCliques(graph Graph, prefix string) int {
nodes := make([]string, len(graph))
i := 0
for n := range graph {
nodes[i] = n
i++
}
cliques := 0
for i1, n1 := range nodes {
for i2, n2 := range nodes[i1+1:] {
for _, n3 := range nodes[i1+i2+1:] {
es1 := graph[n1]
es2 := graph[n2]
es3 := graph[n3]
if es1[n2] && es1[n3] && es2[n1] && es2[n3] && es3[n1] && es3[n2] {
// We have a 3-clique
if strings.HasPrefix(n1, prefix) || strings.HasPrefix(n2, prefix) || strings.HasPrefix(n3, prefix) {
cliques++
}
}
}
}
}
return cliques
}
func addEdge(graph Graph, n1 string, n2 string) {
es, ok := graph[n1]
if !ok {
es = make(map[string]bool)
graph[n1] = es
}
es[n2] = true
}
func main() {
args := os.Args
if len(args) <= 1 {
fmt.Println("usage:", args[0], "<path to input>")
os.Exit(1)
}
data, err := os.ReadFile(args[1])
if err != nil {
panic(err)
}
input := strings.Split(string(data), "\n")
graph := make(Graph)
for _, line := range input {
split := strings.Split(line, "-")
if len(split) >= 2 {
addEdge(graph, split[0], split[1])
addEdge(graph, split[1], split[0])
}
}
fmt.Println("Part 1:", threeCliques(graph, "t"))
// Part 2 was solved by inspecting the output in GraphViz (see the PDFs in
// `./generated`, as generated by `scripts/regenerate`). The clique can be
// found at the very top of `generated/input.pdf`.
}