finished day one for real for real

This commit is contained in:
2024-12-03 02:08:34 +01:00
parent f5a652ed8c
commit d653fcf6c4
5 changed files with 59 additions and 14 deletions

View File

@@ -1,5 +1,16 @@
mod sun_01;
mod types;
mod utils;
use types::*;
pub fn today() {
let key = sun_01::sun_01("./assets/day_1_input").unwrap();
historian_hysteria()
}
pub fn historian_hysteria() {
let key = sun_01::sun_01("./assets/day_1_input").unwrap();
println!("The key is: {key}");
let similarity = sun_01::get_similarity("./assets/day_1_input");
println!("The similarity is: {similarity}");
}

View File

@@ -0,0 +1,3 @@
use super::*;
pub fn mon_02() {}

View File

@@ -1,19 +1,11 @@
use std::{fs::read_to_string, ops::Sub};
use std::ops::Sub;
type Number = i32;
type Key = i32;
use super::*;
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;
let mut key: Id = 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());
}
}
let (mut list_1, mut list_2) = utils::read_id_lists(input);
if list_1.len() != list_2.len() {
return Err(());
@@ -28,7 +20,26 @@ pub fn sun_01(input: &str) -> Result<Key, ()> {
key += calc_distance(list_1[index], list_2[index]);
}
Ok(Key)
Ok(key)
}
pub fn get_similarity(input: &str) -> Similarity {
let (left, right) = utils::read_id_lists(input);
let mut similarity: Similarity = 0;
// Multiply every number in left by the number of times that its present in right
// Then sum every result
for left_element in left {
let mut count = 0;
for right_elment in &right {
if left_element == *right_elment {
count += 1;
}
}
similarity += left_element * count;
}
similarity
}
fn calc_distance<T>(num_1: T, num_2: T) -> T

View File

@@ -0,0 +1,3 @@
pub type Id = i32;
pub type Key = Id;
pub type Similarity = Id;

View File

@@ -0,0 +1,17 @@
use std::fs::read_to_string;
use super::*;
pub fn read_id_lists(input: &str) -> (Vec<Id>, Vec<Id>) {
let mut list_1: Vec<Id> = vec![];
let mut list_2: Vec<Id> = vec![];
for line in read_to_string(input).unwrap().lines() {
if let Some((one, two)) = line.split_once(" ") {
list_1.push(one.parse::<Id>().unwrap());
list_2.push(two.parse::<Id>().unwrap());
}
}
return (list_1, list_2);
}