mirror of
https://github.com/LeMoonStar/AoC24.git
synced 2025-07-07 12:35:32 +02:00
Compare commits
3 commits
bb4c059b25
...
d25a9aad57
Author | SHA1 | Date | |
---|---|---|---|
d25a9aad57 | |||
2d89799e44 | |||
134512e4ba |
2 changed files with 125 additions and 73 deletions
|
@ -3,8 +3,8 @@
|
|||
[](https://adventofcode.com/2024/about)
|
||||
[](https://en.wikipedia.org/wiki/Rust_(programming_language))
|
||||
[](https://mit-license.org/)
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
> ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet.
|
||||
|
||||
|
|
194
src/days/d04.rs
194
src/days/d04.rs
|
@ -1,21 +1,7 @@
|
|||
use crate::dprintln;
|
||||
|
||||
use super::{Answer, Day, DayImpl};
|
||||
|
||||
const CURRENT_DAY: u8 = 4;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Direction {
|
||||
East,
|
||||
West,
|
||||
North,
|
||||
South,
|
||||
NorthEast,
|
||||
NorthWest,
|
||||
SouthEast,
|
||||
SouthWest,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LetterWall(Vec<Vec<char>>);
|
||||
|
||||
|
@ -30,77 +16,144 @@ impl LetterWall {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_upper_left_of_xmas(&self, x: usize, y: usize) -> Option<Direction> {
|
||||
#[rustfmt::skip]
|
||||
#[rustfmt::skip]
|
||||
fn get_xmas_direction_count(&self, x: usize, y: usize) -> u64 {
|
||||
let mut directions = 0;
|
||||
|
||||
let map = (
|
||||
self.at(x, y), self.at(x+1, y), self.at(x+2, y), self.at(x+3, y),
|
||||
self.at(x, y+1), self.at(x+1, y+1), self.at(x+2, y+1), self.at(x+3, y+1),
|
||||
self.at(x, y+2), self.at(x+1, y+2), self.at(x+2, y+2), self.at(x+3, y+2),
|
||||
self.at(x, y+3), self.at(x+1, y+3), self.at(x+2, y+3), self.at(x+3, y+3)
|
||||
);
|
||||
|
||||
if let (
|
||||
Some(&'X'), Some(&'M'), Some(&'A'), Some(&'S'),
|
||||
_, _, _, _,
|
||||
_, _, _, _,
|
||||
_, _, _, _
|
||||
) = map {
|
||||
directions += 1;
|
||||
}
|
||||
|
||||
if let (
|
||||
Some('S'), Some('A'), Some('M'), Some('X'),
|
||||
_, _, _, _,
|
||||
_, _, _, _,
|
||||
_, _, _, _
|
||||
) = map {
|
||||
directions += 1;
|
||||
}
|
||||
|
||||
if let (
|
||||
Some('X'), _, _, _,
|
||||
Some('M'), _, _, _,
|
||||
Some('A'), _, _, _,
|
||||
Some('S'), _, _, _
|
||||
) = map {
|
||||
directions += 1;
|
||||
}
|
||||
|
||||
if let (
|
||||
Some('S'), _, _, _,
|
||||
Some('A'), _, _, _,
|
||||
Some('M'), _, _, _,
|
||||
Some('X'), _, _, _
|
||||
) = map {
|
||||
directions += 1;
|
||||
}
|
||||
|
||||
if let (
|
||||
Some('X'), _, _, _,
|
||||
_, Some('M'), _, _,
|
||||
_, _, Some('A'), _,
|
||||
_, _, _, Some('S')
|
||||
) = map {
|
||||
directions += 1;
|
||||
}
|
||||
|
||||
if let (
|
||||
Some('S'), _, _, _,
|
||||
_, Some('A'), _, _,
|
||||
_, _, Some('M'), _,
|
||||
_, _, _, Some('X')
|
||||
) = map {
|
||||
directions += 1;
|
||||
}
|
||||
|
||||
if let (
|
||||
_, _, _, Some('X'),
|
||||
_, _, Some('M'), _,
|
||||
_, Some('A'), _, _,
|
||||
Some('S'), _, _, _
|
||||
) = map {
|
||||
directions += 1;
|
||||
}
|
||||
|
||||
if let (
|
||||
_, _, _, Some('S'),
|
||||
_, _, Some('A'), _,
|
||||
_, Some('M'), _, _,
|
||||
Some('X'), _, _, _
|
||||
) = map {
|
||||
directions += 1;
|
||||
}
|
||||
|
||||
directions
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
fn is_cross_mas(&self, x: usize, y: usize) -> bool {
|
||||
match(
|
||||
self.at( x, y), self.at( x+1, y), self.at( x+2, y), self.at( x+3, y),
|
||||
self.at( x, y+1), self.at( x+1, y+1), self.at( x+2, y+1), self.at( x+3, y+1),
|
||||
self.at( x, y+2), self.at( x+1, y+2), self.at( x+2, y+2), self.at( x+3, y+2),
|
||||
self.at( x, y+3), self.at( x+1, y+3), self.at( x+2, y+3), self.at( x+3, y+3)
|
||||
self.at( x, y), self.at( x+1, y), self.at( x+2, y),
|
||||
self.at( x, y+1), self.at( x+1, y+1), self.at( x+2, y+1),
|
||||
self.at( x, y+2), self.at( x+1, y+2), self.at( x+2, y+2)
|
||||
) {
|
||||
(
|
||||
Some('X'), Some('M'), Some('A'), Some('S'),
|
||||
_, _, _, _,
|
||||
_, _, _, _,
|
||||
_, _, _, _
|
||||
) => Some(Direction::East),
|
||||
Some('M'), _, Some('S'),
|
||||
_, Some('A'), _,
|
||||
Some('M'), _, Some('S'),
|
||||
) => true,
|
||||
(
|
||||
Some('S'), Some('A'), Some('M'), Some('X'),
|
||||
_, _, _, _,
|
||||
_, _, _, _,
|
||||
_, _, _, _
|
||||
) => Some(Direction::West),
|
||||
Some('S'), _, Some('S'),
|
||||
_, Some('A'), _,
|
||||
Some('M'), _, Some('M'),
|
||||
) => true,
|
||||
(
|
||||
Some('X'), _, _, _,
|
||||
Some('M'), _, _, _,
|
||||
Some('A'), _, _, _,
|
||||
Some('S'), _, _, _
|
||||
) => Some(Direction::South),
|
||||
Some('M'), _, Some('M'),
|
||||
_, Some('A'), _,
|
||||
Some('S'), _, Some('S'),
|
||||
) => true,
|
||||
(
|
||||
Some('S'), _, _, _,
|
||||
Some('A'), _, _, _,
|
||||
Some('M'), _, _, _,
|
||||
Some('X'), _, _, _
|
||||
) => Some(Direction::North),
|
||||
(
|
||||
Some('X'), _, _, _,
|
||||
_, Some('M'), _, _,
|
||||
_, _, Some('A'), _,
|
||||
_, _, _, Some('S')
|
||||
) => Some(Direction::SouthEast),
|
||||
(
|
||||
Some('S'), _, _, _,
|
||||
_, Some('A'), _, _,
|
||||
_, _, Some('M'), _,
|
||||
_, _, _, Some('X')
|
||||
) => Some(Direction::NorthWest),
|
||||
(
|
||||
_, _, _, Some('X'),
|
||||
_, _, Some('M'), _,
|
||||
_, Some('A'), _, _,
|
||||
Some('S'), _, _, _
|
||||
) => Some(Direction::SouthWest),
|
||||
(
|
||||
_, _, _, Some('S'),
|
||||
_, _, Some('A'), _,
|
||||
_, Some('M'), _, _,
|
||||
Some('X'), _, _, _
|
||||
) => Some(Direction::NorthEast),
|
||||
_ => None
|
||||
Some('S'), _, Some('M'),
|
||||
_, Some('A'), _,
|
||||
Some('S'), _, Some('M'),
|
||||
) => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn count_xmas(&self) -> u64 {
|
||||
let mut count = 0;
|
||||
for y in 0..self.0.len() {
|
||||
for x in 0..self.0[0].len() {
|
||||
if let Some(_dir) = self.is_upper_left_of_xmas(x, y) {
|
||||
dprintln!("FOUND AT {} {} '{}' - DIR: {:?}", x, y, self.0[y][x], _dir);
|
||||
count += self.get_xmas_direction_count(x, y);
|
||||
}
|
||||
}
|
||||
return count as u64;
|
||||
}
|
||||
|
||||
pub fn count_cross_mas(&self) -> u64 {
|
||||
let mut count = 0;
|
||||
for y in 0..self.0.len() {
|
||||
for x in 0..self.0[0].len() {
|
||||
if self.is_cross_mas(x, y) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
return count as u64;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,7 +171,7 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
|
|||
}
|
||||
|
||||
fn expected_results() -> (Answer, Answer) {
|
||||
(Answer::Number(18), Answer::Number(0))
|
||||
(Answer::Number(18), Answer::Number(9))
|
||||
}
|
||||
|
||||
fn init(input: &str) -> (Self, Data) {
|
||||
|
@ -126,11 +179,10 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
|
|||
}
|
||||
|
||||
fn one(&self, data: &mut Data) -> Answer {
|
||||
// NOTE TO SELF: Actual result above 2334
|
||||
Answer::Number(data.count_xmas())
|
||||
}
|
||||
|
||||
fn two(&self, data: &mut Data) -> Answer {
|
||||
Answer::Number(0)
|
||||
Answer::Number(data.count_cross_mas())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue