day_4: part 1 done in an ungly way
This commit is contained in:
@@ -3,6 +3,7 @@ mod sun_01;
|
||||
mod tue_03;
|
||||
mod types;
|
||||
mod utils;
|
||||
mod wed_04;
|
||||
|
||||
use types::*;
|
||||
|
||||
@@ -33,4 +34,8 @@ pub fn historian_hysteria() {
|
||||
"The result of the conditional sum of multiplications is {}",
|
||||
conditional_multiplication_added_result
|
||||
);
|
||||
|
||||
// Wednesday 04
|
||||
let xmas_appearances = wed_04::ceres_search("./assets/day_4_input");
|
||||
println!("XMAS appears {} times", xmas_appearances);
|
||||
}
|
||||
|
||||
@@ -16,3 +16,6 @@ pub enum ReportDirection {
|
||||
|
||||
// Mull It Over
|
||||
pub type MulNumber = i32;
|
||||
|
||||
// Ceres Search
|
||||
pub type XMASCount = i32;
|
||||
|
||||
@@ -42,6 +42,20 @@ pub fn read_instruction_input(input: &str) -> String {
|
||||
instructions
|
||||
}
|
||||
|
||||
pub fn read_ceres_puzzle_input(input: &str) -> Vec<Vec<char>> {
|
||||
let mut puzzle_input: Vec<Vec<char>> = vec![];
|
||||
|
||||
for line in read_to_string(input).unwrap().lines() {
|
||||
let mut line_vec: Vec<char> = vec![];
|
||||
for character in line.chars().into_iter() {
|
||||
line_vec.push(character);
|
||||
}
|
||||
puzzle_input.push(line_vec);
|
||||
}
|
||||
|
||||
puzzle_input
|
||||
}
|
||||
|
||||
pub fn calc_distance<T>(num_1: T, num_2: T) -> T
|
||||
where
|
||||
T: PartialOrd + Sub<Output = T>,
|
||||
|
||||
143
src/advent_of_code/wed_04.rs
Normal file
143
src/advent_of_code/wed_04.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
use utils::read_ceres_puzzle_input;
|
||||
|
||||
use super::*;
|
||||
|
||||
const SEARCHED_WORD: &str = "XMAS";
|
||||
|
||||
pub fn ceres_search(input: &str) -> XMASCount {
|
||||
let puzzle_matrix = read_ceres_puzzle_input(input);
|
||||
|
||||
let mut match_count: XMASCount = 0;
|
||||
// Loop through the chars
|
||||
for (line_index, line) in puzzle_matrix.iter().enumerate() {
|
||||
for (character_index, character) in line.iter().enumerate() {
|
||||
if *character == SEARCHED_WORD.chars().nth(0).unwrap() {
|
||||
match_count += check_word_matches_from_start(
|
||||
&puzzle_matrix,
|
||||
character_index,
|
||||
line_index,
|
||||
SEARCHED_WORD,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match_count
|
||||
}
|
||||
|
||||
fn check_word_matches_from_start(
|
||||
matrix: &Vec<Vec<char>>,
|
||||
x: usize,
|
||||
y: usize,
|
||||
word: &str,
|
||||
) -> XMASCount {
|
||||
let mut top_matches: XMASCount = 1;
|
||||
let mut bottom_matches: XMASCount = 1;
|
||||
let mut left_matches: XMASCount = 1;
|
||||
let mut right_matches: XMASCount = 1;
|
||||
|
||||
let mut right_top_matches: XMASCount = 1;
|
||||
let mut right_bottom_matches: XMASCount = 1;
|
||||
let mut left_top_matches: XMASCount = 1;
|
||||
let mut left_bottom_matches: XMASCount = 1;
|
||||
|
||||
for (index, current_char) in word.chars().enumerate() {
|
||||
// check to top
|
||||
if y as i32 - index as i32 >= 0 {
|
||||
if matrix[y - index][x] != current_char {
|
||||
top_matches = 0;
|
||||
}
|
||||
} else {
|
||||
top_matches = 0;
|
||||
}
|
||||
// check to bottom
|
||||
if y + index < matrix.len() {
|
||||
if matrix[y + index][x] != current_char {
|
||||
bottom_matches = 0;
|
||||
}
|
||||
} else {
|
||||
bottom_matches = 0;
|
||||
}
|
||||
// check left
|
||||
if x as i32 - index as i32 >= 0 {
|
||||
if matrix[y][x - index] != current_char {
|
||||
left_matches = 0;
|
||||
}
|
||||
} else {
|
||||
left_matches = 0;
|
||||
}
|
||||
// check right
|
||||
if x + index < matrix[0].len() {
|
||||
if matrix[y][x + index] != current_char {
|
||||
right_matches = 0;
|
||||
}
|
||||
} else {
|
||||
right_matches = 0;
|
||||
}
|
||||
|
||||
// check left_top
|
||||
if x as i32 - index as i32 >= 0 && y as i32 - index as i32 >= 0 {
|
||||
if matrix[y - index][x - index] != current_char {
|
||||
left_top_matches = 0;
|
||||
}
|
||||
} else {
|
||||
left_top_matches = 0;
|
||||
}
|
||||
|
||||
//check right_top
|
||||
if x + index < matrix[0].len() && y as i32 - index as i32 >= 0 {
|
||||
if matrix[y - index][x + index] != current_char {
|
||||
right_top_matches = 0;
|
||||
}
|
||||
} else {
|
||||
right_top_matches = 0;
|
||||
}
|
||||
|
||||
//check left_bottom
|
||||
if x as i32 - index as i32 >= 0 && y + index < matrix.len() {
|
||||
if matrix[y + index][x - index] != current_char {
|
||||
left_bottom_matches = 0;
|
||||
}
|
||||
} else {
|
||||
left_bottom_matches = 0;
|
||||
}
|
||||
//check righ_bottom
|
||||
if x + index < matrix[0].len() && y + index < matrix.len() {
|
||||
if matrix[y + index][x + index] != current_char {
|
||||
right_bottom_matches = 0;
|
||||
}
|
||||
} else {
|
||||
right_bottom_matches = 0;
|
||||
}
|
||||
}
|
||||
|
||||
right_matches
|
||||
+ left_matches
|
||||
+ top_matches
|
||||
+ bottom_matches
|
||||
+ right_top_matches
|
||||
+ left_top_matches
|
||||
+ right_bottom_matches
|
||||
+ left_bottom_matches
|
||||
}
|
||||
|
||||
// WRONG: you did not understand the puzzle
|
||||
// The words must be displayed
|
||||
// I'll leave it here 4fun
|
||||
// let mut x_count: XMASCount = 0;
|
||||
// let mut m_count: XMASCount = 0;
|
||||
// let mut a_count: XMASCount = 0;
|
||||
// let mut s_count: XMASCount = 0;
|
||||
// for letter in puzzle_string.chars() {
|
||||
// match letter {
|
||||
// 'X' => x_count += 1,
|
||||
// 'M' => m_count += 1,
|
||||
// 'A' => a_count += 1,
|
||||
// 'S' => s_count += 1,
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
// println!("X:{} M:{} A:{} S:{}", x_count, m_count, a_count, s_count);
|
||||
// if let Some(value) = vec![x_count, m_count, a_count, s_count].iter().min() {
|
||||
// return *value;
|
||||
// }
|
||||
Reference in New Issue
Block a user