-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (123 loc) · 3.17 KB
/
main.go
File metadata and controls
156 lines (123 loc) · 3.17 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
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strings"
"time"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "12", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type cave struct {
name string
small bool
}
func makeCave(name string) cave {
return cave{
name: name,
small: strings.ToLower(name) == name,
}
}
func parse(r io.Reader) map[cave][]cave {
scanner := bufio.NewScanner(r)
caves := make(map[cave][]cave)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, "-")
first := makeCave(parts[0])
second := makeCave(parts[1])
caves[first] = append(caves[first], second)
caves[second] = append(caves[second], first)
}
if scanner.Err() != nil {
panic(scanner.Err())
}
return caves
}
func solve(r io.Reader) {
caves := parse(r)
t := time.Now()
numPaths := explore(caves, cave{name: "start", small: true}, nil)
fmt.Println(numPaths)
fmt.Println(time.Now().Sub(t))
}
// pathNode represents the current cave being visited along some path. By storing a pointer to the previous pathNode, it
// builds a chain of caves in reverse order.
type pathNode struct {
cave cave
prev *pathNode
}
// explore returns the number of paths from currentCave to "end", given an already travelled path of path and caves for
// valid transitions between caves according to the rules of the part.
func explore(caves map[cave][]cave, currentCave cave, path *pathNode) int {
// a path has been found
if currentCave.name == "end" {
return 1
}
currentPath := &pathNode{
cave: currentCave,
prev: path,
}
// add up the number of paths from taking all the possible caves we could go to from this cave
paths := 0
for _, nextCave := range caves[currentCave] {
// if we've done this transition before, don't do it again to avoid infinite loops
// if we're visiting a small cave again, don't - that's not allowed
if transitionedBefore(currentCave, nextCave, currentPath) || visitingSmallAgain(nextCave, currentPath) {
continue
}
paths += explore(caves, nextCave, currentPath)
}
return paths
}
func transitionedBefore(from, to cave, path *pathNode) bool {
var seen bool
lastCave := cave{}
seenTwice := iterate(path, func(cave cave) bool {
if cave == from && lastCave == to {
if seen {
return false
}
seen = true
}
lastCave = cave
return true
})
return seenTwice
}
func visitingSmallAgain(current cave, path *pathNode) bool {
if !current.small {
return false
}
foundSmall := iterate(path, func(cave cave) bool {
if current == cave {
return false
}
return true
})
return foundSmall
}
// iterate traverses through the path ending at path, performing an action at each cave.
// it returns true if it was told to stop early
func iterate(path *pathNode, action func(cave cave) bool) bool {
for path != nil {
cont := action(path.cave)
if !cont {
return true
}
path = path.prev
}
return false
}
func main() {
solve(strings.NewReader("start-A\nstart-b\nA-c\nA-b\nb-d\nA-end\nb-end"))
solve(strings.NewReader("fs-end\nhe-DX\nfs-he\nstart-DX\npj-DX\nend-zg\nzg-sl\nzg-pj\npj-he\nRW-he\nfs-DX\npj-RW\nzg-RW\nstart-pj\nhe-WI\nzg-he\npj-fs\nstart-RW"))
solve(input())
}