Completed day 1
This commit is contained in:
parent
20df559930
commit
b57b2db34d
5 changed files with 109 additions and 0 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -184,3 +184,9 @@ cython_debug/
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Added by cargo
|
||||||
|
|
||||||
|
/target
|
||||||
|
/inputs
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "AOC"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
14
src/main.rs
Normal file
14
src/main.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
use one::One;
|
||||||
|
use runbase::RunBase;
|
||||||
|
|
||||||
|
|
||||||
|
mod runbase;
|
||||||
|
mod one;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let one = One::new();
|
||||||
|
one.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
62
src/one.rs
Normal file
62
src/one.rs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
use std::{collections::HashMap, hash::Hash};
|
||||||
|
|
||||||
|
use crate::runbase::{read_lines, RunBase};
|
||||||
|
|
||||||
|
pub struct One {
|
||||||
|
lines: Vec<(i32, i32)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RunBase<One> for One {
|
||||||
|
fn new() -> One {
|
||||||
|
return One {
|
||||||
|
lines: read_lines("inputs/1")
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| {
|
||||||
|
let mut temp = x.split(" ");
|
||||||
|
return (
|
||||||
|
temp.next().unwrap().parse().unwrap(),
|
||||||
|
temp.next().unwrap().parse().unwrap(),
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self) {
|
||||||
|
self.run_1();
|
||||||
|
self.run_2();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_1(&self) {
|
||||||
|
let mut left_list: Vec<_> = self.lines.iter().map(|x| x.0).collect();
|
||||||
|
let mut right_list: Vec<_> = self.lines.iter().map(|x| x.1).collect();
|
||||||
|
left_list.sort();
|
||||||
|
right_list.sort();
|
||||||
|
let res = left_list
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.fold(0, |collect: i32, (i, x)| {
|
||||||
|
collect + (x - right_list.get(i).unwrap()).abs()
|
||||||
|
});
|
||||||
|
println!("{}", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_2(&self) {
|
||||||
|
let mut left_list: Vec<_> = self.lines.iter().map(|x| x.0).collect();
|
||||||
|
let mut right_list: Vec<_> = self.lines.iter().map(|x| x.1).collect();
|
||||||
|
let mut map:HashMap<i32, i32> = HashMap::new();
|
||||||
|
|
||||||
|
for left in &left_list{
|
||||||
|
map.insert(*left, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
for right in right_list {
|
||||||
|
if map.contains_key(&right) {
|
||||||
|
*map.get_mut(&right).unwrap() += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let sum = left_list.into_iter().fold(0, |acc,x| acc + (x * map.get(&x).unwrap()) );
|
||||||
|
println!("{}", sum);
|
||||||
|
}
|
||||||
|
}
|
21
src/runbase.rs
Normal file
21
src/runbase.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
|
||||||
|
pub fn read_lines(file: &str) -> Vec<String>{
|
||||||
|
match fs::read_to_string(file){
|
||||||
|
Ok(x) => {
|
||||||
|
return x.split("\n").map(|x| x.to_string()).collect::<Vec<String>>()
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
panic!("Could not read input file");
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait RunBase<T> {
|
||||||
|
fn new() -> T;
|
||||||
|
fn run(&self);
|
||||||
|
fn run_1(&self);
|
||||||
|
fn run_2(&self);
|
||||||
|
}
|
Loading…
Reference in a new issue