mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Refactor common code out
This commit is contained in:
@@ -29,6 +29,22 @@ fn print_cavern(cavern: &IndexSet, max_height: usize) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn collides(shape: &[&[bool]], cavern: &IndexSet, x: usize, y: usize) -> bool {
|
||||||
|
for (row, line) in shape.iter().enumerate() {
|
||||||
|
if x + line.len() > WIDTH {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (col, &on) in line.iter().enumerate() {
|
||||||
|
if on && cavern.contains((y - row) * WIDTH + x + col) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
pub fn part1(input: &[u8]) -> Result<String> {
|
pub fn part1(input: &[u8]) -> Result<String> {
|
||||||
// Poor man's trim()
|
// Poor man's trim()
|
||||||
let input = if input[input.len() - 1] == b'\n' {
|
let input = if input[input.len() - 1] == b'\n' {
|
||||||
@@ -50,25 +66,9 @@ pub fn part1(input: &[u8]) -> Result<String> {
|
|||||||
let mut y = max_height + shape.len() + 2;
|
let mut y = max_height + shape.len() + 2;
|
||||||
|
|
||||||
// Acquire gust of wind
|
// Acquire gust of wind
|
||||||
'falling: for offset in gusts.by_ref() {
|
for offset in gusts.by_ref() {
|
||||||
if let Some(nx) = x.checked_add_signed(offset) {
|
if let Some(nx) = x.checked_add_signed(offset) {
|
||||||
let mut should_move = true;
|
if !collides(shape, &cavern, nx, y) {
|
||||||
|
|
||||||
'collision: for (row, line) in shape.iter().enumerate() {
|
|
||||||
if nx + line.len() > WIDTH {
|
|
||||||
should_move = false;
|
|
||||||
break 'collision;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (col, &on) in line.iter().enumerate() {
|
|
||||||
if on && cavern.contains((y - row) * WIDTH + nx + col) {
|
|
||||||
should_move = false;
|
|
||||||
break 'collision;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if should_move {
|
|
||||||
x = nx;
|
x = nx;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -76,19 +76,10 @@ pub fn part1(input: &[u8]) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Move down if possible
|
// Move down if possible
|
||||||
if y >= shape.len() {
|
if y >= shape.len() && !collides(shape, &cavern, x, y - 1) {
|
||||||
let ny = y - 1;
|
y -= 1;
|
||||||
for (row, line) in shape.iter().enumerate() {
|
|
||||||
// No width check, should not hit that on the way down.
|
|
||||||
for (col, &on) in line.iter().enumerate() {
|
|
||||||
if on && cavern.contains((ny - row) * WIDTH + x + col) {
|
|
||||||
break 'falling;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
y = ny;
|
|
||||||
} else {
|
} else {
|
||||||
break 'falling;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user