1
0
Fork 0

day(24): refactor the modular arithmetics

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-07-07 13:31:44 +02:00
parent ad42be0cb7
commit f3b7ead65b
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -39,31 +39,41 @@ impl Index<Position> for Basin {
}
// We need to account for the loops of the blizzards
let x_mod = self.cols - 2;
let y_mod = self.rows - 2;
let width = self.cols - 2;
let height = self.rows - 2;
let x_w = ((index.x() - 1 + x_mod - (index.z() % x_mod)) % x_mod + 1) as usize;
let x_e = ((index.x() - 1 + x_mod + (index.z() % x_mod)) % x_mod + 1) as usize;
let y_n = ((index.y() - 1 + y_mod - (index.z() % y_mod)) % y_mod + 1) as usize;
let y_s = ((index.y() - 1 + y_mod + (index.z() % y_mod)) % y_mod + 1) as usize;
if self.map[index.y() as usize][x_w] == '>' {
return &self.map[index.y() as usize][x_w];
let blizzard_origin = |size, d, t, i| ((i - 1 + size + d * (t % size)) % size + 1) as usize;
[
(
index.y() as usize,
blizzard_origin(width, -1, index.z(), index.x()),
'>',
),
(
index.y() as usize,
blizzard_origin(width, 1, index.z(), index.x()),
'<',
),
(
blizzard_origin(height, -1, index.z(), index.y()),
index.x() as usize,
'v',
),
(
blizzard_origin(height, 1, index.z(), index.y()),
index.x() as usize,
'^',
),
]
.iter()
.find_map(|&(y, x, direction)| {
if self.map[y][x] == direction {
Some(&self.map[y][x])
} else {
None
}
if self.map[index.y() as usize][x_e] == '<' {
return &self.map[index.y() as usize][x_e];
}
if self.map[y_n][index.x() as usize] == 'v' {
return &self.map[y_n][index.x() as usize];
}
if self.map[y_s][index.x() as usize] == '^' {
return &self.map[y_s][index.x() as usize];
}
&'.'
})
.unwrap_or(&'.')
}
}