#[allow(unused_variables)] use std::ops::RangeInclusive; #[allow(unused_imports)] use advent_of_code::{include_data, include_example}; include_data!(DATA 2024 04); #[derive(Debug, Clone)] enum Direction { Forward(usize, usize), Backward(usize, usize), } impl Direction { fn iter(&self) -> RangeInclusive { let (from, to) = match self { Direction::Forward(from, to) => (from, to), Direction::Backward(to, from) => (from, to), }; *from..=*to } } fn main() { let table: Vec> = DATA .split('\n') .map(|line| line.chars().collect()) .collect(); let length = table[0].len(); let mut xmas; let mut amount = 0; let directions = [ Direction::Forward(0, length - 1), Direction::Backward(0, length - 1), ]; for r in directions.clone() { for c in directions.clone() { println!("Horizontal: {:?}, vertical: {:?}", r.clone(), c.clone()); for row in r.iter() { xmas = 0; for column in c.iter() { println!( "Row {}, Column {}, dir {:?} {:?}", row, column, r.clone(), c.clone() ); match (xmas, table[row][column]) { (_x, 'X') => { xmas = 1; //println!("Found X, previous was {}", _x) } (1, 'M') => { xmas = 2; //println!("Found XM") } (2, 'A') => { xmas = 3; //println!("Found XMA") } (3, 'S') => { xmas = 0; amount += 1; //println!("found XMAS in {}:{}", row, column); } (_x, _ch) => { xmas = 0; //println!("Reset, was {}({})", _x, _ch) } } } } } } println!("There are {} XMAS in text", amount); }