-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.go
142 lines (125 loc) · 2.74 KB
/
day25.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
package main
import (
"fmt"
"strings"
)
type Edge struct {
from, to string
}
func day25() {
lines := getLines("input/25.txt")
nodes := getNodes(lines)
edgeCount := countEdges(nodes)
first := findAndRemoveMax(edgeCount)
removeEdge(nodes, first)
edgeCount = countEdges(nodes)
second := findAndRemoveMax(edgeCount)
removeEdge(nodes, second)
edgeCount = countEdges(nodes)
third := findAndRemoveMax(edgeCount)
removeEdge(nodes, third)
countA := countNodes(nodes, first.from)
countB := len(nodes) - countA
var result = countA * countB
var result2 = 0
fmt.Println("Day 25 Part 1 Result: ", result)
fmt.Println("Day 25 Part 2 Result: ", result2)
}
func removeEdge(nodes map[string][]string, edge Edge) {
new := []string{}
for _, val := range nodes[edge.from] {
if val != edge.to {
new = append(new, val)
}
}
nodes[edge.from] = new
new = []string{}
for _, val := range nodes[edge.to] {
if val != edge.from {
new = append(new, val)
}
}
nodes[edge.to] = new
}
func findAndRemoveMax(edges map[Edge]int) Edge {
max := 0
var maxEdge Edge
for key, val := range edges {
if val > max {
max = val
maxEdge = key
}
}
delete(edges, maxEdge)
return maxEdge
}
func countEdges(nodes map[string][]string) map[Edge]int {
encountered := map[Edge]int{}
i := 0
for from := range nodes {
walkNodes(nodes, from, encountered)
i++
// if this doesn't work, increase value
if i > 50 {
break
}
}
return encountered
}
func countNodes(nodes map[string][]string, start string) int {
visited := map[string]bool{}
queue := []string{start}
for len(queue) > 0 {
from := queue[0]
queue = queue[1:]
for _, to := range nodes[from] {
if _, found := visited[to]; found {
continue
}
queue = append(queue, to)
visited[to] = true
}
}
return len(visited)
}
func walkNodes(nodes map[string][]string, start string, encountered map[Edge]int) {
visited := map[string]bool{}
queue := []string{start}
for len(queue) > 0 {
from := queue[0]
queue = queue[1:]
for _, to := range nodes[from] {
if _, found := visited[to]; found {
continue
}
queue = append(queue, to)
visited[to] = true
var edge Edge
if from < to {
edge = Edge{from, to}
} else {
edge = Edge{to, from}
}
encountered[edge]++
}
}
}
func getNodes(lines []string) map[string][]string {
nodes := map[string][]string{}
for _, line := range lines {
split := strings.Split(line, ": ")
from := split[0]
if _, inside := nodes[from]; !inside {
nodes[from] = []string{}
}
to := strings.Fields(split[1])
for _, target := range to {
nodes[from] = append(nodes[from], target)
if _, inside := nodes[target]; !inside {
nodes[target] = []string{}
}
nodes[target] = append(nodes[target], from)
}
}
return nodes
}