Skip to content

Instantly share code, notes, and snippets.

@bozdoz
Last active December 4, 2024 17:08
Show Gist options
  • Save bozdoz/b09270d8dcf1428fbd80ad533148ddff to your computer and use it in GitHub Desktop.
Save bozdoz/b09270d8dcf1428fbd80ad533148ddff to your computer and use it in GitHub Desktop.
AOC 2024 Day 4 in rust
#![allow(unused)]
fn main() {
let example = "
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX";
// (r, c) differences, clockwise
const DIRS: &'static [(isize, isize)] = &[
(-1, 0), // top
(-1, 1), // tr
(0, 1), // right
(1, 1), // br
(1, 0), // bottom
(1, -1), // bl
(0, -1), // left
(-1, -1), // tl
];
const SEARCH: &'static [char] = &['M', 'A', 'S'];
// skip 1 so we can ignore the empty first line
let grid: Vec<_> = example.lines().skip(1).map(|l| {
l.chars().collect::<Vec<_>>()
}).collect();
let height = grid.len() as isize;
let width = grid[0].len() as isize;
let mut count = 0;
for (r, row) in grid.iter().enumerate() {
for (c, &cell) in row.iter().enumerate() {
if cell == 'X' {
let r = r as isize;
let c = c as isize;
// println!("X at {} {}", r, c);
for dir in DIRS {
let mut nextr = r;
let mut nextc = c;
for &ch in SEARCH {
nextr += dir.0;
nextc += dir.1;
if nextr == -1 || nextc == -1 || nextr >= height || nextc >= width {
break;
}
if grid[nextr as usize][nextc as usize] == ch {
// println!("found {} at {},{}!", ch, nextr, nextc);
if ch == 'S' {
// we did it
count += 1;
}
} else {
break;
}
}
}
}
}
}
println!("COUNT 1: {}", count);
let mut count = 0;
const DIAGONALS: &'static [(isize, isize)] = &[
(-1, -1), // tl
(1, 1), // br
(-1, 1), // tr
(1, -1), // bl
];
// The SAM detector
for (r, row) in grid.iter().enumerate() {
'nextcell: for (c, &cell) in row.iter().enumerate() {
if cell == 'A' {
let r = r as isize;
let c = c as isize;
// println!("A at {}, {}", r, c);
for dirs in DIAGONALS.chunks(2) {
let mut acceptable = vec!['S', 'M'];
for dir in dirs {
let nextr = r + dir.0;
let nextc = c + dir.1;
if nextr == -1 || nextc == -1 || nextr >= height || nextc >= width {
// println!("- went overboard at {},{}", nextr, nextc);
continue 'nextcell;
}
let ch = grid[nextr as usize][nextc as usize];
if acceptable.contains(&ch) {
// println!("- yay {} at {},{}", ch, nextr, nextc);
// remove from acceptable and search next diagonal
acceptable.retain(|&x| x != ch);
} else {
// println!("- didn't find anything: {}", ch);
continue 'nextcell;
}
}
}
// println!("--- found at: {}, {}", r, c);
count += 1;
}
}
}
println!("COUNT 2: {}", count);
}
@bozdoz
Copy link
Author

bozdoz commented Dec 4, 2024

might be easier as:

let ch = &grid[nextr as usize][nextc as usize];
                        
                        if acceptable.contains(ch) {
                            // println!("- yay {} at {},{}", ch, nextr, nextc);
                            // remove from acceptable and search next diagonal
                            acceptable.retain(|x| x != ch);
                        } else {
                            // println!("- didn't find anything: {}", ch);
                            continue 'nextcell;
                        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment