day4 part2

This commit is contained in:
Joe Ardent 2024-12-22 16:19:08 -08:00
parent b7bc7f15a3
commit aaae2a326c

View file

@ -1,3 +1,5 @@
#![feature(let_chains)]
static MAS: [char; 3] = ['M', 'A', 'S']; static MAS: [char; 3] = ['M', 'A', 'S'];
static DIRS: [Dir; 8] = [ static DIRS: [Dir; 8] = [
Dir::N, Dir::N,
@ -50,8 +52,48 @@ fn find_xmas(board: &Board, loc: (usize, usize), dir: Dir) -> bool {
true true
} }
fn pt2(_input: &str) -> u32 { fn pt2(input: &str) -> u32 {
0 let mut total = 0;
let board = Board::new(input);
for (r, row) in board.rows.iter().enumerate() {
for (c, letter) in row.iter().enumerate() {
if letter == &'A' {
if find_x_mas(&board, (r, c)) {
total += 1;
}
}
}
}
total
}
fn find_x_mas(board: &Board, loc: (usize, usize)) -> bool {
let one = if let Some(&nw) = (board.next(loc, Dir::NW))
&& let Some(&se) = board.next(loc, Dir::SE)
{
if (nw == 'M' && se == 'S') || (nw == 'S' && se == 'M') {
true
} else {
false
}
} else {
false
};
let two = if let Some(&ne) = (board.next(loc, Dir::NE))
&& let Some(&sw) = board.next(loc, Dir::SW)
{
if (ne == 'M' && sw == 'S') || (ne == 'S' && sw == 'M') {
true
} else {
false
}
} else {
false
};
one && two
} }
struct Board { struct Board {
@ -108,7 +150,7 @@ impl Dir {
mod test { mod test {
use super::*; use super::*;
static I1: &str = "MMMSXXMASM static INPUT: &str = "MMMSXXMASM
MSAMXMSMSA MSAMXMSMSA
AMXSXMAAMM AMXSXMAAMM
MSAMASMSMX MSAMASMSMX
@ -121,9 +163,11 @@ MXMXAXMASX";
#[test] #[test]
fn p1() { fn p1() {
assert_eq!(18, pt1(I1)); assert_eq!(18, pt1(INPUT));
} }
#[test] #[test]
fn p2() {} fn p2() {
assert_eq!(9, pt2(INPUT));
}
} }