Skip to content

Commit

Permalink
more correct, but slightly slower day 14 version
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 15, 2023
1 parent d6acc87 commit 30cbee5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 11](./src/bin/11.rs) | `16.3µs` | `15.7µs` |
| [Day 12](./src/bin/12.rs) | `137.9µs` | `618.3µs` |
| [Day 13](./src/bin/13.rs) | `12.3µs` | `15.9µs` |
| [Day 14](./src/bin/14.rs) | `26.4µs` | `4.3ms` |
| [Day 14](./src/bin/14.rs) | `25.0µs` | `4.5ms` |
| [Day 15](./src/bin/15.rs) | `20.4µs` | `85.9µs` |

**Total: 6.82ms**
**Total: 7.02ms**
<!--- benchmarking table --->

---
Expand Down
8 changes: 6 additions & 2 deletions src/bin/14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct Platform {
height: u8,
round_rocks: Vec<Coordinate>,
distance_to_cubed_rocks: Vec<Vec<CachedDistance>>,
stacks: Vec<Vec<u8>>,
iteration: u16,
}

Expand Down Expand Up @@ -106,6 +107,7 @@ impl Platform {
iteration: 0,
width,
height,
stacks: vec![vec![0; width as usize]; height as usize],
}
}

Expand All @@ -124,14 +126,16 @@ impl Platform {

fn tilt(&mut self, dir: Direction) {
self.iteration += 1;
let mut stacks = [[0; 100]; 100];
for s in &mut self.stacks {
s.fill(0);
}
for rock in &mut self.round_rocks {
let distance =
self.distance_to_cubed_rocks[rock.row as usize][rock.col as usize].get(dir);
let cubed_rock = rock
.move_in_dir(dir, distance)
.limit_to(self.height - 1, self.width - 1);
let stack = &mut stacks[cubed_rock.row as usize][cubed_rock.col as usize];
let stack = &mut self.stacks[cubed_rock.row as usize][cubed_rock.col as usize];
if *stack > distance - 1 {
let total_move_distance = *stack - distance + 1;
*rock = rock.move_in_dir(dir.rev(), total_move_distance);
Expand Down

0 comments on commit 30cbee5

Please sign in to comment.