diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-05 01:02:49 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-05 01:02:49 +0100 |
commit | a0cab3bbeaabe452eb9b5a63ddfb2643caf79482 (patch) | |
tree | aba78888089eeb433faf4e51e2097761449dfc27 /AoC2022 | |
parent | 92f0857f8f3644e31739f63628156c4b0384b3ac (diff) | |
download | scratch-a0cab3bbeaabe452eb9b5a63ddfb2643caf79482.tar.gz scratch-a0cab3bbeaabe452eb9b5a63ddfb2643caf79482.tar.bz2 scratch-a0cab3bbeaabe452eb9b5a63ddfb2643caf79482.zip |
[AoC2022] Rust 04
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.") +} |