This commit is contained in:
2022-12-04 08:03:11 +00:00
parent e54aad2d33
commit 3bd486af95
4 changed files with 1044 additions and 2 deletions

38
src/day04/mod.rs Normal file
View File

@ -0,0 +1,38 @@
use crate::read;
fn get_ranges(elf: &str) -> std::ops::RangeInclusive<i32> {
let r = elf
.split_once('-')
.map(|e| (e.0.parse().unwrap(), e.1.parse().unwrap()))
.unwrap();
r.0..=r.1
}
pub fn run() {
let input = read("04");
let lines = input
.lines()
.map(|l| {
let pairs = l.split_once(',').unwrap();
(get_ranges(pairs.0), get_ranges(pairs.1))
})
.collect::<Vec<_>>();
#[cfg(feature = "part1")]
{
let count = lines
.iter()
.filter(|(a, b)| a.clone().all(|x| b.contains(&x)) || b.clone().all(|x| a.contains(&x)))
.count();
println!("Day 4, Part 01: {}", count);
}
#[cfg(feature = "part2")]
{
let count = lines
.iter()
.filter(|(a, b)| a.clone().any(|x| b.contains(&x)) || b.clone().any(|x| a.contains(&x)))
.count();
println!("Day 4, Part 02: {}", count);
}
}