-
Notifications
You must be signed in to change notification settings - Fork 1
/
day16_valves.go
381 lines (325 loc) · 9.98 KB
/
day16_valves.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
"golang.org/x/exp/slices"
"gonum.org/v1/gonum/stat/combin"
)
type ValveData struct {
name string
flowRate int
neighbours []string
open bool
}
type ValveMap map[string]*ValveData
type ValveMatrix map[string][]*ValveData
func (m ValveMatrix) String() {
for valveName, neighbours := range m {
fmt.Println("Valve", valveName)
for _, neighbour := range neighbours {
fmt.Println(*neighbour)
}
}
}
func parseValveData(filePath string) ValveMap {
input, _ := os.ReadFile(filePath)
scanner := bufio.NewScanner(strings.NewReader(string(input)))
regEx := `Valve (?P<valve>\w+) has flow rate=(?P<flowRate>\d+); tunnel(s)* lead(s)* to valve(s)* (?P<neighbhours>([A-Z]+(,\s)*)+)`
valveMap := make(map[string]*ValveData)
for scanner.Scan() {
extractedField := processRegularExpression(regEx, scanner.Text())
var neighbours []string
for _, neighbour := range strings.Split(extractedField["neighbhours"], ", ") {
neighbours = append(neighbours, neighbour)
}
open := false
flowRate, _ := strconv.Atoi(extractedField["flowRate"])
if flowRate == 0 {
open = true
}
valve := &ValveData{
name: extractedField["valve"],
flowRate: flowRate,
neighbours: neighbours,
open: open,
}
valveMap[valve.name] = valve
}
return valveMap
}
func createAdjacencyMatrix(valveMap ValveMap) ValveMatrix {
adjMatrix := make(ValveMatrix)
for valveName, valveInfo := range valveMap {
for _, neighbour := range valveInfo.neighbours {
adjMatrix[valveName] = append(adjMatrix[valveName], valveMap[neighbour])
}
}
return adjMatrix
}
type ValvePath struct {
start string
end string
}
type ValveDistance map[ValvePath]int
func createDistanceMatrix(valves ValveMap) ValveDistance {
adjMatrix := createAdjacencyMatrix(valves)
var allValves []string
for valveName := range adjMatrix {
allValves = append(allValves, valveName)
}
distanceMatrix := make(ValveDistance)
for i := 0; i < len(allValves); i++ {
distances := getMinDistanceFromSingleTunnel(valves, adjMatrix, allValves[i])
for end, distance := range distances {
distanceMatrix[ValvePath{start: allValves[i], end: end}] = distance
}
}
return distanceMatrix
}
type ValveCheckData struct {
name string
dist int
}
func getMinDistanceFromSingleTunnel(valves ValveMap, adjMatrix ValveMatrix, start string) map[string]int {
var checkQ []ValveCheckData
checkQ = append(checkQ, ValveCheckData{name: start, dist: 0})
minDistance := make(map[string]int)
for valve := range valves {
minDistance[valve] = math.MaxInt
}
for len(checkQ) > 0 {
currValve := checkQ[0]
checkQ = checkQ[1:]
// is distance to current valve less than what we have?
if currValve.dist < minDistance[currValve.name] {
minDistance[currValve.name] = currValve.dist
}
for _, neighbour := range adjMatrix[currValve.name] {
if currValve.dist+1 < minDistance[neighbour.name] {
nextValveToCheck := ValveCheckData{name: neighbour.name, dist: currValve.dist + 1}
checkQ = append(checkQ, nextValveToCheck)
}
}
}
return minDistance
}
type ValveScenario struct {
step int
prevScenario *ValveScenario
agents []ValveAgent
valvesToOpen []string
startFlowRate int
// endFlowRate int
totalFlow int
}
type ValveAgent struct {
currValue string
nextStepAvailable int
flowRateToAdd int
}
func findMaxPressureRelease(valves ValveMap, maxSteps int, startValve string, numAgents int) int {
distMatrix := createDistanceMatrix(valves)
var valvesToOpen []string
for _, valve := range valves {
if !valve.open {
valvesToOpen = append(valvesToOpen, valve.name)
}
}
var agents []ValveAgent
for i := 0; i < numAgents; i++ {
agent := ValveAgent{currValue: startValve, nextStepAvailable: 0, flowRateToAdd: 0}
agents = append(agents, agent)
}
initialScenario := ValveScenario{
step: 0,
agents: agents,
valvesToOpen: valvesToOpen,
startFlowRate: 0,
// endFlowRate: 0,
totalFlow: 0,
}
var scenariosToProcess []ValveScenario
scenariosToProcess = append(scenariosToProcess, initialScenario)
maxFlow := 0
counter := 0
var bestScenarioEnd *ValveScenario
for len(scenariosToProcess) > 0 {
counter++
// if counter%10 == 0 {
// break
// fmt.Println("scenario #", counter)
// }
currScenario := scenariosToProcess[0]
scenariosToProcess = scenariosToProcess[1:]
// update current flow rate if a valve got opened
// figure out which agents are free
updatedFlowRate := currScenario.startFlowRate
var availableAgents []*ValveAgent
var availableAgentPositions []int
for idx, agent := range currScenario.agents {
if agent.nextStepAvailable == currScenario.step {
updatedFlowRate += agent.flowRateToAdd
agent.flowRateToAdd = 0
agent.nextStepAvailable = 0
// fmt.Println("rate", updatedFlowRate)
availableAgents = append(availableAgents, &agent)
availableAgentPositions = append(availableAgentPositions, idx)
}
}
// fmt.Println("*******************")
// fmt.Println(updatedTotalFlow)
// fmt.Println("Current Scenario")
// fmt.Println(currScenario)
// fmt.Println("*******************")
if currScenario.step == maxSteps {
if currScenario.totalFlow > maxFlow {
maxFlow = currScenario.totalFlow
bestScenarioEnd = &currScenario
}
continue
}
possibleValves := currScenario.valvesToOpen
if len(availableAgents) > 1 {
possibleValves = append(possibleValves, "")
}
var valveCombinations [][]int
n := len(possibleValves)
k := len(availableAgents)
if n == 0 {
} else if n >= k {
valveCombinations = combin.Permutations(len(possibleValves), len(availableAgents))
} else {
arr := make([]int, 1)
arr[0] = 0
valveCombinations = append(valveCombinations, arr)
}
canImprove := false
for _, valvePositions := range valveCombinations {
var updatedAgents []ValveAgent
var valvesBeingOpened []string
movePossible := true
for idx, valvePosition := range valvePositions {
agentToProcess := availableAgents[idx]
valveToOpen := possibleValves[valvePosition]
// fmt.Println("valve to open", valveToOpen)
var updatedAgent ValveAgent
if valveToOpen == "" {
updatedAgent = ValveAgent{
currValue: agentToProcess.currValue,
flowRateToAdd: 0,
nextStepAvailable: maxSteps,
}
} else {
// how far is valve?
// do we have enough time to get there and turn it on?
currValue := agentToProcess.currValue
distance := distMatrix[ValvePath{start: currValue, end: valveToOpen}]
// is there enough time to valve and turn it on?
timeStepValveWouldReleasePressure := currScenario.step + distance + 1
movePossible = movePossible && timeStepValveWouldReleasePressure <= maxSteps
updatedAgent = ValveAgent{
currValue: valveToOpen,
flowRateToAdd: valves[valveToOpen].flowRate,
nextStepAvailable: timeStepValveWouldReleasePressure,
}
valvesBeingOpened = append(valvesBeingOpened, valveToOpen)
}
updatedAgents = append(updatedAgents, updatedAgent)
}
if movePossible {
// add missing agent
for idx, agent := range currScenario.agents {
if !slices.Contains(availableAgentPositions, idx) {
updatedAgents = append(updatedAgents, agent)
}
}
var updatedValvesToOpen []string
for _, valve := range currScenario.valvesToOpen {
if !slices.Contains(valvesBeingOpened, valve) {
updatedValvesToOpen = append(updatedValvesToOpen, valve)
}
}
nextStep := math.MaxInt
for _, agent := range updatedAgents {
if agent.nextStepAvailable < nextStep {
nextStep = agent.nextStepAvailable
}
}
// endFlowRate := updatedFlowRate
// for _, agent := range updatedAgents {
// if agent.nextStepAvailable == nextStep {
// endFlowRate += agent.flowRateToAdd
// }
// }
// calculate total flow since previous
updatedTotalFlow := currScenario.totalFlow
timeElapsed := nextStep - currScenario.step
updatedTotalFlow += updatedFlowRate * timeElapsed
newScenario := ValveScenario{
step: nextStep,
prevScenario: &currScenario,
agents: updatedAgents,
valvesToOpen: updatedValvesToOpen,
startFlowRate: updatedFlowRate,
// endFlowRate: endFlowRate,
totalFlow: updatedTotalFlow,
}
// fmt.Println(newScenario, updatedAgents)
scenariosToProcess = append(scenariosToProcess, newScenario)
canImprove = true
}
}
// fmt.Println("-------")
if !canImprove {
// fmt.Println("reached")
updatedTotalFlow := currScenario.totalFlow
timeElapsed := maxSteps - currScenario.step
updatedTotalFlow += updatedFlowRate * timeElapsed
updatedScenario := ValveScenario{
step: maxSteps,
prevScenario: &currScenario,
agents: currScenario.agents,
valvesToOpen: currScenario.valvesToOpen,
startFlowRate: updatedFlowRate,
// endFlowRate: endFlowRate,
totalFlow: updatedTotalFlow,
}
// fmt.Println(updatedScenario)
// fmt.Println("-------")
scenariosToProcess = append(scenariosToProcess, updatedScenario)
continue
}
}
// fmt.Println(counter)
currScenario := bestScenarioEnd
for currScenario.prevScenario != nil {
fmt.Println(currScenario)
currScenario = currScenario.prevScenario
}
fmt.Println(currScenario)
return maxFlow
}
func day16() {
var valves ValveMap
var maxRelease int
// sample data
// valves = parseValveData("2022/data/day16_sample.txt")
// maxRelease = findMaxPressureRelease(valves, 30, "AA", 1)
// if maxRelease != 1651 {
// panic("Part 1 example is failing")
// }
valves = parseValveData("2022/data/day16_sample.txt")
maxRelease = findMaxPressureRelease(valves, 26, "AA", 2)
fmt.Println(maxRelease)
// real data
// valves = parseValveData("2022/data/day16_input.txt")
// maxRelease = findMaxPressureRelease(valves, 30, "AA", 1)
// fmt.Println("Part 1:", maxRelease)
valves = parseValveData("2022/data/day16_input.txt")
maxRelease = findMaxPressureRelease(valves, 30, "AA", 2)
fmt.Println(maxRelease)
}