summaryrefslogtreecommitdiff
path: root/src/day4.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/day4.rs')
-rw-r--r--src/day4.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/day4.rs b/src/day4.rs
new file mode 100644
index 0000000..46b9077
--- /dev/null
+++ b/src/day4.rs
@@ -0,0 +1,27 @@
+pub fn run(input: String) {
+ let count_overlaps = input.lines().map(|line| {
+ let (r1s, r2s) = line.split_once(",").expect("couldn't split on comma");
+ let (r1starts,r1ends) = r1s.split_once("-").expect("r1s no delimiter -");
+ let (r2starts,r2ends) = r2s.split_once("-").expect("r2s no delimiter -");
+ let r1start: u32 = r1starts.parse().expect("r1starts not a u32");
+ let r1end: u32 = r1ends.parse().expect("r1ends not a u32");
+ let r2start: u32 = r2starts.parse().expect("r2starts not a u32");
+ let r2end: u32 = r2ends.parse().expect("r2ends not a u32");
+ // fully contained (i.e. r1 is fully inside r2 or vice versa)
+ //let b1in2 = r1start >= r2start && r1end <= r2end;
+ //let b2in1 = r2start >= r1start && r2end <= r1end;
+ //return b1in2 || b2in1;
+
+ // partially contained (i.e. at least one end of r1 is contained in r2, or vice versa)
+ let e1 = r1start >= r2start && r1start <= r2end;
+ let e2 = r1end >= r2start && r1end <= r2end;
+ let e3 = r2start >= r1start && r2start <= r1end;
+ let e4 = r2end >= r1start && r2end <= r1end;
+ let res = e1 || e2 || e3 || e4;
+ //println!("{}: {}", line, res);
+ res
+ })
+ .filter(|x| {*x})
+ .count();
+ println!("Day 4: {}", count_overlaps);
+} \ No newline at end of file