aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/04/solver.rs
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/04/solver.rs')
-rw-r--r--AoC2022/04/solver.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/AoC2022/04/solver.rs b/AoC2022/04/solver.rs
new file mode 100644
index 0000000..8b979fa
--- /dev/null
+++ b/AoC2022/04/solver.rs
@@ -0,0 +1,43 @@
+use std::fs;
+use std::io::{self, BufRead};
+
+// fn subinterval(a0: i32, a1: i32, b0: i32, b1: i32) -> bool {
+// a0 <= b0 && b1 <= a1
+// }
+
+fn subcontained(a0: i32, a1: i32, b0: i32, b1: i32) -> bool {
+ // subinterval(a0, a1, b0, b1) || subinterval(b0, b1, a0, a1)
+ ((a0 - b0) * (a1 - b1)) <= 0
+}
+
+fn overlap(a0: i32, a1: i32, b0: i32, b1: i32) -> bool {
+ a0 <= b1 && b0 <= a1
+}
+
+fn main() {
+ let file = fs::File::open("input").unwrap();
+ let lines = io::BufReader::new(file).lines();
+ let mut subcont = 0;
+ let mut overlaps = 0;
+ for line in lines {
+ let li = line.expect("line");
+ if let [a0, a1, b0, b1] = li
+ .split(&[',', '-'])
+ .map(|x| x.parse::<i32>().unwrap())
+ .collect::<Vec<i32>>()[..4]
+ {
+ if subcontained(a0, a1, b0, b1) {
+ subcont += 1
+ }
+ if overlap(a0, a1, b0, b1) {
+ overlaps += 1
+ }
+ } else {
+ panic!("Bad line input")
+ };
+ }
+
+ assert_eq!(515, subcont);
+ assert_eq!(883, overlaps);
+ println!("All test passed.")
+}