Day 4 done
This commit is contained in:
parent
c529a96f30
commit
337508f4e8
148
2024/rust/src/day4.rs
Normal file
148
2024/rust/src/day4.rs
Normal file
|
@ -0,0 +1,148 @@
|
|||
mod prelude;
|
||||
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}");
|
||||
let height = grid.len();
|
||||
let width = grid[0].len();
|
||||
let mut counter = 0;
|
||||
|
||||
const XMAS: (u8, u8, u8, u8) = (b'X', b'M', b'A', b'S');
|
||||
const SAMX: (u8, u8, u8, u8) = (b'S', b'A', b'M', b'X');
|
||||
|
||||
for (y, line) in grid.iter().enumerate() {
|
||||
for (x, letter) in line.iter().enumerate() {
|
||||
// let near_left_edge = x < 3;
|
||||
let near_top_edge = y < 3;
|
||||
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 = (
|
||||
*letter,
|
||||
grid[y - 1][x + 1],
|
||||
grid[y - 2][x + 2],
|
||||
grid[y - 3][x + 3],
|
||||
);
|
||||
if ray == XMAS || ray == SAMX {
|
||||
counter += 1;
|
||||
println!("north-west @ {x} {y} {counter}");
|
||||
}
|
||||
}
|
||||
|
||||
if !near_right_edge {
|
||||
// west
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
if !near_right_edge && !near_bottom_edge {
|
||||
// south-west
|
||||
let ray = (
|
||||
*letter,
|
||||
grid[y + 1][x + 1],
|
||||
grid[y + 2][x + 2],
|
||||
grid[y + 3][x + 3],
|
||||
);
|
||||
if ray == XMAS || ray == SAMX {
|
||||
counter += 1;
|
||||
println!("south-west @ {x} {y} {counter}");
|
||||
}
|
||||
}
|
||||
|
||||
if !near_bottom_edge {
|
||||
// south
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
let mut counter = 0;
|
||||
for y in 1..height - 1 {
|
||||
for x in 1..width - 1 {
|
||||
println!("{x} {y} {}", grid[y][x]);
|
||||
if grid[y][x] != b'A' {
|
||||
continue;
|
||||
}
|
||||
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'))
|
||||
{
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
counter
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(
|
||||
count_xmas(
|
||||
r#"MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX"#
|
||||
),
|
||||
18
|
||||
);
|
||||
assert_eq!(
|
||||
count_mas_x(
|
||||
r#"MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX"#
|
||||
),
|
||||
9
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue