mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Avoid 2/3 hashmap lookups
This commit is contained in:
@@ -39,27 +39,29 @@ fn find_dimensions(field: &Field) -> ((i32, i32), (i32, i32)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn advance(translation: &Translation, field: &Field, new_field: &mut Field, infinity: &mut bool) {
|
fn advance(translation: &Translation, field: &Field, new_field: &mut Field, infinity: &mut bool) {
|
||||||
|
const INDEX_MASK: usize = (1 << 9) - 1;
|
||||||
new_field.clear();
|
new_field.clear();
|
||||||
|
|
||||||
let ((xmin, xmax), (ymin, ymax)) = find_dimensions(field);
|
let ((xmin, xmax), (ymin, ymax)) = find_dimensions(field);
|
||||||
|
|
||||||
for x in (xmin - 1)..=(xmax + 1) {
|
for x in (xmin - 1)..=(xmax + 1) {
|
||||||
|
let mut index = if *infinity { INDEX_MASK } else { 0 };
|
||||||
|
|
||||||
for y in (ymin - 1)..=(ymax + 1) {
|
for y in (ymin - 1)..=(ymax + 1) {
|
||||||
let mut index = 0;
|
|
||||||
for dy in -1..=1 {
|
|
||||||
for dx in -1..=1 {
|
for dx in -1..=1 {
|
||||||
index <<= 1;
|
index <<= 1;
|
||||||
|
|
||||||
let nx = x + dx;
|
let nx = x + dx;
|
||||||
let ny = y + dy;
|
let ny = y + 1;
|
||||||
|
|
||||||
if nx < xmin || nx > xmax || ny < ymin || ny > ymax {
|
if nx < xmin || nx > xmax || ny < ymin || ny > ymax {
|
||||||
index |= *infinity as usize;
|
index |= *infinity as usize;
|
||||||
} else if field.contains(&(x + dx, y + dy)) {
|
} else if field.contains(&(nx, ny)) {
|
||||||
index |= 1;
|
index |= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
index &= INDEX_MASK;
|
||||||
|
|
||||||
if translation[index] {
|
if translation[index] {
|
||||||
new_field.insert((x, y));
|
new_field.insert((x, y));
|
||||||
|
|||||||
Reference in New Issue
Block a user