From fa26f694d8cf98272763952a698301ee9935c19e Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Jun 2025 09:47:23 +0200 Subject: Added 2022, first few problems --- 2022/04/a.rs | 7 +++++++ 2022/04/b.rs | 8 ++++++++ 2022/04/common.rs | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 2022/04/a.rs create mode 100644 2022/04/b.rs create mode 100644 2022/04/common.rs (limited to '2022/04') diff --git a/2022/04/a.rs b/2022/04/a.rs new file mode 100644 index 0000000..d69821a --- /dev/null +++ b/2022/04/a.rs @@ -0,0 +1,7 @@ +mod common; +use common::*; + +fn main() { + let contained = |a: (i64, i64), b: (i64, i64)| a.0 >= b.0 && a.1 <= b.1; + println!("{}", count(|a, b| contained(a, b) || contained(b, a))); +} diff --git a/2022/04/b.rs b/2022/04/b.rs new file mode 100644 index 0000000..2affb65 --- /dev/null +++ b/2022/04/b.rs @@ -0,0 +1,8 @@ +mod common; +use common::*; + +fn main() { + let overlap = |a: (i64, i64), b: (i64, i64)| + (a.0 >= b.0 && a.0 <= b.1) || (b.0 >= a.0 && b.0 <= a.1); + println!("{}", count(overlap)); +} diff --git a/2022/04/common.rs b/2022/04/common.rs new file mode 100644 index 0000000..92c32f1 --- /dev/null +++ b/2022/04/common.rs @@ -0,0 +1,20 @@ +fn get_ints(line: &str) -> ((i64, i64), (i64, i64)) { + let i = line.find('-').unwrap(); + let j = line.find(',').unwrap(); + let k = line[j..].find('-').unwrap() + j; + ((line[..i].parse().unwrap(), line[i+1..j].parse().unwrap()), + (line[j+1..k].parse().unwrap(), line[k+1..line.len()-1].parse().unwrap())) +} + +pub fn count bool>(condition: F) -> i64 { + let mut line = String::new(); + let mut sum = 0; + while std::io::stdin().read_line(&mut line).unwrap() > 0 { + let (a, b) = get_ints(&line); + if condition(a, b) { + sum += 1; + } + line.clear(); + } + sum +} -- cgit v1.3