day 4: part 2: rust

This commit is contained in:
Samuel Ortion 2024-12-04 12:50:46 +01:00
parent f543fd930d
commit 9fe58d5490
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,6 @@
[package]
name = "part1"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,38 @@
fn count_xmas(hay: Vec<Vec<char>>) -> u64 {
let mut count: u64 = 0;
let target: Vec<char> = vec!['M', 'A', 'S'];
for center_i in 1..(hay.len() - 1) {
for center_j in 1..(hay.len() - 1) {
let mut diag1: Vec<char> = Vec::new();
let mut diag2: Vec<char> = 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<char>> = Vec::new();
for line in std::io::stdin().lines() {
let line = line.unwrap();
let mut row: Vec<char> = Vec::new();
for chr in line.chars() {
row.push(chr);
}
hay.push(row);
}
let c = count_xmas(hay);
println!("{}", c);
Ok(())
}