Day 1 on Day (almost) 3, sorry I'm late

This commit is contained in:
2024-12-03 01:37:03 +01:00
commit ae3c0533cc
7 changed files with 1067 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
mod sun_01;
pub fn today() {
let key = sun_01::sun_01("./assets/day_1_input").unwrap();
}

View File

@@ -0,0 +1,42 @@
use std::{fs::read_to_string, ops::Sub};
type Number = i32;
type Key = i32;
pub fn sun_01(input: &str) -> Result<Key, ()> {
let mut list_1: Vec<Number> = vec![];
let mut list_2: Vec<Number> = vec![];
let mut key: Number = 0;
for line in read_to_string(input).unwrap().lines() {
if let Some((one, two)) = line.split_once(" ") {
list_1.push(one.parse::<Number>().unwrap());
list_2.push(two.parse::<Number>().unwrap());
}
}
if list_1.len() != list_2.len() {
return Err(());
}
// 1. Pair up the numbers by size and compare the distance between them: Sorting
list_1.sort();
list_2.sort();
// 2. Calc the distance between the pair elements
for index in 0..list_1.len() {
key += calc_distance(list_1[index], list_2[index]);
}
Ok(Key)
}
fn calc_distance<T>(num_1: T, num_2: T) -> T
where
T: PartialOrd + Sub<Output = T>,
{
if num_1 > num_2 {
return num_1 - num_2;
}
return num_2 - num_1;
}

6
src/main.rs Normal file
View File

@@ -0,0 +1,6 @@
mod advent_of_code;
fn main() {
advent_of_code::today();
println!("Happy new year!");
}