Skip to content

Commit

Permalink
refactor day 16 slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 16, 2023
1 parent eae96b7 commit f7a5436
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 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) | `177.4µs` | `8.2ms` |
| [Day 16](./src/bin/16.rs) | `178.8µs` | `8.1ms` |

**Total: 15.39ms**
**Total: 15.29ms**
<!--- benchmarking table --->

---
Expand Down
18 changes: 5 additions & 13 deletions src/bin/16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ fn energize_count(grid: &Grid, start: Coordinate, start_dir: Direction) -> u32 {
energized[start.row * grid.width + start.col] = true;
queue.push_back((start, start_dir, grid.get_tile(start)));
while let Some((cur, dir, tile)) = queue.pop_front() {
for (next, next_dir, tile) in grid.neighbors(cur, dir, tile) {
for (next, next_dir) in tile
.next(dir)
.filter_map(|dir| grid.move_in_dir(cur, dir).map(|c| (c, dir)))
{
let tile = grid.get_tile(next);
let i = next.row * grid.width + next.col;
let was_energized = energized[i];
energized[i] = true;
Expand Down Expand Up @@ -78,18 +82,6 @@ enum Direction {
}

impl Grid {
pub fn neighbors(
&self,
coord: Coordinate,
dir: Direction,
tile: Tile,
) -> impl Iterator<Item = (Coordinate, Direction, Tile)> + '_ {
tile.next(dir).filter_map(move |dir| {
self.move_in_dir(coord, dir)
.map(|c| (c, dir, self.get_tile(c)))
})
}

pub fn get_tile(&self, coord: Coordinate) -> Tile {
self.tiles[coord.row * self.width + coord.col]
}
Expand Down

0 comments on commit f7a5436

Please sign in to comment.