-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.go
144 lines (132 loc) · 2.78 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
package main
import (
"bufio"
"fmt"
"math"
"os"
"regexp"
"strconv"
"sync"
)
func max(a, b int) int {
if a > b {
return a
}
return b
}
type vset map[int]bool
func (set vset) contains(key int) bool {
_, result := set[key]
return result
}
var flow = map[int]int{}
var tunnels = vset{}
type state struct {
valve int
opened vset
timeLeft int
pressure int
}
func (s state) open() state {
opened := vset{}
for k, v := range s.opened {
opened[k] = v
}
opened[s.valve] = true
return state{
s.valve,
opened,
s.timeLeft - 1,
s.pressure + flow[s.valve]*(s.timeLeft-1),
}
}
func pressure(initial state, forbidden vset) int {
result := 0
processed := vset{}
queue := []state{}
queue = append(queue, initial)
for len(queue) > 0 {
s := queue[0]
queue = queue[1:]
key := s.pressure*26*26 + s.valve
if processed.contains(key) || s.timeLeft == 0 {
continue
}
processed[key] = true
result = max(result, s.pressure)
if !s.opened.contains(s.valve) && !forbidden.contains(s.valve) && flow[s.valve] > 0 {
queue = append(queue, s.open())
}
for to := range flow {
if to != s.valve && !forbidden.contains(to) && tunnels.contains(s.valve*26*26+to) {
queue = append(queue, state{to, s.opened, s.timeLeft - 1, s.pressure})
}
}
}
return result
}
func key(valve string) int {
return int(valve[0]-'A')*26 + int(valve[1]-'A')
}
func key2(v1, v2 string) int {
return key(v1)*26*26 + key(v2)
}
func powerset(set []int) []vset {
size := int(math.Pow(2, float64(len(set))))
result := []vset{}
var index int
for index < size {
subSet := vset{}
for j, elem := range set {
if index&(1<<uint(j)) > 0 {
subSet[elem] = true
}
}
result = append(result, subSet)
index++
}
return result
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
reFlow := regexp.MustCompile("[0-9]+")
reNames := regexp.MustCompile("[A-Z]{2}")
for scanner.Scan() {
names := reNames.FindAllString(scanner.Text(), -1)
f, _ := strconv.ParseInt(reFlow.FindString(scanner.Text()), 0, 0)
flow[key(names[0])] = int(f)
for i := 1; i < len(names); i++ {
tunnels[key2(names[0], names[i])] = true
}
}
valves := []int{}
nonempty := []int{}
for v, f := range flow {
valves = append(valves, v)
if f != 0 {
nonempty = append(nonempty, v)
}
}
n1 := pressure(state{0, vset{}, 30, 0}, vset{})
n2 := 0
var wg sync.WaitGroup
var mutex sync.Mutex
for _, first := range powerset(nonempty) {
wg.Add(1)
go func(first vset, n2 *int) {
defer wg.Done()
second := vset{}
for v, f := range flow {
if !first.contains(v) && f > 0 {
second[v] = true
}
}
n := pressure(state{0, vset{}, 26, 0}, first) + pressure(state{0, vset{}, 26, 0}, second)
mutex.Lock()
defer mutex.Unlock()
*n2 = max(*n2, n)
}(first, &n2)
}
wg.Wait()
fmt.Println(n1, n2)
}