Skip to content

Commit

Permalink
Solve Day 4 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
destinationunknown committed Dec 9, 2024
1 parent b9db034 commit 104f93f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
10 changes: 10 additions & 0 deletions data/examples/04-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.M.S......
..A..MSMS.
.M.S.MAA..
..A.ASMSM.
.M.S.M....
..........
S.S.S.S.S.
.A.A.A.A..
M.M.M.M.M.
..........
41 changes: 35 additions & 6 deletions src/bin/04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ pub fn part_one(input: &str) -> Option<u32> {
for i in 0..4 {
let (r, c) = (row + (i * dr), col + (i * dc));
if r >= 0 && c >= 0 && r < m && c < n {
if xmas.chars().nth(i as usize).unwrap()
!= matrix[r as usize][c as usize]
{
if xmas.chars().nth(i as usize).unwrap() != matrix[r as usize][c as usize] {
continue 'outer;
}
} else {
Expand All @@ -51,7 +49,36 @@ pub fn part_one(input: &str) -> Option<u32> {
}

pub fn part_two(input: &str) -> Option<u32> {
None
let matrix: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect();
let m = matrix.len() as i32;
let n = matrix[0].len() as i32;

let valid = |c: char| -> bool {
return c == 'M' || c == 'S';
};

let check_pos = |row: usize, col: usize| -> bool {
let center = matrix[row][col];
let a = matrix[row - 1][col - 1];
let b = matrix[row + 1][col + 1];
let c = matrix[row + 1][col - 1];
let d = matrix[row - 1][col + 1];
let primary = valid(a) && valid(b) && (a != b);
let secondary = valid(c) && valid(d) && (c != d);
return center == 'A' && primary && secondary;
};

let mut count = 0;

for i in 1..m - 1 {
for j in 1..n - 1 {
if check_pos(i as usize, j as usize) {
count += 1;
}
}
}

Some(count as u32)
}

#[cfg(test)]
Expand All @@ -66,7 +93,9 @@ mod tests {

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(9));
}
}

0 comments on commit 104f93f

Please sign in to comment.