[rs] Solve 2022_04 part 1

This commit is contained in:
Joscha 2022-12-04 12:34:38 +01:00
parent 58d59ecd97
commit ce2613373a
5 changed files with 1033 additions and 0 deletions

1000
inputs/2022_04 Normal file

File diff suppressed because it is too large Load diff

View file

@ -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),
}
}

View file

@ -1,3 +1,4 @@
pub mod d01;
pub mod d02;
pub mod d03;
pub mod d04;

24
rs/src/y2022/d04.rs Normal file
View 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}");
}

6
sample_inputs/2022_04 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