aboutsummaryrefslogtreecommitdiff
path: root/2022/04
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-06-26 09:47:23 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-06-26 23:37:23 +0200
commitfa26f694d8cf98272763952a698301ee9935c19e (patch)
tree5af7a788fb8e9fa12b2b2fbea64250ed496a23c0 /2022/04
parentf0159b7d36e2c81182f2d047a34bf883394894db (diff)
downloadaoc-fa26f694d8cf98272763952a698301ee9935c19e.tar.gz
aoc-fa26f694d8cf98272763952a698301ee9935c19e.zip
Added 2022, first few problems
Diffstat (limited to '2022/04')
-rw-r--r--2022/04/a.rs7
-rw-r--r--2022/04/b.rs8
-rw-r--r--2022/04/common.rs20
3 files changed, 35 insertions, 0 deletions
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 @@
1mod common;
2use common::*;
3
4fn main() {
5 let contained = |a: (i64, i64), b: (i64, i64)| a.0 >= b.0 && a.1 <= b.1;
6 println!("{}", count(|a, b| contained(a, b) || contained(b, a)));
7}
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 @@
1mod common;
2use common::*;
3
4fn main() {
5 let overlap = |a: (i64, i64), b: (i64, i64)|
6 (a.0 >= b.0 && a.0 <= b.1) || (b.0 >= a.0 && b.0 <= a.1);
7 println!("{}", count(overlap));
8}
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 @@
1fn get_ints(line: &str) -> ((i64, i64), (i64, i64)) {
2 let i = line.find('-').unwrap();
3 let j = line.find(',').unwrap();
4 let k = line[j..].find('-').unwrap() + j;
5 ((line[..i].parse().unwrap(), line[i+1..j].parse().unwrap()),
6 (line[j+1..k].parse().unwrap(), line[k+1..line.len()-1].parse().unwrap()))
7}
8
9pub fn count<F: Fn((i64, i64), (i64, i64)) -> bool>(condition: F) -> i64 {
10 let mut line = String::new();
11 let mut sum = 0;
12 while std::io::stdin().read_line(&mut line).unwrap() > 0 {
13 let (a, b) = get_ints(&line);
14 if condition(a, b) {
15 sum += 1;
16 }
17 line.clear();
18 }
19 sum
20}

Generated with cgit - Back to sebastiano.tronto.net