Improve perf of day 4
This commit is contained in:
@@ -35,4 +35,4 @@ Timings are given as: [lower-bound **best-estimate** upper-bound]
|
|||||||
| 01 | [101.34 µs **101.95 µs** 102.61 µs] | [105.90 µs **106.40 µs** 106.95 µs] |
|
| 01 | [101.34 µs **101.95 µs** 102.61 µs] | [105.90 µs **106.40 µs** 106.95 µs] |
|
||||||
| 02 | [2.0990 ms **2.1113 ms** 2.1236 ms] | [2.0954 ms **2.1055 ms** 2.1157 ms] |
|
| 02 | [2.0990 ms **2.1113 ms** 2.1236 ms] | [2.0954 ms **2.1055 ms** 2.1157 ms] |
|
||||||
| 03 | [38.717 µs **39.002 µs** 39.311 µs] | [175.35 µs **176.59 µs** 177.92 µs] |
|
| 03 | [38.717 µs **39.002 µs** 39.311 µs] | [175.35 µs **176.59 µs** 177.92 µs] |
|
||||||
| 04 | [233.55 µs **234.88 µs** 236.20 µs] | [3.1026 ms **3.1174 ms** 3.1326 ms] |
|
| 04 | [147.02 µs **147.75 µs** 148.53 µs] | [1.5579 ms **1.5737 ms** 1.5900 ms] |
|
||||||
|
|||||||
@@ -8,6 +8,17 @@ use tracing::{debug, instrument};
|
|||||||
|
|
||||||
pub const INPUT: &str = include_str!("input/input.txt");
|
pub const INPUT: &str = include_str!("input/input.txt");
|
||||||
|
|
||||||
|
const ADJACENT_DELTAS: [(isize, isize); 8] = [
|
||||||
|
(-1, -1),
|
||||||
|
(-1, 0),
|
||||||
|
(-1, 1),
|
||||||
|
(0, -1),
|
||||||
|
(0, 1),
|
||||||
|
(1, -1),
|
||||||
|
(1, 0),
|
||||||
|
(1, 1),
|
||||||
|
];
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
enum Cell {
|
enum Cell {
|
||||||
Empty,
|
Empty,
|
||||||
@@ -77,14 +88,6 @@ impl<const R: usize, const C: usize> Display for Grid<R, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<const R: usize, const C: usize> Grid<R, C> {
|
impl<const R: usize, const C: usize> Grid<R, C> {
|
||||||
fn get_cell(&self, row: isize, col: isize) -> Option<Cell> {
|
|
||||||
if row >= 0 && col >= 0 && (row as usize) < R && (col as usize) < C {
|
|
||||||
Some(self.cells[row as usize][col as usize])
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn count_accessible_papers(&mut self, replace_with: Cell) -> usize {
|
fn count_accessible_papers(&mut self, replace_with: Cell) -> usize {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
for row in 0..R {
|
for row in 0..R {
|
||||||
@@ -93,15 +96,16 @@ impl<const R: usize, const C: usize> Grid<R, C> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut adjacent_papers = 0;
|
let mut adjacent_papers = 0;
|
||||||
for dr in -1..=1 {
|
for &(dr, dc) in &ADJACENT_DELTAS {
|
||||||
for dc in -1..=1 {
|
let adj_row = row as isize + dr;
|
||||||
if dr == 0 && dc == 0 {
|
let adj_col = col as isize + dc;
|
||||||
continue;
|
if adj_row >= 0
|
||||||
}
|
&& adj_col >= 0
|
||||||
let adjacent = self
|
&& (adj_row as usize) < R
|
||||||
.get_cell(row as isize + dr, col as isize + dc)
|
&& (adj_col as usize) < C
|
||||||
.unwrap_or(Cell::Empty);
|
{
|
||||||
if adjacent == Cell::Paper || adjacent == Cell::AccessiblePaper {
|
let adjacent = self.cells[adj_row as usize][adj_col as usize];
|
||||||
|
if matches!(adjacent, Cell::Paper | Cell::AccessiblePaper) {
|
||||||
adjacent_papers += 1;
|
adjacent_papers += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user