2024-12-04 13:22:39 +01:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
use std::ops::RangeInclusive;
|
|
|
|
|
|
|
|
#[allow(unused_imports)]
|
2024-12-06 16:56:47 +01:00
|
|
|
use advent_of_code::{include_data, include_example};
|
2024-12-04 13:22:39 +01:00
|
|
|
|
|
|
|
include_data!(DATA 2024 04);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Direction {
|
|
|
|
Forward(usize, usize),
|
|
|
|
Backward(usize, usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Direction {
|
|
|
|
fn iter(&self) -> RangeInclusive<usize> {
|
|
|
|
let (from, to) = match self {
|
|
|
|
Direction::Forward(from, to) => (from, to),
|
|
|
|
Direction::Backward(to, from) => (from, to),
|
|
|
|
};
|
|
|
|
*from..=*to
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let table: Vec<Vec<char>> = 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);
|
|
|
|
}
|