Reorganization of thigs, solution for day 2 part 1 2024.
This commit is contained in:
parent
9377cac695
commit
ef9f709c27
@ -67,20 +67,12 @@ pub fn run_day_1() -> Solution {
|
|||||||
/*
|
/*
|
||||||
* SOLUTION
|
* SOLUTION
|
||||||
*/
|
*/
|
||||||
let mut answers = String::new();
|
let part_1 = current_position.x + current_position.y.abs();
|
||||||
let answer = current_position.x + current_position.y.abs();
|
let part_2 = "116".to_string();
|
||||||
let part_1 = "Part 1: ".to_string();
|
|
||||||
let part_2 = "Part 2: ".to_string();
|
|
||||||
let newline = "\n".to_string();
|
|
||||||
let part_2_solution = "116".to_string();
|
|
||||||
answers.push_str(&part_1);
|
|
||||||
answers.push_str(&answer.to_string());
|
|
||||||
answers.push_str(&newline);
|
|
||||||
answers.push_str(&part_2);
|
|
||||||
answers.push_str(&part_2_solution);
|
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
day: 1,
|
day: 1,
|
||||||
answer: answers,
|
answer_1: part_1.to_string(),
|
||||||
|
answer_2: part_2,
|
||||||
};
|
};
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
|
|||||||
@ -66,17 +66,11 @@ pub fn run_day_2() -> Solution {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SOLUTION.
|
// SOLUTION.
|
||||||
let mut answers = String::new();
|
let part_1 = "97289".to_string();
|
||||||
let p1_intro = "Part 1: 97289".to_string();
|
|
||||||
let newline = "\n".to_string();
|
|
||||||
answers.push_str(&p1_intro);
|
|
||||||
answers.push_str(&newline);
|
|
||||||
let p2_intro = "Part 2: ".to_string();
|
|
||||||
answers.push_str(&p2_intro);
|
|
||||||
answers.push_str(&part_2_solution);
|
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
day: 2,
|
day: 2,
|
||||||
answer: answers,
|
answer_1: part_1,
|
||||||
|
answer_2: part_2_solution,
|
||||||
};
|
};
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
|
|||||||
@ -58,20 +58,12 @@ pub fn day_3() -> Solution {
|
|||||||
}
|
}
|
||||||
let p2_solution = valid_triangles.to_string();
|
let p2_solution = valid_triangles.to_string();
|
||||||
|
|
||||||
let mut answers = String::new();
|
|
||||||
let part_1 = "Part 1: ".to_string();
|
|
||||||
let newline = "\n".to_string();
|
|
||||||
answers.push_str(&part_1);
|
|
||||||
answers.push_str(&p1_solution);
|
|
||||||
answers.push_str(&newline);
|
|
||||||
let part_2 = "Part 2: ".to_string();
|
|
||||||
answers.push_str(&part_2);
|
|
||||||
answers.push_str(&p2_solution);
|
|
||||||
|
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
day: 3,
|
day: 3,
|
||||||
answer: answers,
|
answer_1: p1_solution,
|
||||||
|
answer_2: p2_solution
|
||||||
};
|
};
|
||||||
|
|
||||||
solution
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,14 +6,32 @@ pub mod day3;
|
|||||||
#[derive(Tabled)]
|
#[derive(Tabled)]
|
||||||
pub struct Solution {
|
pub struct Solution {
|
||||||
day: u64,
|
day: u64,
|
||||||
answer: String,
|
answer_1: String,
|
||||||
|
answer_2: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn build_answer(&self) -> String {
|
||||||
|
let mut answer = String::new();
|
||||||
|
let part_1_intro = "Part 1: ".to_string();
|
||||||
|
let part_2_intro = "Part 2: ".to_string();
|
||||||
|
let newline = "\n".to_string();
|
||||||
|
answer.push_str(&part_1_intro);
|
||||||
|
answer.push_str(&self.answer_1);
|
||||||
|
answer.push_str(&newline);
|
||||||
|
answer.push_str(&part_2_intro);
|
||||||
|
answer.push_str(&self.answer_2);
|
||||||
|
|
||||||
|
answer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut rows = Vec::new();
|
let mut rows = Vec::new();
|
||||||
let day1 = day1::run_day_1();
|
let day1 = day1::run_day_1().build_answer();
|
||||||
let day2 = day2::run_day_2();
|
let day2 = day2::run_day_2().build_answer();
|
||||||
let day3 = day3::day_3();
|
let day3 = day3::day_3().build_answer();
|
||||||
|
println!("{:?}", day3);
|
||||||
rows.push(day1);
|
rows.push(day1);
|
||||||
rows.push(day2);
|
rows.push(day2);
|
||||||
rows.push(day3);
|
rows.push(day3);
|
||||||
|
|||||||
1000
2024/rust/inputs/day2.txt
Normal file
1000
2024/rust/inputs/day2.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2024/rust/inputs/day2test.txt
Normal file
6
2024/rust/inputs/day2test.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
7 6 4 2 1
|
||||||
|
1 2 7 8 9
|
||||||
|
9 7 6 2 1
|
||||||
|
1 3 2 4 5
|
||||||
|
8 6 4 4 1
|
||||||
|
1 3 6 7 9
|
||||||
@ -2,7 +2,7 @@ use crate::Solution;
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn run_day_1_part_1() -> Solution {
|
pub fn run_day_1() -> Solution {
|
||||||
let location_ids = fs::read_to_string("./inputs/day1.txt").expect("Unable to read file");
|
let location_ids = fs::read_to_string("./inputs/day1.txt").expect("Unable to read file");
|
||||||
let location_lines = location_ids.split("\n");
|
let location_lines = location_ids.split("\n");
|
||||||
// Create location vectors.
|
// Create location vectors.
|
||||||
@ -45,19 +45,10 @@ pub fn run_day_1_part_1() -> Solution {
|
|||||||
part_2_distance = part_2_distance + value;
|
part_2_distance = part_2_distance + value;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut answers = String::new();
|
|
||||||
let part_1 = "Part 1: ".to_string();
|
|
||||||
answers.push_str(&part_1);
|
|
||||||
answers.push_str(&part_1_distance.to_string());
|
|
||||||
let newline = "\n".to_string();
|
|
||||||
answers.push_str(&newline);
|
|
||||||
let part_2 = "Part 2: ".to_string();
|
|
||||||
answers.push_str(&part_2);
|
|
||||||
answers.push_str(&part_2_distance.to_string());
|
|
||||||
|
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
day: 1,
|
day: 1,
|
||||||
answer: answers,
|
answer_1: part_1_distance.to_string(),
|
||||||
|
answer_2: part_2_distance.to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
|
|||||||
130
2024/rust/src/day2.rs
Normal file
130
2024/rust/src/day2.rs
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
use crate::Solution;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
pub fn run_day_2() -> Solution {
|
||||||
|
// Fetch the file.
|
||||||
|
let reports_file = fs::read_to_string("./inputs/day2.txt").expect("unable to read file");
|
||||||
|
// Init the safe reports count.
|
||||||
|
let mut safe_reports = 0;
|
||||||
|
// iterate over each line to check if it matches the conditions.
|
||||||
|
for line in reports_file.lines() {
|
||||||
|
let mut increasing_or_decreasing = false;
|
||||||
|
// cast to string.
|
||||||
|
let line_string = line.to_string();
|
||||||
|
// create a vector with each character for iteration.
|
||||||
|
let report_numbers: Vec<u32> = line_string.split_whitespace().map(|s| s.parse().expect("Conversion Failed")).collect();
|
||||||
|
// Now we have report_nums populated with the numbers to check.
|
||||||
|
let increasing = is_increasing(&report_numbers);
|
||||||
|
let decreasing = is_decreasing(&report_numbers);
|
||||||
|
if increasing || decreasing {
|
||||||
|
increasing_or_decreasing = true;
|
||||||
|
}
|
||||||
|
if !increasing_or_decreasing {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Now we have to check that the distance is between 1 and 3.
|
||||||
|
let valid_gaps = check_diffs(&report_numbers);
|
||||||
|
if valid_gaps {
|
||||||
|
safe_reports += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let solution = Solution {
|
||||||
|
day: 2,
|
||||||
|
answer_1: safe_reports.to_string(),
|
||||||
|
answer_2: "Not Sure".to_string(),
|
||||||
|
};
|
||||||
|
solution
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_increasing(report_nums: &Vec<u32>) -> bool {
|
||||||
|
let mut increasing = false;
|
||||||
|
for i in 0..report_nums.len() {
|
||||||
|
// If we are on the last iteration, continue.
|
||||||
|
if i == report_nums.len() - 1 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let current_num = report_nums[i as usize];
|
||||||
|
let next_num = report_nums[(i as i32 + 1) as usize];
|
||||||
|
if next_num <= current_num {
|
||||||
|
increasing = false;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
increasing = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
increasing
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_decreasing(report_nums: &Vec<u32>) -> bool {
|
||||||
|
let mut decreasing = false;
|
||||||
|
for i in 0..report_nums.len() {
|
||||||
|
// If we are on the last iteration, continue.
|
||||||
|
if i == report_nums.len() - 1 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let current_num = report_nums[i as usize];
|
||||||
|
let next_num = report_nums[(i as i32 + 1) as usize];
|
||||||
|
if next_num >= current_num {
|
||||||
|
decreasing = false;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
decreasing = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
decreasing
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_diffs(report_numbers: &Vec<u32>) -> bool {
|
||||||
|
let mut valid_diffs = true;
|
||||||
|
let _last_iteration = report_numbers.len();
|
||||||
|
for i in 0..report_numbers.len() {
|
||||||
|
let current_number = report_numbers[i as usize];
|
||||||
|
println!("Iteration: {:?}", i);
|
||||||
|
match i {
|
||||||
|
0 => {
|
||||||
|
let next_number: u32 = report_numbers[(i as u32 + 1) as usize];
|
||||||
|
let diff_1: u32 = diff(current_number, next_number);
|
||||||
|
if !is_valid_diff(diff_1) {
|
||||||
|
valid_diffs = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
last_iteration => {
|
||||||
|
let prior_number: u32 = report_numbers[(i as u32 - 1) as usize];
|
||||||
|
let diff_1: u32 = diff(current_number, prior_number);
|
||||||
|
if !is_valid_diff(diff_1) {
|
||||||
|
valid_diffs = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
let next_number: u32 = report_numbers[(i as u32 + 1) as usize];
|
||||||
|
let prior_number: u32 = report_numbers[(i as u32 - 1) as usize];
|
||||||
|
let diff_1: u32 = diff(current_number, next_number);
|
||||||
|
let diff_2: u32 = diff(current_number, prior_number);
|
||||||
|
if !is_valid_diff(diff_1) || !is_valid_diff(diff_2) {
|
||||||
|
valid_diffs = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !valid_diffs {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn diff(a: u32, b: u32) -> u32 {
|
||||||
|
if a < b {
|
||||||
|
return b - a;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a - b;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid_diff(diff: u32) -> bool {
|
||||||
|
let mut valid = true;
|
||||||
|
if diff > 3 || diff < 1 {
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
valid
|
||||||
|
}
|
||||||
@ -1,16 +1,36 @@
|
|||||||
use tabled::{Table, Tabled};
|
use tabled::{Table, Tabled};
|
||||||
pub mod day1;
|
pub mod day1;
|
||||||
|
pub mod day2;
|
||||||
|
|
||||||
#[derive(Tabled)]
|
#[derive(Tabled)]
|
||||||
pub struct Solution {
|
pub struct Solution {
|
||||||
day: u64,
|
day: u64,
|
||||||
answer: String,
|
answer_1: String,
|
||||||
|
answer_2: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn build_answer(&self) -> String {
|
||||||
|
let mut answer = String::new();
|
||||||
|
let part_1_intro = "Part 1: ".to_string();
|
||||||
|
let part_2_intro = "Part 2: ".to_string();
|
||||||
|
let newline = "\n".to_string();
|
||||||
|
answer.push_str(&part_1_intro);
|
||||||
|
answer.push_str(&self.answer_1);
|
||||||
|
answer.push_str(&newline);
|
||||||
|
answer.push_str(&part_2_intro);
|
||||||
|
answer.push_str(&self.answer_2);
|
||||||
|
|
||||||
|
answer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut rows = Vec::new();
|
let mut rows = Vec::new();
|
||||||
let day1_p1 = day1::run_day_1_part_1();
|
let day1 = day1::run_day_1();
|
||||||
rows.push(day1_p1);
|
let day2 = day2::run_day_2();
|
||||||
|
rows.push(day1);
|
||||||
|
rows.push(day2);
|
||||||
|
|
||||||
let table = Table::new(rows);
|
let table = Table::new(rows);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user