From 30cbee5b682021d8e6f0ab6b220a56c85a24ba3f Mon Sep 17 00:00:00 2001 From: Kevin Caffrey Date: Fri, 15 Dec 2023 18:54:09 -0500 Subject: [PATCH] more correct, but slightly slower day 14 version --- README.md | 4 ++-- src/bin/14.rs | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9b067b8..3685583 100644 --- a/README.md +++ b/README.md @@ -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** --- diff --git a/src/bin/14.rs b/src/bin/14.rs index 3618113..b9de700 100644 --- a/src/bin/14.rs +++ b/src/bin/14.rs @@ -61,6 +61,7 @@ struct Platform { height: u8, round_rocks: Vec, distance_to_cubed_rocks: Vec>, + stacks: Vec>, iteration: u16, } @@ -106,6 +107,7 @@ impl Platform { iteration: 0, width, height, + stacks: vec![vec![0; width as usize]; height as usize], } } @@ -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);