This commit is contained in:
Tim Nope
2022-12-11 18:06:33 +01:00
commit 6b6fc8d486
1361 changed files with 19939 additions and 0 deletions

19
d04/src/a1.rs Normal file
View File

@ -0,0 +1,19 @@
fn contains(v1 :&Vec<i32>, v2 :&Vec<i32>) -> bool {
if v1.len() < 2 || v1.len() < 2 { return false; }
return v1[0] <= v2[0] && v2[1] <= v1[1];
}
pub fn run(inp :Vec<String>) {
let sum = inp.iter().map(|pair| {
let sec :Vec<&str> = pair.split(',').collect();
let first :Vec<i32> = sec[0].split('-').map(|p| p.parse::<i32>().unwrap()).collect();
let second :Vec<i32> = sec[1].split('-').map(|p| p.parse::<i32>().unwrap()).collect();
if contains(&first, &second) || contains(&second, &first) {1} else {0}
}).sum::<i32>();
println!("a1: {}", sum);
}

21
d04/src/a2.rs Normal file
View File

@ -0,0 +1,21 @@
fn contains(v1 :&Vec<i32>, v2 :&Vec<i32>) -> bool {
if v1.len() < 2 || v1.len() < 2 { return false; }
return (v1[0]..=v1[1]).contains(&v2[0]) ||
(v1[0]..=v1[1]).contains(&v2[1]);
}
pub fn run(inp :Vec<String>) {
let sum = inp.iter().map(|pair| {
let sec :Vec<&str> = pair.split(',').collect();
let first :Vec<i32> = sec[0].split('-').map(|p| p.parse::<i32>().unwrap()).collect();
let second :Vec<i32> = sec[1].split('-').map(|p| p.parse::<i32>().unwrap()).collect();
if contains(&first, &second) || contains(&second, &first) {1} else {0}
}).sum::<i32>();
println!("a1: {}", sum);
}

41
d04/src/main.rs Normal file
View File

@ -0,0 +1,41 @@
use std::io::BufRead;
mod a1;
mod a2;
fn read_file(path :&str) -> Vec<String> {
let file = std::fs::File::open(path);
return match file {
Ok(handle) => {
let reader = std::io::BufReader::new(handle);
let mut vec : Vec<String> = vec![];
reader.lines().for_each(|elem| {
vec.push(elem.unwrap());
});
vec
}
Err(_) => vec![]
}
}
fn main() {
let inp :Vec<String> = read_file("input.txt");
a1::run(inp.clone());
a2::run(inp);
}