Skip to content

Commit b99ba6a

Browse files
committed
Simplify cost checks and swap left/right up/down order
1 parent 28b35cb commit b99ba6a

File tree

2 files changed

+73
-77
lines changed

2 files changed

+73
-77
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Place input files in `input/yearYYYY/dayDD.txt` including leading zeroes. For ex
5353
## Performance
5454

5555
Benchmarks are measured using the built-in `cargo bench` tool run on an [Apple M2 Max][apple-link].
56-
All 225 solutions from 2023 to 2015 complete sequentially in **581 milliseconds**.
56+
All 225 solutions from 2023 to 2015 complete sequentially in **580 milliseconds**.
5757
Interestingly 84% of the total time is spent on just 9 solutions.
5858
Performance is reasonable even on older hardware, for example a 2011 MacBook Pro with an
5959
[Intel i7-2720QM][intel-link] processor takes 3.5 seconds to run the same 225 solutions.
@@ -62,7 +62,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
6262

6363
| Year | Benchmark (ms) |
6464
| --- | --: |
65-
| [2023](#2023) | 7 |
65+
| [2023](#2023) | 6 |
6666
| [2022](#2022) | 9 |
6767
| [2021](#2021) | 9 |
6868
| [2020](#2020) | 272 |
@@ -94,7 +94,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
9494
| 14 | [Parabolic Reflector Dish](https://adventofcode.com/2023/day/14) | [Source](src/year2023/day14.rs) | 632 |
9595
| 15 | [Lens Library](https://adventofcode.com/2023/day/15) | [Source](src/year2023/day15.rs) | 84 |
9696
| 16 | [The Floor Will Be Lava](https://adventofcode.com/2023/day/16) | [Source](src/year2023/day16.rs) | 826 |
97-
| 17 | [Clumsy Crucible](https://adventofcode.com/2023/day/17) | [Source](src/year2023/day17.rs) | 2568 |
97+
| 17 | [Clumsy Crucible](https://adventofcode.com/2023/day/17) | [Source](src/year2023/day17.rs) | 2320 |
9898
| 18 | [Lavaduct Lagoon](https://adventofcode.com/2023/day/18) | [Source](src/year2023/day18.rs) | 17 |
9999
| 19 | [Aplenty](https://adventofcode.com/2023/day/19) | [Source](src/year2023/day19.rs) | 100 |
100100
| 20 | [Pulse Propagation](https://adventofcode.com/2023/day/20) | [Source](src/year2023/day20.rs) | 6 |

src/year2023/day17.rs

Lines changed: 70 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ fn astar<const L: usize, const U: usize>(grid: &Grid<u32>) -> u32 {
5656

5757
let mut index = 0;
5858
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()];
6060

6161
// Start from the top left corner checking both vertical and horizontal directions.
6262
todo[0].push((0, 0, 0));
6363
todo[0].push((0, 0, 1));
6464

65+
cost[0][0] = 0;
66+
cost[0][1] = 0;
67+
6568
loop {
6669
// All items in the same bucket have the same priority.
6770
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 {
8386
if direction == 0 {
8487
// We just moved vertically so now check both left and right directions.
8588

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;
108110
}
109111
}
110112

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;
128128
}
129129
}
130130
} else {
131131
// We just moved horizontally so now check both up and down directions.
132132

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;
150148
}
151149
}
152150

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;
170166
}
171167
}
172168
}

0 commit comments

Comments
 (0)