/
16.go
251 lines (236 loc) · 5.19 KB
/
16.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
package main
import (
"math/bits"
"regexp"
"sort"
"strings"
"github.com/cespare/hasty"
"github.com/cespare/next/container/heap"
"golang.org/x/exp/slices"
)
func init() {
addSolutions(16, problem16)
}
func problem16(ctx *problemContext) {
valves := scanSlice(ctx, parseValve)
t := buildValveTunnels(valves)
ctx.reportLoad()
ctx.reportPart1(t.bestRelease())
ctx.reportPart2(t.bestRelease2())
}
var valveRegexp = regexp.MustCompile(`Valve (?P<Source>[A-Z]+) has flow rate=(?P<Flow>\d+); tunnels? leads? to valves? (?P<Dests>[A-Z, ]+)$`)
func parseValve(line string) rawValve {
var v struct {
Source string
Flow int64
Dests string
}
hasty.MustParse([]byte(line), &v, valveRegexp)
return rawValve{
src: v.Source,
flow: v.Flow,
dsts: strings.Split(v.Dests, ", "),
}
}
type rawValve struct {
src string
flow int64
dsts []string
}
type valveTunnels struct {
edges [][]int
flow []int64
bestFlows []flowValve // non-zero flows sorted largest -> smallest
}
type flowValve struct {
flow int64
valve int
}
func buildValveTunnels(valves []rawValve) *valveTunnels {
nodes := make([]string, len(valves))
for i, v := range valves {
nodes[i] = v.src
}
sort.Strings(nodes)
if nodes[0] != "AA" {
panic("unexpected")
}
byName := make(map[string]int)
for i, src := range nodes {
byName[src] = i
}
t := &valveTunnels{
edges: make([][]int, len(valves)),
flow: make([]int64, len(valves)),
}
for _, v := range valves {
si := byName[v.src]
t.flow[si] = v.flow
for _, dst := range v.dsts {
di := byName[dst]
t.edges[si] = append(t.edges[si], di)
}
if v.flow > 0 {
t.bestFlows = append(t.bestFlows, flowValve{v.flow, si})
}
}
slices.SortFunc(t.bestFlows, func(fv0, fv1 flowValve) bool {
return fv0.flow > fv1.flow
})
return t
}
type tunnelState struct {
rem int
pos0 int
pos1 int // unused for part 1
open uint64
total int64
max int64
}
// maxPossible computes a loose upper bound on the best outcome of the current
// state by assuming we visit as many remaining valves as we can, largest to
// smallest, with a single walk in between.
func (t *valveTunnels) maxPossible(ts tunnelState, numWorkers int) int64 {
if ts.rem < 2 { // need rem>=2 to benefit from opening a valve
return ts.total
}
w := numWorkers
for _, fv := range t.bestFlows {
if ts.open&(1<<fv.valve) > 0 {
continue
}
if w == numWorkers {
ts.rem-- // 1 to open
}
ts.total += int64(ts.rem) * fv.flow
w--
if w == 0 {
ts.rem -= 1 // 1 to move
if ts.rem < 2 {
break
}
w = numWorkers
}
}
return ts.total
}
func (t *valveTunnels) bestRelease() int64 {
var best int64
bestByState := make(map[tunnelState]int64) // state{total: 0} -> best total
q := heap.New(func(ts0, ts1 tunnelState) bool {
return ts0.max > ts1.max
})
pushState := func(ts tunnelState) {
vs := ts
vs.total = 0
if b, ok := bestByState[vs]; !ok || ts.total > b {
bestByState[vs] = ts.total
ts.max = t.maxPossible(ts, 1)
q.Push(ts)
}
}
pushState(tunnelState{rem: 30})
for {
ts := q.Pop()
if ts.max < best {
return best
}
if ts.total > best {
best = ts.total
}
if ts.rem == 0 || bits.OnesCount64(ts.open) == len(t.edges) {
continue
}
flow := t.flow[ts.pos0]
if flow > 0 && ts.open&(1<<ts.pos0) == 0 {
ts1 := ts
ts1.rem--
ts1.open |= 1 << ts.pos0
ts1.total += int64(ts1.rem) * flow
pushState(ts1)
}
for _, dst := range t.edges[ts.pos0] {
ts1 := ts
ts1.rem--
ts1.pos0 = dst
pushState(ts1)
}
}
}
func (t *valveTunnels) bestRelease2() int64 {
var best int64
bestByState := make(map[tunnelState]int64) // state{pos0<=pos1, total=0} -> best total
q := heap.New(func(ts0, ts1 tunnelState) bool {
return ts0.max > ts1.max
})
pushState := func(ts tunnelState) {
vs := ts
vs.total = 0
if vs.pos0 > vs.pos1 {
// We can swap the positions WLOG.
vs.pos0, vs.pos1 = vs.pos1, vs.pos0
}
if b, ok := bestByState[vs]; !ok || ts.total > b {
bestByState[vs] = ts.total
ts.max = t.maxPossible(ts, 2)
q.Push(ts)
}
}
pushState(tunnelState{rem: 26})
for {
ts := q.Pop()
if ts.max < best {
return best
}
if ts.total > best {
best = ts.total
}
if ts.rem == 0 || bits.OnesCount64(ts.open) == len(t.edges) {
continue
}
flow0, flow1 := t.flow[ts.pos0], t.flow[ts.pos1]
canOpen0 := flow0 > 0 && ts.open&(1<<ts.pos0) == 0
canOpen1 := flow1 > 0 && ts.open&(1<<ts.pos1) == 0 && ts.pos0 != ts.pos1
if canOpen0 {
ts1 := ts
ts1.rem--
ts1.open |= 1 << ts.pos0
ts1.total += int64(ts1.rem) * flow0
if canOpen1 {
// Open both
ts2 := ts1
ts2.open |= 1 << ts.pos1
ts2.total += int64(ts1.rem) * flow1
pushState(ts2)
}
// Open pos0, move pos1
for _, dst := range t.edges[ts.pos1] {
ts2 := ts1
ts2.pos1 = dst
pushState(ts2)
}
}
if canOpen1 {
// Move pos0, open pos1
ts1 := ts
ts1.rem--
ts1.open |= 1 << ts.pos1
ts1.total += int64(ts1.rem) * flow1
for _, dst := range t.edges[ts.pos0] {
ts2 := ts1
ts2.pos0 = dst
pushState(ts2)
}
}
// Move both
for _, dst0 := range t.edges[ts.pos0] {
for _, dst1 := range t.edges[ts.pos1] {
ts1 := ts
ts1.rem--
ts1.pos0 = dst0
ts1.pos1 = dst1
pushState(ts1)
}
}
}
}