-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.go
67 lines (51 loc) · 1.51 KB
/
day12.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
package main
import (
"collections"
"os"
"panic"
"readers"
"strings"
)
func Day12Part1() int {
file, err := os.Open("assets/day12.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
caves := make(map[string][]string)
for _, line := range lines {
caveParts := strings.Split(line, "-")
caves[caveParts[0]] = append(caves[caveParts[0]], caveParts[1])
caves[caveParts[1]] = append(caves[caveParts[1]], caveParts[0])
}
return FindPaths("start", []string{}, caves, false)
}
func Day12Part2() int {
file, err := os.Open("assets/day12.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
caves := make(map[string][]string)
for _, line := range lines {
caveParts := strings.Split(line, "-")
caves[caveParts[0]] = append(caves[caveParts[0]], caveParts[1])
caves[caveParts[1]] = append(caves[caveParts[1]], caveParts[0])
}
return FindPaths("start", []string{}, caves, true)
}
func FindPaths(current string, visited []string, caves map[string][]string, extra bool) int {
total := 0
for _, direction := range caves[current] {
if direction == "end" {
total += 1
} else if direction == strings.ToUpper(direction) {
total += FindPaths(direction, visited, caves, extra)
} else if direction != "start" {
if !collections.ContainsString(visited, direction) {
total += FindPaths(direction, append(visited, direction), caves, extra)
} else if extra {
total += FindPaths(direction, visited, caves, false)
}
}
}
return total
}