From 5214edf75d2f3f0804a1758e39e85d4c95b94cbf Mon Sep 17 00:00:00 2001 From: Devon Sawatsky Date: Wed, 19 Nov 2025 03:27:07 +0000 Subject: [PATCH] day4 part2 --- src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6ad2c48..e5dbec1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -178,6 +178,58 @@ pub mod day4 { count.to_string() } + pub fn day4_part2(input: &str) -> String{ + let input = string_to_array(input); + let mut count = 0; + for row in 1..input.len()-1 { + for col in 1..input.len()-1 { + if is_x_mas(&input, row, col) { + count += 1; + } + } + } + count.to_string() + } + + /* S&M combinations for X-MAS + MS + MS + + SM + SM + + MM + SS + + SS + MM */ + //precondition: row and col are one away from the edge + fn is_x_mas(input: &Vec>, row: usize, col: usize) -> bool { + if input[row][col] != 'A' { + return false; + } + if input[row-1][col-1] == 'M' { + if input[row-1][col+1] == 'M' { + input[row+1][col+1] == 'S' && input[row+1][col-1] == 'S' + } else if input[row+1][col-1] == 'M' { + input[row+1][col+1] == 'S' && input[row-1][col+1] == 'S' + } else { + false + } + } else if input[row-1][col-1] == 'S' { + if input[row-1][col+1] == 'S' { + input[row+1][col+1] == 'M' && input[row+1][col-1] == 'M' + } else if input[row+1][col-1] == 'S' { + input[row+1][col+1] == 'M' && input[row-1][col+1] == 'M' + } else { + false + } + } else { + false + } + + } + //squares only fn rotate_string_45degrees_counterclockwise(input: &str) -> String { let array_input = string_to_array(input); @@ -305,13 +357,13 @@ mod tests { assert_eq!(result, "2493"); } - // #[test] - // fn day4_part2_test() { - // let test_result = day4::day4_part2(include_str!("../input/day4_part2.test.txt")); - // assert_eq!(test_result, "48"); - // let result = day4::day4_part2(include_str!("../input/day4.txt")); - // assert_eq!(result, "62098619"); - // } + #[test] + fn day4_part2_test() { + let test_result = day4::day4_part2(include_str!("../input/day4.test.txt")); + assert_eq!(test_result, "9"); + let result = day4::day4_part2(include_str!("../input/day4.txt")); + assert_eq!(result, "1890"); + } #[test] fn day3_part1_test() { -- 2.51.2