This commit is contained in:
Daniel Flanagan 2024-12-04 10:07:31 -06:00
parent 337508f4e8
commit 80315133c4

View file

@ -3,16 +3,15 @@ pub use crate::prelude::*;
fn main() {
let input = day_input(4);
show_answers(count_xmas(&input), count_mas_x(&input));
}
fn count_xmas(input: &str) -> usize {
let grid: Vec<Vec<u8>> = input
.trim()
.lines()
.map(|l| l.trim().bytes().collect())
.collect();
println!("{grid:?}\n{input}");
show_answers(count_xmas(&grid), count_mas_x(&grid));
}
fn count_xmas(grid: &Vec<Vec<u8>>) -> usize {
let height = grid.len();
let width = grid[0].len();
let mut counter = 0;
@ -27,8 +26,6 @@ fn count_xmas(input: &str) -> usize {
let near_right_edge = x >= width - 3;
let near_bottom_edge = y >= height - 3;
// println!("{x} {y} {width} {height} {near_right_edge} {near_top_edge}");
if !near_right_edge && !near_top_edge {
// north-west
let ray = (
@ -39,7 +36,6 @@ fn count_xmas(input: &str) -> usize {
);
if ray == XMAS || ray == SAMX {
counter += 1;
println!("north-west @ {x} {y} {counter}");
}
}
@ -48,7 +44,6 @@ fn count_xmas(input: &str) -> usize {
let ray = (*letter, grid[y][x + 1], grid[y][x + 2], grid[y][x + 3]);
if ray == XMAS || ray == SAMX {
counter += 1;
println!("west @ {x} {y} {counter}");
}
}
@ -62,7 +57,6 @@ fn count_xmas(input: &str) -> usize {
);
if ray == XMAS || ray == SAMX {
counter += 1;
println!("south-west @ {x} {y} {counter}");
}
}
@ -71,7 +65,6 @@ fn count_xmas(input: &str) -> usize {
let ray = (*letter, grid[y + 1][x], grid[y + 2][x], grid[y + 3][x]);
if ray == XMAS || ray == SAMX {
counter += 1;
println!("south @ {x} {y} {counter}");
}
}
}
@ -79,25 +72,21 @@ fn count_xmas(input: &str) -> usize {
counter
}
fn count_mas_x(input: &str) -> usize {
let grid: Vec<Vec<u8>> = input
.trim()
.lines()
.map(|l| l.trim().bytes().collect())
.collect();
println!("{grid:?}\n{input}");
let height = grid.len();
let width = grid[0].len();
fn count_mas_x(grid: &Vec<Vec<u8>>) -> usize {
let mut counter = 0;
let (width, height) = (grid[0].len(), grid.len());
for y in 1..height - 1 {
for x in 1..width - 1 {
println!("{x} {y} {}", grid[y][x]);
// 'A' will always be in the middle, so we can easily just look for
// middle 'A' occurences and then check for opposing 'S' and 'M'
// characters
if grid[y][x] != b'A' {
continue;
}
// check opposing cross-characters
let ray1 = (grid[y - 1][x - 1], grid[y + 1][x + 1]);
let ray2 = (grid[y + 1][x - 1], grid[y - 1][x + 1]);
println!("{x} {y} {ray1:?} {ray2:?} (S: {:?}, M: {:?})", b'S', b'M');
if (ray1 == (b'M', b'S') || ray1 == (b'S', b'M'))
&& (ray2 == (b'M', b'S') || ray2 == (b'S', b'M'))
{
@ -114,9 +103,7 @@ mod tests {
#[test]
fn test() {
assert_eq!(
count_xmas(
r#"MMMSXXMASM
let input = r#"MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
@ -125,24 +112,15 @@ XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX"#
),
18
);
assert_eq!(
count_mas_x(
r#"MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX"#
),
9
);
MXMXAXMASX"#;
let grid: Vec<Vec<u8>> = input
.trim()
.lines()
.map(|l| l.trim().bytes().collect())
.collect();
assert_eq!(count_xmas(&grid), 18);
assert_eq!(count_mas_x(&grid), 9);
}
}