diff --git a/README.md b/README.md index 87c2602..43d7d95 100644 --- a/README.md +++ b/README.md @@ -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** --- diff --git a/src/bin/16.rs b/src/bin/16.rs index e92ea69..b8ae449 100644 --- a/src/bin/16.rs +++ b/src/bin/16.rs @@ -27,19 +27,16 @@ pub fn part_two(input: &str) -> Option { } 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) => {} @@ -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 {