aboutsummaryrefslogtreecommitdiff
path: root/2022/15/a.rs
diff options
context:
space:
mode:
Diffstat (limited to '2022/15/a.rs')
-rw-r--r--2022/15/a.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/2022/15/a.rs b/2022/15/a.rs
new file mode 100644
index 0000000..7b87150
--- /dev/null
+++ b/2022/15/a.rs
@@ -0,0 +1,30 @@
1use std::cmp::max;
2use std::collections::HashSet;
3mod common;
4use common::*;
5
6fn main() {
7 const Y: i64 = 2000000;
8 let sensors = read_sensors_from_stdin();
9 let mut ranges = sensors.iter()
10 .map(|s| s.get_range(Y))
11 .filter(|r| r.left <= r.right)
12 .collect::<Vec<_>>();
13 ranges.sort();
14 let mut current = Range { left: i64::MIN, right: i64::MIN };
15 let mut sum = 0;
16 for r in ranges {
17 current = Range {
18 left: max(current.right+1, r.left),
19 right: max(current.right, r.right)
20 };
21 sum += (current.right - current.left + 1) as usize;
22 }
23 sum -= sensors.iter()
24 .map(|s| s.b)
25 .collect::<HashSet<_>>() // Remove duplicates
26 .iter()
27 .filter(|b| b.y == Y)
28 .count();
29 println!("{sum}");
30}

Generated with cgit - Back to sebastiano.tronto.net