diff options
Diffstat (limited to 'AoC2022')
-rw-r--r-- | AoC2022/04/makefile | 2 | ||||
-rw-r--r-- | AoC2022/04/solver.rs | 43 |
2 files changed, 44 insertions, 1 deletions
diff --git a/AoC2022/04/makefile b/AoC2022/04/makefile index f4940ca..a7661bd 100644 --- a/AoC2022/04/makefile +++ b/AoC2022/04/makefile @@ -11,4 +11,4 @@ run: sbcl --load ~/.sbclrc --script solver.lisp emacs -batch -l ert -l solver.el -f ert-run-tests-batch-and-exit - # rustc solver.rs && ./solver + rustc solver.rs && ./solver 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.") +} |