Advent of Code 2024 - Day 1 & 2

This commit is contained in:
Jos van Goor 2024-12-02 16:45:15 +01:00
parent 7865e624c5
commit 7ed2746251
15 changed files with 2245 additions and 1 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
**/a.out **/a.out
test/ test/
rust_ws/ rust_ws/
**/target/**

7
advent_of_code_2024/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "advent_of_code_2024"
version = "0.1.0"

View file

@ -0,0 +1,6 @@
[package]
name = "advent_of_code_2024"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -0,0 +1,11 @@
unstable_features = true
max_width = 120
binop_separator = "Back"
use_small_heuristics = "Off"
fn_single_line = false
indent_style = "Visual"
imports_granularity = "Module"
reorder_impl_items = true
group_imports = "StdExternalCrate"
enum_discrim_align_threshold = 120
struct_field_align_threshold = 120

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,3 @@
pub mod part1;
pub mod part2;

View 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);
}

View 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);
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,3 @@
pub mod part1;
pub mod part2;

View 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);
}

View 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(&copy).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);
}

View 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);
}

View 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.")
}

View file

@ -0,0 +1,4 @@
mod inputstring;
pub use inputstring::get_input_string;