Skip to content

Commit

Permalink
Solve Day 4 Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
destinationunknown committed Dec 9, 2024
1 parent 1e14dbb commit 70c4969
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions data/examples/04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
72 changes: 72 additions & 0 deletions src/bin/04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
advent_of_code::solution!(4);

pub fn part_one(input: &str) -> Option<u32> {
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 check_pos = |row: i32, col: i32| -> i32 {
let xmas = "XMAS";
let dirs = [
(0, 1),
(0, -1),
(-1, 0),
(1, 0),
(-1, -1),
(-1, 1),
(1, -1),
(1, 1),
];

let mut result = 0;

'outer: for (dr, dc) in dirs {
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]
{
continue 'outer;
}
} else {
continue 'outer;
}
}
result += 1;
}

return result;
};

let mut count = 0;

for i in 0..m {
for j in 0..n {
count += check_pos(i, j);
}
}

Some(count as u32)
}

pub fn part_two(input: &str) -> Option<u32> {
None
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(18));
}

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

0 comments on commit 70c4969

Please sign in to comment.