diff --git a/src/main.rs b/src/main.rs index b6cfd7e..450f27f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,6 +106,7 @@ mod day3 { use regex::Regex; + #[allow(dead_code)] pub fn task1() { let rx = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap(); let mut result: i64 = 0; @@ -116,6 +117,7 @@ mod day3 { println!("{result}"); } + #[allow(dead_code)] pub fn task2() { let rx = Regex::new(r"(mul)\((\d{1,3}),(\d{1,3})\)|do()()()\(\)|do(n't)()()\(\)").unwrap(); let mut enabled = true; @@ -136,11 +138,78 @@ mod day3 { } } +mod day4 { + use itertools::Itertools; + + #[allow(dead_code)] + pub fn task1() { + fn check(vals: &[u8]) -> usize { + (vals == b"XMAS" || vals == b"SAMX").into() + } + + let data = std::fs::read_to_string("input.txt").unwrap(); + let mut lines = Vec::new(); + let mut count = 0; + for line in data.lines() { + count += line.matches("XMAS").count(); + count += line.matches("SAMX").count(); + lines.push(line.as_bytes()); + } + for (i, &line) in lines[0..lines.len() - 3].iter().enumerate() { + for j in 0..line.len() { + count += check(&[ + lines[i][j], + lines[i + 1][j], + lines[i + 2][j], + lines[i + 3][j], + ]); + if j + 3 < line.len() { + count += check(&[ + lines[i][j], + lines[i + 1][j + 1], + lines[i + 2][j + 2], + lines[i + 3][j + 3], + ]); + } + if j > 2 { + count += check(&[ + lines[i][j], + lines[i + 1][j - 1], + lines[i + 2][j - 2], + lines[i + 3][j - 3], + ]); + } + } + } + println!("{count}"); + } + + #[allow(dead_code)] + pub fn task2() { + fn check(vals: &[u8]) -> usize { + (vals == b"MAS" || vals == b"SAM").into() + } + + let data = std::fs::read_to_string("input.txt").unwrap(); + let lines = data.lines().map(str::as_bytes).collect_vec(); + let mut count: usize = 0; + for i in 0..lines[..lines.len() - 2].len() { + for j in 0..lines[i].len() - 2 { + count += check(&[lines[i][j], lines[i + 1][j + 1], lines[i + 2][j + 2]]) + * check(&[lines[i][j + 2], lines[i + 1][j + 1], lines[i + 2][j]]); + } + } + println!("{count}"); + } +} + fn main() { // day1::task1(); // day1::task1(); // day2::task1(); // day2::task2(); - day3::task1(); - day3::task2(); + // day3::task1(); + // day3::task2(); + day4::task1(); + day4::task2(); }