Skip to content

Commit

Permalink
small optimizations for day 16
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 16, 2023
1 parent 1422273 commit be01b79
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 13](./src/bin/13.rs) | `12.3µs` | `15.9µs` |
| [Day 14](./src/bin/14.rs) | `25.0µs` | `4.5ms` |
| [Day 15](./src/bin/15.rs) | `20.4µs` | `85.9µs` |
| [Day 16](./src/bin/16.rs) | `335.3µs` | `17.0ms` |
| [Day 16](./src/bin/16.rs) | `332.7µs` | `16.7ms` |

**Total: 24.35ms**
**Total: 24.05ms**
<!--- benchmarking table --->

---
Expand Down
15 changes: 6 additions & 9 deletions src/bin/16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,16 @@ pub fn part_two(input: &str) -> Option<u32> {
}

fn energize_count(grid: &Grid, start: Coordinate, start_dir: Direction) -> u32 {
let mut energized = vec![vec![false; grid.width]; grid.height];
let mut energized_count = 1;
let mut energized = vec![false; grid.height * grid.width];
let mut queue = VecDeque::new();
energized[start.row][start.col] = true;
energized[start.row * grid.width + start.col] = true;
queue.push_back((start, start_dir));
while let Some((cur, dir)) = queue.pop_front() {
for (next, next_dir) in grid.neighbors(cur, dir) {
if let Some(tile) = grid.get_tile(next) {
let was_energized = energized[next.row][next.col];
energized[next.row][next.col] = true;
if !was_energized {
energized_count += 1;
}
let i = next.row * grid.width + next.col;
let was_energized = energized[i];
energized[i] = true;
match (tile, was_energized) {
// If we hit a splitter that was already energized, we know we are entering a loop so we can stop
(Tile::HorizontalSplitter, true) | (Tile::VerticalSplitter, true) => {}
Expand All @@ -50,7 +47,7 @@ fn energize_count(grid: &Grid, start: Coordinate, start_dir: Direction) -> u32 {
}
}
}
energized_count
energized.into_iter().filter(|&e| e).count() as u32
}

struct Grid {
Expand Down

0 comments on commit be01b79

Please sign in to comment.