-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.go
156 lines (136 loc) · 2.88 KB
/
day12.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"io/ioutil"
"sort"
"strings"
)
func readLinesFromFile(fname string) (result []string, err error) {
b, err := ioutil.ReadFile(fname)
if err != nil {
return nil, err
}
lines := strings.Split(string(b), "\n")
// filter out empty lines
result = make([]string, 0, len(lines))
for _, l := range lines {
if len(l) == 0 {
continue
}
result = append(result, l)
}
return result, nil
}
type Vertex struct {
Key string
Vertices map[string]*Vertex
}
type Graph struct {
Vertices map[string]*Vertex
Start, End *Vertex
}
func newGraph() *Graph {
return &Graph{
Vertices: map[string]*Vertex{},
}
}
func (g *Graph) AddVertex(key string) {
v := g.Vertices[key]
if v == nil {
v := &Vertex{
Key: key,
Vertices: map[string]*Vertex{},
}
g.Vertices[key] = v
}
}
// The AddEdge method adds an edge between two vertices in the graph
func (g *Graph) AddEdge(k1, k2 string) {
v1 := g.Vertices[k1]
v2 := g.Vertices[k2]
v1.Vertices[v2.Key] = v2
v2.Vertices[v1.Key] = v1
g.Vertices[v1.Key] = v1
g.Vertices[v2.Key] = v2
}
var paths []string
func (g *Graph) isSmall(v *Vertex) bool {
return strings.ToLower(v.Key) == v.Key && v != g.Start
}
func (g *Graph) findPaths(start, end *Vertex, path string, visited map[string]int, lastSmall *Vertex) {
if g.isSmall(start) {
if start == lastSmall {
if visited[start.Key] > 1 {
return
}
} else if visited[start.Key] > 0 {
return
}
}
visited[start.Key] += 1
for _, edge := range start.Vertices {
if edge.Key == g.End.Key {
paths = append(paths, path+",end")
} else if edge != g.Start {
if lastSmall == nil && g.isSmall(start) {
g.findPaths(edge, end, path+","+edge.Key, visited, start)
}
g.findPaths(edge, end, path+","+edge.Key, visited, lastSmall)
}
}
visited[start.Key] -= 1
}
func createGraph(data []string) *Graph {
g := newGraph()
for _, d := range data {
sf := strings.Split(d, "-")
g.AddVertex(sf[0])
g.AddVertex(sf[1])
g.AddEdge(sf[0], sf[1])
}
g.Start = g.Vertices["start"]
g.End = g.Vertices["end"]
return g
}
const DEBUG = false
func day12part1(data []string) {
g := createGraph(data)
g.findPaths(g.Start, g.End, "start", make(map[string]int), g.Start)
sort.Strings(paths)
if DEBUG {
for _, p := range paths {
fmt.Println(p)
}
}
fmt.Println(len(paths))
}
func unique(slice []string) (u []string) {
keys := make(map[string]bool)
u = []string{}
for _, s := range slice {
if _, v := keys[s]; !v {
keys[s] = true
u = append(u, s)
}
}
return u
}
func day12part2(data []string) {
g := createGraph(data)
paths = []string{}
g.findPaths(g.Start, g.End, "start", make(map[string]int), nil)
paths = unique(paths)
if DEBUG {
for _, p := range paths {
fmt.Println(p)
}
}
sort.Strings(paths)
fmt.Println(len(paths))
}
func main() {
data, _ := readLinesFromFile("day12.txt")
day12part1(data)
fmt.Println()
day12part2(data)
}