1
0
Fork 0

day 4 solution

main
Andrew Coleman 2022-12-05 10:10:12 -05:00
parent 408a690061
commit 84842992d0
5 changed files with 1051 additions and 2 deletions

1000
2022/day4.txt Normal file

File diff suppressed because it is too large Load Diff

39
2022/src/days/day4.rs Normal file
View File

@ -0,0 +1,39 @@
use anyhow::Result;
use std::fs;
pub fn run() -> Result<()> {
let file_contents = fs::read_to_string("day4.txt")?;
let lines = file_contents.lines();
let mut part_one_pairs = 0;
let mut part_two_pairs = 0;
for line in lines {
let v: Vec<&str> = line.split(",").collect();
let left: Vec<u32> = v[0].split("-").map(|t| t.parse::<u32>().unwrap()).collect();
let right: Vec<u32> = v[1].split("-").map(|t| t.parse::<u32>().unwrap()).collect();
let left_outside = left[0] <= right[0] && left[1] >= right[1];
let left_inside = left[0] >= right[0] && left[1] <= right[1];
let left_covered = left_outside || left_inside;
let right_outside = right[0] <= left[0] && right[1] >= left[1];
let right_inside = right[0] >= left[0] && right[1] <= left[1];
let right_covered = right_outside || right_inside;
if left_covered && right_covered {
part_one_pairs += 1;
}
let left_1_inside = left[0] >= right[0] && left[0] <= right[1];
let left_2_inside = left[1] >= right[0] && left[1] <= right[1];
let right_1_inside = right[0] >= left[0] && right[0] <= left[1];
let right_2_inside = right[1] >= left[0] && right[1] <= left[1];
if left_1_inside || left_2_inside || right_1_inside || right_2_inside {
part_two_pairs += 1;
}
}
println!("part one pairs {}", part_one_pairs);
println!("part two pairs {}", part_two_pairs);
Ok(())
}

View File

@ -1,3 +1,4 @@
pub mod day1;
pub mod day2;
pub mod day3;
pub mod day4;

View File

@ -14,10 +14,13 @@ fn run_day(number: i32) -> Result<()> {
}
2 => {
days::day2::run()?;
},
}
3 => {
days::day3::run()?;
},
}
4 => {
days::day4::run()?;
}
_ => return Err(anyhow!("Invalid day provided")),
}
Ok(())

6
2022/tests/day4.txt Normal file
View File

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8