-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
121 lines (110 loc) · 2.63 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
package main
import (
"io"
"sort"
"strings"
"github.com/teivah/go-aoc"
)
func fs1(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
graph := make(map[string][]string)
linked := make(map[aoc.Pair[string, string]]bool)
for _, line := range lines {
del := aoc.NewDelimiter(line, "-")
a, b := del.GetString(0), del.GetString(1)
graph[a] = append(graph[a], b)
graph[b] = append(graph[b], a)
linked[aoc.Pair[string, string]{a, b}] = true
linked[aoc.Pair[string, string]{b, a}] = true
}
sets := make(map[tri]bool)
for from, tos := range graph {
group(from, tos, sets, linked)
}
res := 0
for s := range sets {
if s.startsWithT() {
res++
}
}
return res
}
type tri struct {
a string
b string
c string
}
func (t tri) startsWithT() bool {
return t.a[0] == 't' ||
t.b[0] == 't' ||
t.c[0] == 't'
}
func newSet(machines ...string) tri {
if len(machines) != 3 {
panic(len(machines))
}
sort.Strings(machines)
return tri{machines[0], machines[1], machines[2]}
}
func group(a string, tos []string, sets map[tri]bool, linked map[aoc.Pair[string, string]]bool) {
for i := 0; i < len(tos); i++ {
b := tos[i]
for j := i + 1; j < len(tos); j++ {
c := tos[j]
if linked[aoc.Pair[string, string]{b, c}] {
sets[newSet(a, b, c)] = true
}
}
}
}
func fs2(input io.Reader) string {
lines := aoc.ReaderToStrings(input)
graph := make(map[string][]string)
linked := make(map[aoc.Pair[string, string]]bool)
for _, line := range lines {
del := aoc.NewDelimiter(line, "-")
a, b := del.GetString(0), del.GetString(1)
graph[a] = append(graph[a], b)
graph[b] = append(graph[b], a)
linked[aoc.Pair[string, string]{a, b}] = true
linked[aoc.Pair[string, string]{b, a}] = true
}
sets := make(map[string]bool)
for from, tos := range graph {
group2(from, tos, sets, linked)
}
res := ""
for s := range sets {
if areAllConnected(aoc.NewDelimiter(s, ",").GetStrings(), linked) {
if len(s) > len(res) {
res = s
}
}
}
return res
}
func areAllConnected(s []string, linked map[aoc.Pair[string, string]]bool) bool {
for i := 0; i < len(s); i++ {
for j := i + 1; j < len(s); j++ {
if !linked[aoc.Pair[string, string]{s[i], s[j]}] {
return false
}
}
}
return true
}
func group2(a string, tos []string, sets map[string]bool, linked map[aoc.Pair[string, string]]bool) {
for i := 0; i < len(tos); i++ {
b := tos[i]
var connected []string
for j := i + 1; j < len(tos); j++ {
c := tos[j]
if linked[aoc.Pair[string, string]{b, c}] {
connected = append(connected, c)
}
}
connected = append(connected, a, b)
sort.Strings(connected)
sets[strings.Join(connected, ",")] = true
}
}