[rs] Solve 2022_04 part 1
This commit is contained in:
parent
58d59ecd97
commit
ce2613373a
5 changed files with 1033 additions and 0 deletions
|
|
@ -40,6 +40,7 @@ days! {
|
|||
Y2022D01: "2022_01",
|
||||
Y2022D02: "2022_02",
|
||||
Y2022D03: "2022_03",
|
||||
Y2022D04: "2022_04",
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
|
|
@ -75,6 +76,7 @@ fn main() -> io::Result<()> {
|
|||
Day::Y2022D01 => y2022::d01::solve(input),
|
||||
Day::Y2022D02 => y2022::d02::solve(input),
|
||||
Day::Y2022D03 => y2022::d03::solve(input),
|
||||
Day::Y2022D04 => y2022::d04::solve(input),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod d01;
|
||||
pub mod d02;
|
||||
pub mod d03;
|
||||
pub mod d04;
|
||||
|
|
|
|||
24
rs/src/y2022/d04.rs
Normal file
24
rs/src/y2022/d04.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use std::ops::RangeInclusive;
|
||||
|
||||
fn contains(a: &RangeInclusive<u32>, b: &RangeInclusive<u32>) -> bool {
|
||||
a.start() <= b.start() && b.end() <= a.end()
|
||||
}
|
||||
|
||||
pub fn solve(input: String) {
|
||||
let pairs = input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
let mut ranges = l.split(',').map(|p| {
|
||||
let mut numbers = p.split('-').map(|n| n.parse::<u32>().unwrap());
|
||||
numbers.next().unwrap()..=numbers.next().unwrap()
|
||||
});
|
||||
(ranges.next().unwrap(), ranges.next().unwrap())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let score = pairs
|
||||
.iter()
|
||||
.filter(|(a, b)| contains(a, b) || contains(b, a))
|
||||
.count();
|
||||
println!("Part 1: {score}");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue