From be02406c0f35650bee12640f5e25175f95d21d41 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sun, 4 Dec 2022 09:59:17 +0100 Subject: [PATCH] 2022 day4/rust: add solution --- 2022/day4/rust/Cargo.toml | 8 +++++++ 2022/day4/rust/src/main.rs | 45 ++++++++++++++++++++++++++++++++++++++ Cargo.lock | 4 ++++ Cargo.toml | 1 + 4 files changed, 58 insertions(+) create mode 100644 2022/day4/rust/Cargo.toml create mode 100644 2022/day4/rust/src/main.rs diff --git a/2022/day4/rust/Cargo.toml b/2022/day4/rust/Cargo.toml new file mode 100644 index 0000000..e996c8f --- /dev/null +++ b/2022/day4/rust/Cargo.toml @@ -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] diff --git a/2022/day4/rust/src/main.rs b/2022/day4/rust/src/main.rs new file mode 100644 index 0000000..5b5c611 --- /dev/null +++ b/2022/day4/rust/src/main.rs @@ -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() + ); +} diff --git a/Cargo.lock b/Cargo.lock index f73a21a..3e16e9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 5e2892a..74c5e3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ members = [ "2022/day1/rust", "2022/day2/rust", "2022/day3/rust", + "2022/day4/rust", ]