-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (77 loc) · 1.43 KB
/
main.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
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
)
type NodeType int
const (
Start NodeType = iota
End
Neither
)
type Node struct {
height int
nodeType NodeType
adjacent []*Node
}
func (n *Node) String() string {
switch n.nodeType {
case Start:
return "S"
case End:
return "E"
default:
return string(rune('a' + n.height))
}
}
func (n *Node) Adjacent() []*Node {
return n.adjacent
}
func parse(char byte) *Node {
var height int
var nodeType NodeType
switch char {
case 'S':
nodeType = Start
height = 0
case 'E':
nodeType = End
height = 26
default:
nodeType = Neither
height = int(char - 'a')
}
return &Node{
height: height,
nodeType: nodeType,
}
}
func solve(grid [][]*Node) int {
var end *Node
for y, line := range grid {
for x, node := range line {
if node.nodeType == End {
end = node
}
adjacent := lib.Adjacent[*Node](y, x, false)
reachable := lib.Filter(adjacent, func(n *Node) bool {
return n.height >= node.height-1 // reversed from part 1, since we're going the opposite direction
})
node.adjacent = reachable
}
}
path, ok := lib.BFS(end, func(n *Node) bool {
return n.height == 0
})
if !ok {
panic("not found")
}
return len(path) - 1
}
func main() {
solver := lib.Solver[[][]*Node, int]{
ParseF: lib.ParseGrid(parse),
SolveF: solve,
}
solver.Expect("Sabqponm\nabcryxxl\naccszExk\nacctuvwj\nabdefghi", 29)
solver.Verify(321)
}