add solution for day 4 part 1
This commit is contained in:
parent
61615b2736
commit
964119473d
1 changed files with 49 additions and 2 deletions
|
@ -1,8 +1,55 @@
|
|||
use crate::cli::PathBuf;
|
||||
use crate::util::read_lines;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Lines};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn part1(lines: Lines<BufReader<File>>) {
|
||||
let mut full_containt: u32 = 0;
|
||||
let mut overlapping: u32 = 0;
|
||||
for line in lines {
|
||||
let temp_line = line;
|
||||
let mut team: Vec<u32> = Vec::new();
|
||||
temp_line
|
||||
.unwrap()
|
||||
.split(",")
|
||||
.collect::<Vec<&str>>()
|
||||
.iter()
|
||||
.for_each(|s| {
|
||||
s.split("-")
|
||||
.into_iter()
|
||||
.for_each(|s| team.push(s.parse::<u32>().unwrap()))
|
||||
});
|
||||
let t1_start: &u32 = team.get(0).unwrap();
|
||||
let t1_end: &u32 = team.get(1).unwrap();
|
||||
let t2_start: &u32 = team.get(2).unwrap();
|
||||
let t2_end: &u32 = team.get(3).unwrap();
|
||||
if (t1_start >= t2_start && t1_end <= t2_end)
|
||||
|| (t2_start >= t1_start && t2_end <= t1_end)
|
||||
|| (t1_start == t2_end &&t2_start == t2_end)
|
||||
|| (t1_end == t2_end && t2_start == t2_end)
|
||||
{
|
||||
println!(
|
||||
"Gruppe full: {:?}-{:?}, {:?}-{:?}",
|
||||
t1_start, t1_end, t2_start, t2_end
|
||||
);
|
||||
//println!("{:?}", team);
|
||||
full_containt += 1;
|
||||
} else if (t1_start < t2_end && t1_end < t2_end) || (t2_start >= t1_start) {
|
||||
println!(
|
||||
"Gruppe part: {:?}-{:?}, {:?}-{:?}",
|
||||
t1_start, t1_end, t2_start, t2_end
|
||||
);
|
||||
overlapping += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("full {:?}", full_containt);
|
||||
println!("part {:?}", overlapping);
|
||||
println!("total {:?}", full_containt + overlapping);
|
||||
}
|
||||
|
||||
pub fn subcmd_day004(file: &Option<PathBuf>, alt: &bool) {
|
||||
if let Ok(lines) = read_lines(file.as_ref().unwrap().as_path()) {
|
||||
|
||||
part1(lines);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue