-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.go
83 lines (78 loc) · 1.53 KB
/
12.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type edge struct {
from, to string
}
var paths int = 0
var edges []edge = make([]edge, 0, 100)
func walk(path *[]string, canstep func(path *[]string, to string) bool) {
cave := (*path)[len(*path)-1]
if cave == "end" {
paths++
return
}
for _, edge := range edges {
if edge.from == cave && canstep(path, edge.to) {
*path = append(*path, edge.to)
walk(path, canstep)
*path = (*path)[0 : len(*path)-1]
}
if edge.to == cave && canstep(path, edge.from) {
*path = append(*path, edge.from)
walk(path, canstep)
*path = (*path)[0 : len(*path)-1]
}
}
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
pair := strings.Split(scanner.Text(), "-")
edges = append(edges, edge{pair[0], pair[1]})
}
path := make([]string, 0, 100)
path = append(path, "start")
walk(&path, func(path *[]string, to string) bool {
if to[0] >= 'a' && to[0] <= 'z' {
for _, cave := range *path {
if cave == to {
return false
}
}
}
return true
})
fmt.Println(paths)
paths = 0
path = make([]string, 0, 100)
path = append(path, "start")
walk(&path, func(path *[]string, to string) bool {
if to == "start" {
return false
}
dup := 0
*path = append(*path, to)
for _, cave := range *path {
if cave[0] < 'a' || cave[0] > 'z' {
continue
}
n := 0
for _, c := range *path {
if c == cave {
n++
}
}
if n >= 2 {
dup++
}
}
*path = (*path)[0 : len(*path)-1]
return dup <= 2
})
fmt.Println(paths)
}