-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday16.go
More file actions
83 lines (72 loc) · 1.73 KB
/
Copy pathday16.go
File metadata and controls
83 lines (72 loc) · 1.73 KB
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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
readFile, err := os.Open("data16")
if err != nil {
fmt.Println(err)
}
rates := map[string]int{}
tunnels := map[string][]string{}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := fileScanner.Text();
valve:=strings.Split(line, " ")[1]
rate, _ :=strconv.Atoi(strings.Split(strings.Split(line, "=")[1], ";")[0])
var t []string
if (strings.Contains(line, "to valves")) {
t = strings.Split(strings.Split(line, "to valves ")[1], ", ")
} else {
t = []string{strings.Split(line, "to valve ")[1]}
}
rates[valve] = rate
tunnels[valve] = t
fmt.Println(valve, rate, t)
}
var move func(valve string, time int, open map[string]bool, path []string) (int)
move = func(valve string, time int, open map[string]bool, path []string) (int) {
if time == 0 {
return 0;
}
ret := 0
if (!open[valve] && rates[valve]>0) {
// fmt.Println("path", path, "time left", time, "opening", valve)
newOpen := map[string]bool{}
for k, v := range open {
newOpen[k] = v
}
newOpen[valve] = true
ret = move(valve, time - 1, newOpen, append(path, "*")) + rates[valve] * (time - 1)
}
for _, t := range tunnels[valve] {
// fmt.Println("path", path, "time left", time, "going to valve", t)
found := false
for i := len(path)-1; i>=0; i-- {
if path[i] == "*" {
break;
}
if path[i] == t {
found = true
break;
}
}
if !found {
ret = Max(ret, move(t, time - 1, open, append(path, t)))
}
}
return ret
}
fmt.Println(move("AA", 30, map[string]bool{}, []string{"AA"}))
}
func Max(x int, y int) int {
if x > y {
return x
}
return y
}