2022 day4/rust: add solution

This commit is contained in:
Xiretza 2022-12-04 09:59:17 +01:00
parent 3e27ce8334
commit be02406c0f
4 changed files with 58 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "rust_2022_04"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,45 @@
use std::io::{stdin, Read};
fn main() {
let mut data = String::new();
stdin().read_to_string(&mut data).unwrap();
let pairs: Vec<_> = data
.lines()
.map(|line| {
let make_range = |s: &str| {
let (start, end) = s.split_once('-').unwrap();
let start: u64 = start.parse().unwrap();
let end = end.parse().unwrap();
assert!(start <= end);
start..=end
};
let (left, right) = line.split_once(',').unwrap();
let left = make_range(left);
let right = make_range(right);
(left, right)
})
.collect();
println!(
"{}",
pairs
.iter()
.filter(|(l, r)| l.start() == r.start()
|| l.end() == r.end()
|| l.start().cmp(r.start()) != l.end().cmp(r.end()))
.count()
);
println!(
"{}",
pairs
.iter()
.filter(|(l, r)| l.end() >= r.start() && r.end() >= l.start())
.count()
);
}

4
Cargo.lock generated
View file

@ -473,6 +473,10 @@ version = "0.1.0"
name = "rust_2022_03"
version = "0.1.0"
[[package]]
name = "rust_2022_04"
version = "0.1.0"
[[package]]
name = "ryu"
version = "1.0.11"

View file

@ -12,4 +12,5 @@ members = [
"2022/day1/rust",
"2022/day2/rust",
"2022/day3/rust",
"2022/day4/rust",
]