From 82be8473d21c15bc22c46cc4ba5463e3d2213c7d Mon Sep 17 00:00:00 2001 From: j-james Date: Mon, 5 Dec 2022 00:01:10 -0800 Subject: Day Four --- 2022/rust/day04/src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2022/rust/day04/src/main.rs (limited to '2022/rust/day04/src') diff --git a/2022/rust/day04/src/main.rs b/2022/rust/day04/src/main.rs new file mode 100644 index 0000000..3f8b9cd --- /dev/null +++ b/2022/rust/day04/src/main.rs @@ -0,0 +1,43 @@ +use std::env; +use std::fs; +use std::ops::RangeInclusive; + +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let assignments = input.trim().split('\n').map(|x| { + let mut vec = x.split(['-',',']).map(|x| x.parse::().unwrap()); + return [vec.next().unwrap(), vec.next().unwrap(), + vec.next().unwrap(), vec.next().unwrap()]; + }).collect::>(); + + println!("{}", assignments.iter() + .filter(|x| contains(x[0] ..= x[1], x[2] ..= x[3])).count()); + + println!("{}", assignments.iter() + .filter(|x| overlaps(x[0] ..= x[1], x[2] ..= x[3])).count()); +} + +fn contains(a: RangeInclusive::, b: RangeInclusive::) -> bool { + return b.contains(a.start()) && b.contains(a.end()) || + a.contains(b.start()) && a.contains(b.end()); +} + +fn overlaps(a: RangeInclusive::, b: RangeInclusive::) -> bool { + return b.contains(a.start()) || b.contains(a.end()) || + a.contains(b.start()) || a.contains(b.end()); +} + +/* +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let assignments = input.trim().split('\n') + .map(|x| {x.split(['-',',']).collect()}) + .collect::>>(); + + println!("{:?}", assignments); + +} + +*/ -- cgit v1.2.3-70-g09d2