Day 4: Solved part 1!

This commit is contained in:
LeMoonStar 2024-12-04 19:33:28 +01:00
parent bb4c059b25
commit 134512e4ba

View file

@ -30,77 +30,100 @@ impl LetterWall {
} }
} }
fn is_upper_left_of_xmas(&self, x: usize, y: usize) -> Option<Direction> {
#[rustfmt::skip] #[rustfmt::skip]
match( fn get_xmas_directions(&self, x: usize, y: usize) -> Vec<Direction> {
self.at( x, y), self.at( x+1, y), self.at( x+2, y), self.at( x+3, y), let mut directions = vec!();
self.at( x, y+1), self.at( x+1, y+1), self.at( x+2, y+1), self.at( x+3, y+1),
self.at( x, y+2), self.at( x+1, y+2), self.at( x+2, y+2), self.at( x+3, y+2), let map = (
self.at( x, y+3), self.at( x+1, y+3), self.at( x+2, y+3), self.at( x+3, y+3) self.at(x, y), self.at(x+1, y), self.at(x+2, y), self.at(x+3, y),
) { self.at(x, y+1), self.at(x+1, y+1), self.at(x+2, y+1), self.at(x+3, y+1),
( self.at(x, y+2), self.at(x+1, y+2), self.at(x+2, y+2), self.at(x+3, y+2),
Some('X'), Some('M'), Some('A'), Some('S'), self.at(x, y+3), self.at(x+1, y+3), self.at(x+2, y+3), self.at(x+3, y+3)
);
if let (
Some(&'X'), Some(&'M'), Some(&'A'), Some(&'S'),
_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _,
_, _, _, _ _, _, _, _
) => Some(Direction::East), ) = map {
( directions.push(Direction::East);
}
if let (
Some('S'), Some('A'), Some('M'), Some('X'), Some('S'), Some('A'), Some('M'), Some('X'),
_, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _,
_, _, _, _ _, _, _, _
) => Some(Direction::West), ) = map {
( directions.push(Direction::West);
}
if let (
Some('X'), _, _, _, Some('X'), _, _, _,
Some('M'), _, _, _, Some('M'), _, _, _,
Some('A'), _, _, _, Some('A'), _, _, _,
Some('S'), _, _, _ Some('S'), _, _, _
) => Some(Direction::South), ) = map {
( directions.push(Direction::South);
}
if let (
Some('S'), _, _, _, Some('S'), _, _, _,
Some('A'), _, _, _, Some('A'), _, _, _,
Some('M'), _, _, _, Some('M'), _, _, _,
Some('X'), _, _, _ Some('X'), _, _, _
) => Some(Direction::North), ) = map {
( directions.push(Direction::North);
}
if let (
Some('X'), _, _, _, Some('X'), _, _, _,
_, Some('M'), _, _, _, Some('M'), _, _,
_, _, Some('A'), _, _, _, Some('A'), _,
_, _, _, Some('S') _, _, _, Some('S')
) => Some(Direction::SouthEast), ) = map {
( directions.push(Direction::SouthEast);
}
if let (
Some('S'), _, _, _, Some('S'), _, _, _,
_, Some('A'), _, _, _, Some('A'), _, _,
_, _, Some('M'), _, _, _, Some('M'), _,
_, _, _, Some('X') _, _, _, Some('X')
) => Some(Direction::NorthWest), ) = map {
( directions.push(Direction::NorthWest);
}
if let (
_, _, _, Some('X'), _, _, _, Some('X'),
_, _, Some('M'), _, _, _, Some('M'), _,
_, Some('A'), _, _, _, Some('A'), _, _,
Some('S'), _, _, _ Some('S'), _, _, _
) => Some(Direction::SouthWest), ) = map {
( directions.push(Direction::SouthWest);
}
if let (
_, _, _, Some('S'), _, _, _, Some('S'),
_, _, Some('A'), _, _, _, Some('A'), _,
_, Some('M'), _, _, _, Some('M'), _, _,
Some('X'), _, _, _ Some('X'), _, _, _
) => Some(Direction::NorthEast), ) = map {
_ => None directions.push(Direction::SouthEast);
} }
directions
} }
pub fn count_xmas(&self) -> u64 { pub fn count_xmas(&self) -> u64 {
let mut count = 0; let mut count = 0;
for y in 0..self.0.len() { for y in 0..self.0.len() {
for x in 0..self.0[0].len() { for x in 0..self.0[0].len() {
if let Some(_dir) = self.is_upper_left_of_xmas(x, y) { count += self.get_xmas_directions(x, y).len();
dprintln!("FOUND AT {} {} '{}' - DIR: {:?}", x, y, self.0[y][x], _dir);
count += 1;
} }
} }
} return count as u64;
return count;
} }
} }