From a0cab3bbeaabe452eb9b5a63ddfb2643caf79482 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 5 Dec 2022 01:02:49 +0100 Subject: [AoC2022] Rust 04 --- AoC2022/04/makefile | 2 +- AoC2022/04/solver.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 AoC2022/04/solver.rs (limited to 'AoC2022') 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::().unwrap()) + .collect::>()[..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.") +} -- cgit v1.2.3