diff --git a/day04/src/main.rs b/day04/src/main.rs index 9fb009b..5bd332d 100644 --- a/day04/src/main.rs +++ b/day04/src/main.rs @@ -1,3 +1,5 @@ +#![feature(let_chains)] + static MAS: [char; 3] = ['M', 'A', 'S']; static DIRS: [Dir; 8] = [ Dir::N, @@ -50,8 +52,48 @@ fn find_xmas(board: &Board, loc: (usize, usize), dir: Dir) -> bool { true } -fn pt2(_input: &str) -> u32 { - 0 +fn pt2(input: &str) -> u32 { + 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 { @@ -108,7 +150,7 @@ impl Dir { mod test { use super::*; - static I1: &str = "MMMSXXMASM + static INPUT: &str = "MMMSXXMASM MSAMXMSMSA AMXSXMAAMM MSAMASMSMX @@ -121,9 +163,11 @@ MXMXAXMASX"; #[test] fn p1() { - assert_eq!(18, pt1(I1)); + assert_eq!(18, pt1(INPUT)); } #[test] - fn p2() {} + fn p2() { + assert_eq!(9, pt2(INPUT)); + } }