-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (92 loc) · 1.75 KB
/
main.go
File metadata and controls
117 lines (92 loc) · 1.75 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
package main
import (
"fmt"
"github.com/danvolchek/AdventOfCode/lib"
)
type node struct {
x, y int
adjacent []*node
}
func (n *node) Id() string {
return fmt.Sprintf("%d,%d", n.y, n.x)
}
func (n *node) Adjacent() []*node {
return n.adjacent
}
func (n *node) String() string {
return n.Id()
}
func parse(char byte) int {
if char == 'S' {
return 9999999
}
if char == 'E' {
return -2
}
return int(char - 'a')
}
func solve(grid [][]int) int {
var start *node
var end string
gridMap := make(map[int]map[int]*node)
for y, line := range grid {
for x, height := range line {
n := &node{
x: x,
y: y,
adjacent: nil,
}
if height == 9999999 {
start = n
}
if height == -2 {
end = n.Id()
}
if gridMap[y] == nil {
gridMap[y] = make(map[int]*node)
}
gridMap[y][x] = n
if height == -2 {
grid[y][x] = parse('z')
}
}
}
for y, line := range grid {
for x, height := range line {
for iy := -1; iy <= 1; iy += 1 {
for ix := -1; ix <= 1; ix += 1 {
if iy == 0 && ix == 0 {
continue
}
if lib.Abs(iy)+lib.Abs(ix) == 2 {
continue
}
ey := y + iy
ex := x + ix
if ey < 0 || ex < 0 || ey >= len(grid) || ex >= len(grid[ey]) {
continue
}
if height >= grid[ey][ex]-1 {
gridMap[y][x].adjacent = append(gridMap[y][x].adjacent, gridMap[ey][ex])
}
}
}
}
}
result, ok := lib.BFS(start, func(n *node) bool {
return n.Id() == end
})
if !ok {
panic("not found")
}
fmt.Println(result)
return len(result) - 1
}
func main() {
solver := lib.Solver[[][]int, int]{
ParseF: lib.ParseGrid(parse),
SolveF: solve,
}
solver.Expect("Sabqponm\nabcryxxl\naccszExk\nacctuvwj\nabdefghi", 31)
solver.Verify(330)
}