-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.go
187 lines (164 loc) · 4.09 KB
/
day16.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"sort"
"strconv"
"strings"
)
type Pipe struct {
name string
pressure int
leadsTo []string
}
type FromTo struct {
from string
to string
}
type Action struct {
pressureGain int
distance int
targetPipe Pipe
}
type ByPressure []Pipe
func (l ByPressure) Len() int { return len(l) }
func (l ByPressure) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l ByPressure) Less(i, j int) bool { return l[i].pressure > l[j].pressure }
func calcPressure(lines []string) int {
sum := 0
pipes := extractPipes(lines)
distances := make(map[FromTo]int)
for _, f := range pipes {
if f.pressure == 0 && f.name != "AA" {
continue
}
from := f.name
for _, t := range pipes {
if t.pressure == 0 {
continue
}
if f.name != t.name {
shortestPath := 9999
var history []string
findShortestDistance(from, from, t.name, pipes, &shortestPath, 0, history)
distances[FromTo{from, t.name}] = shortestPath
}
}
}
var valuePipes []Pipe
for _, p := range pipes {
if p.pressure != 0 {
valuePipes = append(valuePipes, p)
}
}
sort.Sort(ByPressure(valuePipes))
currentPipe := "AA"
// for i := 0; i < 30; i++ {
// highestNextPipe := findHighest(currentPipe, distances, valuePipes, 30-i)
// if highestNextPipe.pressureGain != 0 {
// sum += highestNextPipe.pressureGain
// i += highestNextPipe.distance + 1
// currentPipe = highestNextPipe.targetPipe.name
// remove(valuePipes, highestNextPipe.targetPipe)
// } else {
// break
// }
// }
var pipesList []Pipe
pipesList = append(pipesList, pipes[currentPipe])
bruteForceIt(&sum, pipes[currentPipe], valuePipes[:], distances, 0, 0, pipesList[:])
return sum
}
func bruteForceIt(highest *int, current Pipe, valuePipes []Pipe, distances map[FromTo]int, i int, sum int, pipesList []Pipe) {
timeLeft := 30 - i
if sum > *highest {
*highest = sum
}
if timeLeft <= 0 {
return
}
for _, p := range valuePipes {
if !containsPipe(pipesList, p) {
distance := distances[FromTo{current.name, p.name}]
if (distance + 1) > timeLeft {
continue
}
bruteForceIt(highest, p, valuePipes, distances, i+distance+1, sum+((timeLeft-distance-1)*p.pressure), append(pipesList, p)[:])
}
}
}
func remove(slice []Pipe, s int) []Pipe {
return append(slice[:s], slice[s+1:]...)
}
func findHighest(currentPipe string, distances map[FromTo]int, valuePipes []Pipe, timeLeft int) Action {
var bestMove Action
for _, p := range valuePipes {
distance := distances[FromTo{currentPipe, p.name}]
if (distance + 1) > timeLeft {
continue
}
total := (timeLeft - distance - 1) * p.pressure
if total > bestMove.pressureGain {
bestMove.pressureGain = total
bestMove.targetPipe = p
bestMove.distance = distance
}
}
return bestMove
}
// func remove(s []Pipe, r Pipe) []Pipe {
// for i, v := range s {
// if v.name == r.name {
// return append(s[:i], s[i+1:]...)
// }
// }
// return s
// }
func findShortestDistance(start, from, to string, pipes map[string]Pipe, shortest *int, steps int, history []string) {
if contains(history, from) {
return
}
if contains(pipes[from].leadsTo, to) {
if steps < *shortest {
*shortest = steps + 1
}
return
}
for _, v := range pipes[from].leadsTo {
history = append(history, from)
findShortestDistance(start, v, to, pipes, shortest, steps+1, history[:])
}
}
func containsPipe(s []Pipe, e Pipe) bool {
for _, a := range s {
if a.name == e.name {
return true
}
}
return false
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func extractPipes(lines []string) map[string]Pipe {
pipes := make(map[string]Pipe)
for _, v := range lines {
parts := strings.Split(v, " ")
name := parts[1]
pressurePart := strings.Split(parts[4], "=")
pressure, _ := strconv.Atoi(pressurePart[1][:len(pressurePart[1])-1])
var leadsTo []string
for i := 9; i < len(parts); i++ {
if i == len(parts)-1 {
leadsTo = append(leadsTo, parts[i])
} else {
leadsTo = append(leadsTo, parts[i][:2])
}
}
pipes[name] = Pipe{name, pressure, leadsTo}
}
return pipes
}