feat: implement day04
This commit is contained in:
parent
e6d7480e7d
commit
f67485afc6
63
src/day04.rs
Normal file
63
src/day04.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use crate::utils;
|
||||
|
||||
pub fn execute_task01(content: &str) {
|
||||
let vector = utils::convert_to_string_slice(&content);
|
||||
let pairs: Vec<((i32, i32), (i32, i32))> = vector.iter().map(|line| convert_to_tuple(line)).collect();
|
||||
|
||||
let fully_pairs: usize = pairs
|
||||
.into_iter()
|
||||
.filter(|pair| is_fully_in_other(pair.0, pair.1))
|
||||
.count();
|
||||
|
||||
println!("Day04 - Task01 - Sum of fully included pairs: {}", fully_pairs);
|
||||
}
|
||||
|
||||
pub fn execute_task02(content: &str) {
|
||||
let vector = utils::convert_to_string_slice(&content);
|
||||
let pairs: Vec<((i32, i32), (i32, i32))> = vector.iter().map(|line| convert_to_tuple(line)).collect();
|
||||
|
||||
let fully_pairs: usize = pairs
|
||||
.into_iter()
|
||||
.filter(|pair| is_partial_in_other(pair.0, pair.1))
|
||||
.count();
|
||||
|
||||
println!("Day04 - Task02 - Sum of partial included pairs: {}", fully_pairs);
|
||||
}
|
||||
|
||||
|
||||
fn convert_to_tuple(line: &str) -> ((i32, i32), (i32, i32)) {
|
||||
let pairs: Vec<(i32, i32)> = line
|
||||
.split(",")
|
||||
.map(|pair| {
|
||||
let digits: Vec<i32> = pair.split("-").map(|digit| digit.parse::<i32>().unwrap()).collect();
|
||||
return (digits[0], digits[1]);
|
||||
})
|
||||
.collect();
|
||||
|
||||
return (pairs[0], pairs[1]);
|
||||
}
|
||||
|
||||
fn is_fully_in_other(first: (i32, i32), second: (i32,i32)) -> bool {
|
||||
if first.0 <= second.0 && first.1 >= second.1 { // second is fully in first
|
||||
return true;
|
||||
}
|
||||
|
||||
if second.0 <= first.0 && second.1 >= first.1 { // first is fully in second
|
||||
return true;
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
fn is_partial_in_other(first: (i32, i32), second: (i32, i32)) -> bool {
|
||||
if first.0 <= second.0 && first.1 >= second.0 { // 5-7, 6-9
|
||||
return true
|
||||
}
|
||||
|
||||
if first.0 >= second.0 && first.0 <= second.1 { // 6-9, 5-7
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
1000
src/input/day04/input01
Normal file
1000
src/input/day04/input01
Normal file
File diff suppressed because it is too large
Load Diff
@ -2,10 +2,12 @@ mod utils;
|
||||
mod day01;
|
||||
mod day02;
|
||||
mod day03;
|
||||
mod day04;
|
||||
|
||||
const CONTENT01: &'static str = include_str!("input/day01/input01");
|
||||
const CONTENT02: &'static str = include_str!("input/day02/input01");
|
||||
const CONTENT03: &'static str = include_str!("input/day03/input01");
|
||||
const CONTENT04: &'static str = include_str!("input/day04/input01");
|
||||
|
||||
fn main() {
|
||||
day01::execute_task01(&CONTENT01);
|
||||
@ -15,5 +17,8 @@ fn main() {
|
||||
day02::execute_task02(&CONTENT02);
|
||||
|
||||
day03::execute_task01(&CONTENT03);
|
||||
day03::execute_task02(&CONTENT03)
|
||||
day03::execute_task02(&CONTENT03);
|
||||
|
||||
day04::execute_task01(&CONTENT04);
|
||||
day04::execute_task02(&CONTENT04)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user