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
157
158
159
160
161
162
163
164
165
166
167
|
package main
import (
"bufio"
"fmt"
"log"
"os"
)
const (
startSquare = 'S'
endSquare = 'E'
)
func byte2height(b byte) int {
return int(b - 'a')
}
type square struct {
x, y, i int
height int
}
type heightmap struct {
squares [][]*square
work []*square
pathlog map[int]*square
distances map[int]int
}
func (h *heightmap) distance(s *square, o *square) int {
return 1
}
func (h *heightmap) neighbors(s *square, pred func(n, s *square) bool) []*square {
potential := []*square{}
if s.x > 0 {
potential = append(potential, h.squares[s.y][s.x-1])
}
if s.x < len(h.squares[0])-1 {
potential = append(potential, h.squares[s.y][s.x+1])
}
if s.y > 0 {
potential = append(potential, h.squares[s.y-1][s.x])
}
if s.y < len(h.squares)-1 {
potential = append(potential, h.squares[s.y+1][s.x])
}
ns := []*square{}
for _, n := range potential {
if pred(n, s) {
ns = append(ns, n)
}
}
return ns
}
func (hm *heightmap) shortestPath(
start *square,
pred func(sqr *square) bool,
neighborPred func(n, s *square) bool,
) ([]*square, bool) {
infinity := len(hm.squares) * len(hm.squares[0])
hm.work = make([]*square, len(hm.squares)*len(hm.squares[0]))
for i := range hm.squares {
for j := range hm.squares[i] {
s := hm.squares[i][j]
hm.distances[s.i] = infinity
hm.pathlog[s.i] = nil
hm.work[j+(i*len(hm.squares[0]))] = s
}
}
hm.distances[start.i] = 0
var sqr *square
for len(hm.work) > 0 {
var i int
sqr = hm.work[0]
for j, sqrmin := range hm.work {
if hm.distances[sqrmin.i] < hm.distances[sqr.i] {
sqr = sqrmin
i = j
}
}
if sqr == nil {
log.Fatal("No min distance square found")
}
hm.work[i], hm.work[len(hm.work)-1] = hm.work[len(hm.work)-1], hm.work[i]
hm.work = hm.work[:len(hm.work)-1]
if pred(sqr) {
break
}
for _, n := range hm.neighbors(sqr, neighborPred) {
d := hm.distances[sqr.i] + hm.distance(sqr, n)
if d < hm.distances[n.i] {
hm.distances[n.i] = d
hm.pathlog[n.i] = sqr
}
}
}
path := []*square{}
if hm.pathlog[sqr.i] == nil {
return path, false
}
ptr := sqr.i
for hm.pathlog[ptr] != nil {
p := hm.pathlog[ptr]
path = append(path, p)
ptr = p.i
}
return path, true
}
func main() {
f, err := os.Open("day12.txt")
if err != nil {
log.Fatal("Could not read input data", err)
}
s := bufio.NewScanner(f)
y := 0
d := make(map[int]int)
hm := &heightmap{
pathlog: make(map[int]*square),
distances: d,
}
var start, end *square
for s.Scan() {
squareRow := make([]*square, len(s.Bytes()))
for x, c := range s.Bytes() {
if c == 'S' {
s := &square{x, y, x + (y * len(s.Bytes())), byte2height('a')}
squareRow[x] = s
start = s
} else if c == 'E' {
s := &square{x, y, x + (y * len(s.Bytes())), byte2height('z')}
squareRow[x] = s
end = s
} else {
s := &square{x, y, x + (y * len(s.Bytes())), byte2height(c)}
squareRow[x] = s
}
}
hm.squares = append(hm.squares, squareRow)
y++
}
p1path, ok := hm.shortestPath(start, func(sqr *square) bool {
return sqr == end
}, func(n, s *square) bool {
return (n.height - s.height) <= 1
})
if ok {
fmt.Println("Part 1:", len(p1path))
}
p2path, ok := hm.shortestPath(end, func(sqr *square) bool {
return sqr.height == byte2height('a')
}, func(n, s *square) bool {
return (s.height - n.height) <= 1
})
if ok {
fmt.Println("Part 2:", len(p2path))
}
}
|