Advent of Code 2024 - Day 1 & 2
This commit is contained in:
parent
7865e624c5
commit
7ed2746251
15 changed files with 2245 additions and 1 deletions
1000
advent_of_code_2024/src/day1/input.txt
Normal file
1000
advent_of_code_2024/src/day1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
3
advent_of_code_2024/src/day1/mod.rs
Normal file
3
advent_of_code_2024/src/day1/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
pub mod part1;
|
||||
pub mod part2;
|
||||
26
advent_of_code_2024/src/day1/part1.rs
Normal file
26
advent_of_code_2024/src/day1/part1.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
pub fn solve(input: &String) {
|
||||
let all_numbers = input.split_whitespace()
|
||||
.filter(|str| !str.is_empty())
|
||||
.map(|str| str.parse::<i32>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut left = Vec::new();
|
||||
let mut right = Vec::new();
|
||||
|
||||
for idx in (0..all_numbers.len()).step_by(2) {
|
||||
left.push(all_numbers[idx]);
|
||||
right.push(all_numbers[idx + 1]);
|
||||
}
|
||||
|
||||
left.sort();
|
||||
right.sort();
|
||||
|
||||
let mut sum_of_distances = 0;
|
||||
|
||||
for idx in 0..left.len() {
|
||||
sum_of_distances += (left[idx] - right[idx]).abs();
|
||||
}
|
||||
|
||||
println!("sum of distances: {}", sum_of_distances);
|
||||
}
|
||||
29
advent_of_code_2024/src/day1/part2.rs
Normal file
29
advent_of_code_2024/src/day1/part2.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
pub fn solve(input: &String) {
|
||||
let all_numbers = input.split_whitespace()
|
||||
.filter(|str| !str.is_empty())
|
||||
.map(|str| str.parse::<i32>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut left = Vec::new();
|
||||
let mut right = Vec::new();
|
||||
|
||||
for idx in (0..all_numbers.len()).step_by(2) {
|
||||
left.push(all_numbers[idx]);
|
||||
right.push(all_numbers[idx + 1]);
|
||||
}
|
||||
|
||||
let mut similarity_score: i32 = 0;
|
||||
let mut counts = HashMap::<i32, i32>::new();
|
||||
|
||||
for num in right.iter() {
|
||||
*counts.entry(*num).or_default() += 1;
|
||||
}
|
||||
|
||||
for num in left.iter() {
|
||||
similarity_score += num * *counts.entry(*num).or_default();
|
||||
}
|
||||
|
||||
println!("Similarity score: {}", similarity_score);
|
||||
}
|
||||
1000
advent_of_code_2024/src/day2/input.txt
Normal file
1000
advent_of_code_2024/src/day2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
3
advent_of_code_2024/src/day2/mod.rs
Normal file
3
advent_of_code_2024/src/day2/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
pub mod part1;
|
||||
pub mod part2;
|
||||
59
advent_of_code_2024/src/day2/part1.rs
Normal file
59
advent_of_code_2024/src/day2/part1.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum ReportType {
|
||||
Increasing,
|
||||
Decreasing,
|
||||
Unsafe,
|
||||
Unknown
|
||||
}
|
||||
|
||||
impl ReportType {
|
||||
|
||||
pub fn update(self, value: ReportType) -> Self {
|
||||
match self {
|
||||
ReportType::Decreasing | ReportType::Increasing => if value == self { self } else { ReportType::Unsafe },
|
||||
ReportType::Unsafe => self,
|
||||
ReportType::Unknown => value,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_safe(&self) -> bool {
|
||||
match self {
|
||||
ReportType::Increasing | ReportType::Decreasing => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_report(report: &Vec<i32>) -> ReportType {
|
||||
let mut report_type = ReportType::Unknown;
|
||||
|
||||
for idx in 0..(report.len() - 1) {
|
||||
let step = match report[idx] - report[idx + 1] {
|
||||
-3 | -2 | -1 => ReportType::Decreasing,
|
||||
1 | 2 | 3 => ReportType::Increasing,
|
||||
_ => return ReportType::Unsafe
|
||||
};
|
||||
|
||||
report_type = report_type.update(step);
|
||||
}
|
||||
|
||||
report_type
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn solve(input: &String) {
|
||||
let reports = input.split("\r\n")
|
||||
.map(|line| line.split_whitespace().map(|str| str.parse::<i32>().unwrap()).collect::<Vec<_>>())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut safe_report_count = 0;
|
||||
|
||||
for report in reports.iter() {
|
||||
if ReportType::from_report(report).is_safe() {
|
||||
safe_report_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("safe reports count: {}", safe_report_count);
|
||||
}
|
||||
73
advent_of_code_2024/src/day2/part2.rs
Normal file
73
advent_of_code_2024/src/day2/part2.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum ReportType {
|
||||
Increasing,
|
||||
Decreasing,
|
||||
Unsafe,
|
||||
Unknown
|
||||
}
|
||||
|
||||
impl ReportType {
|
||||
|
||||
pub fn update(self, value: ReportType) -> Self {
|
||||
match self {
|
||||
ReportType::Decreasing | ReportType::Increasing => if value == self { self } else { ReportType::Unsafe },
|
||||
ReportType::Unsafe => self,
|
||||
ReportType::Unknown => value,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_safe(&self) -> bool {
|
||||
match self {
|
||||
ReportType::Increasing | ReportType::Decreasing => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_report(report: &Vec<i32>) -> ReportType {
|
||||
let mut report_type = ReportType::Unknown;
|
||||
|
||||
for idx in 0..(report.len() - 1) {
|
||||
let step = match report[idx] - report[idx + 1] {
|
||||
-3 | -2 | -1 => ReportType::Decreasing,
|
||||
1 | 2 | 3 => ReportType::Increasing,
|
||||
_ => return ReportType::Unsafe
|
||||
};
|
||||
|
||||
report_type = report_type.update(step);
|
||||
}
|
||||
|
||||
report_type
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn problem_dampener(report: &Vec<i32>) -> bool {
|
||||
// check default
|
||||
if ReportType::from_report(report).is_safe() {
|
||||
return true;
|
||||
}
|
||||
|
||||
for skipped in 0..report.len() {
|
||||
let copy = report.iter().enumerate().filter_map(|(idx, v)| (idx != skipped).then(|| *v)).collect::<Vec<_>>();
|
||||
if ReportType::from_report(©).is_safe() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn solve(input: &String) {
|
||||
let reports = input.split("\r\n")
|
||||
.map(|line| line.split_whitespace().map(|str| str.parse::<i32>().unwrap()).collect::<Vec<_>>())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut safe_report_count = 0;
|
||||
|
||||
for report in reports.iter() {
|
||||
problem_dampener(report).then(|| safe_report_count += 1);
|
||||
}
|
||||
|
||||
println!("safe reports count: {}", safe_report_count);
|
||||
}
|
||||
17
advent_of_code_2024/src/main.rs
Normal file
17
advent_of_code_2024/src/main.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
mod day1;
|
||||
mod day2;
|
||||
mod utility;
|
||||
|
||||
pub fn main() {
|
||||
let mut input = String::new();
|
||||
|
||||
println!("-- Day 1 --");
|
||||
input = utility::get_input_string(1);
|
||||
day1::part1::solve(&input);
|
||||
day1::part2::solve(&input);
|
||||
|
||||
println!("-- Day 2 --");
|
||||
input = utility::get_input_string(2);
|
||||
day2::part1::solve(&input);
|
||||
day2::part2::solve(&input);
|
||||
}
|
||||
5
advent_of_code_2024/src/utility/inputstring.rs
Normal file
5
advent_of_code_2024/src/utility/inputstring.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
pub fn get_input_string(day: u8) -> String {
|
||||
read_to_string(format!("src/day{}/input.txt", day)).expect("Failed to read input file.")
|
||||
}
|
||||
4
advent_of_code_2024/src/utility/mod.rs
Normal file
4
advent_of_code_2024/src/utility/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
|
||||
mod inputstring;
|
||||
pub use inputstring::get_input_string;
|
||||
Loading…
Add table
Add a link
Reference in a new issue