️ Day 4: Performance and cleanup

This commit is contained in:
LeMoonStar 2024-12-04 19:45:57 +01:00
parent 2d89799e44
commit d25a9aad57

View file

@ -1,21 +1,7 @@
use crate::dprintln;
use super::{Answer, Day, DayImpl}; use super::{Answer, Day, DayImpl};
const CURRENT_DAY: u8 = 4; const CURRENT_DAY: u8 = 4;
#[derive(Debug, Clone, Copy)]
enum Direction {
East,
West,
North,
South,
NorthEast,
NorthWest,
SouthEast,
SouthWest,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct LetterWall(Vec<Vec<char>>); pub struct LetterWall(Vec<Vec<char>>);
@ -31,8 +17,8 @@ impl LetterWall {
} }
#[rustfmt::skip] #[rustfmt::skip]
fn get_xmas_directions(&self, x: usize, y: usize) -> Vec<Direction> { fn get_xmas_direction_count(&self, x: usize, y: usize) -> u64 {
let mut directions = vec!(); let mut directions = 0;
let map = ( let map = (
self.at(x, y), self.at(x+1, y), self.at(x+2, y), self.at(x+3, y), self.at(x, y), self.at(x+1, y), self.at(x+2, y), self.at(x+3, y),
@ -47,7 +33,7 @@ impl LetterWall {
_, _, _, _, _, _, _, _,
_, _, _, _ _, _, _, _
) = map { ) = map {
directions.push(Direction::East); directions += 1;
} }
if let ( if let (
@ -56,7 +42,7 @@ impl LetterWall {
_, _, _, _, _, _, _, _,
_, _, _, _ _, _, _, _
) = map { ) = map {
directions.push(Direction::West); directions += 1;
} }
if let ( if let (
@ -65,7 +51,7 @@ impl LetterWall {
Some('A'), _, _, _, Some('A'), _, _, _,
Some('S'), _, _, _ Some('S'), _, _, _
) = map { ) = map {
directions.push(Direction::South); directions += 1;
} }
if let ( if let (
@ -74,7 +60,7 @@ impl LetterWall {
Some('M'), _, _, _, Some('M'), _, _, _,
Some('X'), _, _, _ Some('X'), _, _, _
) = map { ) = map {
directions.push(Direction::North); directions += 1;
} }
if let ( if let (
@ -83,7 +69,7 @@ impl LetterWall {
_, _, Some('A'), _, _, _, Some('A'), _,
_, _, _, Some('S') _, _, _, Some('S')
) = map { ) = map {
directions.push(Direction::SouthEast); directions += 1;
} }
if let ( if let (
@ -92,7 +78,7 @@ impl LetterWall {
_, _, Some('M'), _, _, _, Some('M'), _,
_, _, _, Some('X') _, _, _, Some('X')
) = map { ) = map {
directions.push(Direction::NorthWest); directions += 1;
} }
if let ( if let (
@ -101,7 +87,7 @@ impl LetterWall {
_, Some('A'), _, _, _, Some('A'), _, _,
Some('S'), _, _, _ Some('S'), _, _, _
) = map { ) = map {
directions.push(Direction::SouthWest); directions += 1;
} }
if let ( if let (
@ -110,7 +96,7 @@ impl LetterWall {
_, Some('M'), _, _, _, Some('M'), _, _,
Some('X'), _, _, _ Some('X'), _, _, _
) = map { ) = map {
directions.push(Direction::SouthEast); directions += 1;
} }
directions directions
@ -124,24 +110,24 @@ impl LetterWall {
self.at( x, y+2), self.at( x+1, y+2), self.at( x+2, y+2) self.at( x, y+2), self.at( x+1, y+2), self.at( x+2, y+2)
) { ) {
( (
Some('M'), _,Some('S'), Some('M'), _, Some('S'),
_, Some('A'), _, _, Some('A'), _,
Some('M'), _, Some('S'), Some('M'), _, Some('S'),
) => true, ) => true,
( (
Some('S'), _,Some('S'), Some('S'), _, Some('S'),
_, Some('A'), _, _, Some('A'), _,
Some('M'), _, Some('M'), Some('M'), _, Some('M'),
) => true, ) => true,
( (
Some('M'), _,Some('M'), Some('M'), _, Some('M'),
_, Some('A'), _, _, Some('A'), _,
Some('S'), _, Some('S'), Some('S'), _, Some('S'),
) => true, ) => true,
( (
Some('S'), _,Some('M'), Some('S'), _, Some('M'),
_, Some('A'), _, _, Some('A'), _,
Some('S'), _, Some('M'), Some('S'), _, Some('M'),
) => true, ) => true,
_ => false _ => false
} }
@ -152,7 +138,7 @@ impl LetterWall {
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() {
count += self.get_xmas_directions(x, y).len(); count += self.get_xmas_direction_count(x, y);
} }
} }
return count as u64; return count as u64;