diff --git a/2024/days/04/part2/Cargo.toml b/2024/days/04/part2/Cargo.toml new file mode 100644 index 0000000..d85b608 --- /dev/null +++ b/2024/days/04/part2/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "part1" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2024/days/04/part2/src/main.rs b/2024/days/04/part2/src/main.rs new file mode 100644 index 0000000..fba2bb7 --- /dev/null +++ b/2024/days/04/part2/src/main.rs @@ -0,0 +1,38 @@ +fn count_xmas(hay: Vec>) -> u64 { + let mut count: u64 = 0; + let target: Vec = vec!['M', 'A', 'S']; + for center_i in 1..(hay.len() - 1) { + for center_j in 1..(hay.len() - 1) { + let mut diag1: Vec = Vec::new(); + let mut diag2: Vec = Vec::new(); + for shift in 0..3 { + let shift: isize = shift as isize - 1; + diag1.push(hay[(center_i as isize + shift) as usize][(center_j as isize - shift) as usize]); + diag2.push(hay[(center_i as isize + shift) as usize][(center_j as isize + shift) as usize]); + } + let mut diag1_rev = diag1.clone(); + let mut diag2_rev = diag2.clone(); + diag1_rev.reverse(); + diag2_rev.reverse(); + if (diag1_rev == target || diag1 == target) && (diag2_rev == target || diag2 == target) { + count += 1; + } + } + } + count +} + +fn main() -> std::io::Result<()> { + let mut hay: Vec> = Vec::new(); + for line in std::io::stdin().lines() { + let line = line.unwrap(); + let mut row: Vec = Vec::new(); + for chr in line.chars() { + row.push(chr); + } + hay.push(row); + } + let c = count_xmas(hay); + println!("{}", c); + Ok(()) +} \ No newline at end of file