finished day04
This commit is contained in:
64
src/day04/common.rs
Normal file
64
src/day04/common.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
pub fn parse_to_floor(input: String) -> Vec<Vec<u8>> {
|
||||
let mut rows: Vec<Vec<u8>> = Vec::with_capacity(input.len() + 2);
|
||||
rows.push(vec![0u8; input.len()]);
|
||||
rows.append(
|
||||
&mut input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let mut col: Vec<u8> = Vec::with_capacity(line.len() + 2);
|
||||
col.push(0u8);
|
||||
col.append(
|
||||
&mut line
|
||||
.chars()
|
||||
.map(|c| match c {
|
||||
'@' => 1u8,
|
||||
_ => 0u8,
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
col.push(0u8);
|
||||
col
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
rows.push(vec![0u8; input.len()]);
|
||||
|
||||
rows
|
||||
}
|
||||
|
||||
pub fn remove_if_possible(floor: &mut Vec<Vec<u8>>) -> Option<u64> {
|
||||
let mut removed: Vec<(usize, usize)> = Vec::new();
|
||||
|
||||
floor
|
||||
.windows(3)
|
||||
.enumerate()
|
||||
.for_each(|(r, rows)| {
|
||||
rows[0]
|
||||
.iter()
|
||||
.zip(rows[1].iter())
|
||||
.zip(rows[2].iter())
|
||||
.map(|((a, b), c)| (a, b, c))
|
||||
.collect::<Vec<(&u8, &u8, &u8)>>()
|
||||
.windows(3)
|
||||
.enumerate()
|
||||
.for_each(|(c, cols)| {
|
||||
if cols[1].1 == &0u8 {
|
||||
return;
|
||||
}
|
||||
if cols.iter().map(|(a, b, c)| *a + *b + *c).sum::<u8>() - 1 < 4 {
|
||||
removed.push((r+1, c+1));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
removed.iter().for_each(|(r, c)| {
|
||||
floor[*r][*c] = 0;
|
||||
});
|
||||
|
||||
if removed.len() == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(removed.len() as u64)
|
||||
}
|
||||
Reference in New Issue
Block a user