finished day one for real for real
This commit is contained in:
@@ -1,5 +1,16 @@
|
|||||||
mod sun_01;
|
mod sun_01;
|
||||||
|
mod types;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use types::*;
|
||||||
|
|
||||||
pub fn today() {
|
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}");
|
||||||
}
|
}
|
||||||
|
|||||||
3
src/advent_of_code/mon_02.rs
Normal file
3
src/advent_of_code/mon_02.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn mon_02() {}
|
||||||
@@ -1,19 +1,11 @@
|
|||||||
use std::{fs::read_to_string, ops::Sub};
|
use std::ops::Sub;
|
||||||
|
|
||||||
type Number = i32;
|
use super::*;
|
||||||
type Key = i32;
|
|
||||||
|
|
||||||
pub fn sun_01(input: &str) -> Result<Key, ()> {
|
pub fn sun_01(input: &str) -> Result<Key, ()> {
|
||||||
let mut list_1: Vec<Number> = vec![];
|
let mut key: Id = 0;
|
||||||
let mut list_2: Vec<Number> = vec![];
|
|
||||||
let mut key: Number = 0;
|
|
||||||
|
|
||||||
for line in read_to_string(input).unwrap().lines() {
|
let (mut list_1, mut list_2) = utils::read_id_lists(input);
|
||||||
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() {
|
if list_1.len() != list_2.len() {
|
||||||
return Err(());
|
return Err(());
|
||||||
@@ -28,7 +20,26 @@ pub fn sun_01(input: &str) -> Result<Key, ()> {
|
|||||||
key += calc_distance(list_1[index], list_2[index]);
|
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
|
fn calc_distance<T>(num_1: T, num_2: T) -> T
|
||||||
|
|||||||
3
src/advent_of_code/types.rs
Normal file
3
src/advent_of_code/types.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub type Id = i32;
|
||||||
|
pub type Key = Id;
|
||||||
|
pub type Similarity = Id;
|
||||||
17
src/advent_of_code/utils.rs
Normal file
17
src/advent_of_code/utils.rs
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user