@@ -56,12 +56,15 @@ fn astar<const L: usize, const U: usize>(grid: &Grid<u32>) -> u32 {
56
56
57
57
let mut index = 0 ;
58
58
let mut todo = ( 0 ..100 ) . map ( |_| Vec :: with_capacity ( 1000 ) ) . collect :: < Vec < _ > > ( ) ;
59
- let mut cost = vec ! [ [ 0_u32 ; 2 ] ; heat. len( ) ] ;
59
+ let mut cost = vec ! [ [ u32 :: MAX ; 2 ] ; heat. len( ) ] ;
60
60
61
61
// Start from the top left corner checking both vertical and horizontal directions.
62
62
todo[ 0 ] . push ( ( 0 , 0 , 0 ) ) ;
63
63
todo[ 0 ] . push ( ( 0 , 0 , 1 ) ) ;
64
64
65
+ cost[ 0 ] [ 0 ] = 0 ;
66
+ cost[ 0 ] [ 1 ] = 0 ;
67
+
65
68
loop {
66
69
// All items in the same bucket have the same priority.
67
70
while let Some ( ( x, y, direction) ) = todo[ index % 100 ] . pop ( ) {
@@ -83,90 +86,83 @@ fn astar<const L: usize, const U: usize>(grid: &Grid<u32>) -> u32 {
83
86
if direction == 0 {
84
87
// We just moved vertically so now check both left and right directions.
85
88
86
- // Left
87
- {
88
- let mut index = index;
89
- let mut steps = steps;
90
-
91
- // Each direction loop is the same:
92
- // * Check to see if we gone out of bounds
93
- // * Increase the cost by the "heat" of the square we've just moved into.
94
- // * Check if we've already been to this location with a lower cost.
95
- // * Add new state to priority queue.
96
- for i in 1 ..=U {
97
- if i > x {
98
- break ;
99
- }
100
-
101
- index -= 1 ;
102
- steps += heat[ index] ;
103
-
104
- if i >= L && ( cost[ index] [ 1 ] == 0 || steps < cost[ index] [ 1 ] ) {
105
- todo[ heuristic ( x - i, y, steps) ] . push ( ( x - i, y, 1 ) ) ;
106
- cost[ index] [ 1 ] = steps;
107
- }
89
+ // Each direction loop is the same:
90
+ // * Check to see if we gone out of bounds
91
+ // * Increase the cost by the "heat" of the square we've just moved into.
92
+ // * Check if we've already been to this location with a lower cost.
93
+ // * Add new state to priority queue.
94
+
95
+ // Right
96
+ let mut next = index;
97
+ let mut extra = steps;
98
+
99
+ for i in 1 ..=U {
100
+ if x + i >= width {
101
+ break ;
102
+ }
103
+
104
+ next += 1 ;
105
+ extra += heat[ next] ;
106
+
107
+ if i >= L && extra < cost[ next] [ 1 ] {
108
+ todo[ heuristic ( x + i, y, extra) ] . push ( ( x + i, y, 1 ) ) ;
109
+ cost[ next] [ 1 ] = extra;
108
110
}
109
111
}
110
112
111
- // Right
112
- {
113
- let mut index = index;
114
- let mut steps = steps;
115
-
116
- for i in 1 ..=U {
117
- if x + i >= width {
118
- break ;
119
- }
120
-
121
- index += 1 ;
122
- steps += heat[ index] ;
123
-
124
- if i >= L && ( cost[ index] [ 1 ] == 0 || steps < cost[ index] [ 1 ] ) {
125
- todo[ heuristic ( x + i, y, steps) ] . push ( ( x + i, y, 1 ) ) ;
126
- cost[ index] [ 1 ] = steps;
127
- }
113
+ // Left
114
+ let mut next = index;
115
+ let mut extra = steps;
116
+
117
+ for i in 1 ..=U {
118
+ if i > x {
119
+ break ;
120
+ }
121
+
122
+ next -= 1 ;
123
+ extra += heat[ next] ;
124
+
125
+ if i >= L && extra < cost[ next] [ 1 ] {
126
+ todo[ heuristic ( x - i, y, extra) ] . push ( ( x - i, y, 1 ) ) ;
127
+ cost[ next] [ 1 ] = extra;
128
128
}
129
129
}
130
130
} else {
131
131
// We just moved horizontally so now check both up and down directions.
132
132
133
- // Up
134
- {
135
- let mut index = index;
136
- let mut steps = steps;
137
-
138
- for i in 1 ..=U {
139
- if i > y {
140
- break ;
141
- }
142
-
143
- index -= width;
144
- steps += heat[ index] ;
145
-
146
- if i >= L && ( cost[ index] [ 0 ] == 0 || steps < cost[ index] [ 0 ] ) {
147
- todo[ heuristic ( x, y - i, steps) ] . push ( ( x, y - i, 0 ) ) ;
148
- cost[ index] [ 0 ] = steps;
149
- }
133
+ // Down
134
+ let mut next = index;
135
+ let mut extra = steps;
136
+
137
+ for i in 1 ..=U {
138
+ if y + i >= height {
139
+ break ;
140
+ }
141
+
142
+ next += width;
143
+ extra += heat[ next] ;
144
+
145
+ if i >= L && extra < cost[ next] [ 0 ] {
146
+ todo[ heuristic ( x, y + i, extra) ] . push ( ( x, y + i, 0 ) ) ;
147
+ cost[ next] [ 0 ] = extra;
150
148
}
151
149
}
152
150
153
- // Down
154
- {
155
- let mut index = index;
156
- let mut steps = steps;
157
-
158
- for i in 1 ..=U {
159
- if y + i >= height {
160
- break ;
161
- }
162
-
163
- index += width;
164
- steps += heat[ index] ;
165
-
166
- if i >= L && ( cost[ index] [ 0 ] == 0 || steps < cost[ index] [ 0 ] ) {
167
- todo[ heuristic ( x, y + i, steps) ] . push ( ( x, y + i, 0 ) ) ;
168
- cost[ index] [ 0 ] = steps;
169
- }
151
+ // Up
152
+ let mut next = index;
153
+ let mut extra = steps;
154
+
155
+ for i in 1 ..=U {
156
+ if i > y {
157
+ break ;
158
+ }
159
+
160
+ next -= width;
161
+ extra += heat[ next] ;
162
+
163
+ if i >= L && extra < cost[ next] [ 0 ] {
164
+ todo[ heuristic ( x, y - i, extra) ] . push ( ( x, y - i, 0 ) ) ;
165
+ cost[ next] [ 0 ] = extra;
170
166
}
171
167
}
172
168
}
0 commit comments