day_5: part 1 done
This commit is contained in:
@@ -7,7 +7,7 @@ https://adventofcode.com/2024
|
|||||||
| 02 | Red-Nosed Reports | :pushpin: | :pushpin: | Bidirectional arrays |
|
| 02 | Red-Nosed Reports | :pushpin: | :pushpin: | Bidirectional arrays |
|
||||||
| 03 | Mull It Over | :pushpin: | :pushpin: | Regex |
|
| 03 | Mull It Over | :pushpin: | :pushpin: | Regex |
|
||||||
| 04 | Ceres Search | :pushpin: | :pushpin: | Matrix multidirectional search |
|
| 04 | Ceres Search | :pushpin: | :pushpin: | Matrix multidirectional search |
|
||||||
| 05 | | | | |
|
| 05 | Print Queue | :pushpin: | | Queue order |
|
||||||
| 06 | | | | |
|
| 06 | | | | |
|
||||||
| 07 | | | | |
|
| 07 | | | | |
|
||||||
| 08 | | | | |
|
| 08 | | | | |
|
||||||
|
|||||||
1377
assets/day_5_rules_queue_input
Normal file
1377
assets/day_5_rules_queue_input
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
|||||||
mod mon_02;
|
mod mon_02;
|
||||||
mod sun_01;
|
mod sun_01;
|
||||||
|
mod thu_05;
|
||||||
mod tue_03;
|
mod tue_03;
|
||||||
mod types;
|
mod types;
|
||||||
mod utils;
|
mod utils;
|
||||||
@@ -39,4 +40,8 @@ pub fn historian_hysteria() {
|
|||||||
let (xmas_appearances, x_mas_appearances) = wed_04::ceres_search("./assets/day_4_input");
|
let (xmas_appearances, x_mas_appearances) = wed_04::ceres_search("./assets/day_4_input");
|
||||||
println!("XMAS appears {} times", xmas_appearances);
|
println!("XMAS appears {} times", xmas_appearances);
|
||||||
println!("X-MAS appears {} times", x_mas_appearances);
|
println!("X-MAS appears {} times", x_mas_appearances);
|
||||||
|
|
||||||
|
// Thursday 05
|
||||||
|
let queue_mid_sum = thu_05::mid_queue_sum("./assets/day_5_rules_queue_input");
|
||||||
|
println!("The update mid-queue-sum is {}", queue_mid_sum);
|
||||||
}
|
}
|
||||||
|
|||||||
48
src/advent_of_code/thu_05.rs
Normal file
48
src/advent_of_code/thu_05.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use utils::read_rules_and_queue;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn mid_queue_sum(input: &str) -> u32 {
|
||||||
|
let (rules, queues) = read_rules_and_queue(input);
|
||||||
|
|
||||||
|
let mut valid_queue_list: Vec<Queue> = vec![];
|
||||||
|
|
||||||
|
let mut sum_of_mids: u32 = 0;
|
||||||
|
|
||||||
|
for queue in queues {
|
||||||
|
if is_queue_valid(&queue, &rules) {
|
||||||
|
valid_queue_list.push(queue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for queue in valid_queue_list {
|
||||||
|
sum_of_mids += queue[queue.len() / 2];
|
||||||
|
}
|
||||||
|
|
||||||
|
sum_of_mids
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_queue_valid(queue: &Queue, rules: &Vec<Rule>) -> bool {
|
||||||
|
for rule in rules {
|
||||||
|
if let Ok(first_index) = get_index_of(queue, &rule.0) {
|
||||||
|
if let Ok(second_index) = get_index_of(queue, &rule.1) {
|
||||||
|
if first_index > second_index {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_index_of<T>(vec: &Vec<T>, element: &T) -> Result<usize, ()>
|
||||||
|
where
|
||||||
|
T: PartialEq,
|
||||||
|
{
|
||||||
|
for (index, e) in vec.iter().enumerate() {
|
||||||
|
if e == element {
|
||||||
|
return Ok(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
@@ -19,3 +19,7 @@ pub type MulNumber = i32;
|
|||||||
|
|
||||||
// Ceres Search
|
// Ceres Search
|
||||||
pub type XMASCount = i32;
|
pub type XMASCount = i32;
|
||||||
|
|
||||||
|
// Print Queue
|
||||||
|
pub struct Rule(pub u32, pub u32);
|
||||||
|
pub type Queue = Vec<u32>;
|
||||||
|
|||||||
@@ -56,6 +56,36 @@ pub fn read_ceres_puzzle_input(input: &str) -> Vec<Vec<char>> {
|
|||||||
puzzle_input
|
puzzle_input
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_rules_and_queue(input: &str) -> (Vec<Rule>, Vec<Queue>) {
|
||||||
|
let mut rules: Vec<Rule> = vec![];
|
||||||
|
let mut queues: Vec<Queue> = vec![];
|
||||||
|
|
||||||
|
let input_string = read_to_string(input).unwrap();
|
||||||
|
if let Some((rule_set, queue_list)) = input_string.split_once("\n\n") {
|
||||||
|
// process rules
|
||||||
|
for rule in rule_set.split("\n") {
|
||||||
|
if let Some((first, second)) = rule.split_once("|") {
|
||||||
|
rules.push(Rule(
|
||||||
|
first.parse::<u32>().unwrap(),
|
||||||
|
second.parse::<u32>().unwrap(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// process queues
|
||||||
|
for queue in queue_list.split("\n") {
|
||||||
|
if queue.len() > 0 {
|
||||||
|
let mut queue_vec: Queue = vec![];
|
||||||
|
for element in queue.split(",") {
|
||||||
|
queue_vec.push(element.parse::<u32>().unwrap());
|
||||||
|
}
|
||||||
|
queues.push(queue_vec);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(rules, queues)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn calc_distance<T>(num_1: T, num_2: T) -> T
|
pub fn calc_distance<T>(num_1: T, num_2: T) -> T
|
||||||
where
|
where
|
||||||
T: PartialOrd + Sub<Output = T>,
|
T: PartialOrd + Sub<Output = T>,
|
||||||
|
|||||||
@@ -156,7 +156,6 @@ fn check_x_word_matches_from_center(matrix: &Vec<Vec<char>>, x: usize, y: usize)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_string_mas(word: String) -> bool {
|
fn is_string_mas(word: String) -> bool {
|
||||||
println!("Word: {}", word);
|
|
||||||
word == "MAS" || word == "SAM"
|
word == "MAS" || word == "SAM"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user