aoc2022

Advent of Code 2022 solutions in Rust
git clone git://code.mfashby.net:/aoc2022
Log | Files | Refs

day4.rs (1337B)


      1 pub fn run(input: String) {
      2     let count_overlaps = input.lines().map(|line| {
      3         let (r1s, r2s) = line.split_once(",").expect("couldn't split on comma");
      4         let (r1starts,r1ends) = r1s.split_once("-").expect("r1s no delimiter -");
      5         let (r2starts,r2ends) = r2s.split_once("-").expect("r2s no delimiter -");
      6         let r1start: u32 = r1starts.parse().expect("r1starts not a u32");
      7         let r1end: u32 = r1ends.parse().expect("r1ends not a u32");
      8         let r2start: u32 = r2starts.parse().expect("r2starts not a u32");
      9         let r2end: u32 = r2ends.parse().expect("r2ends not a u32");
     10         // fully contained (i.e. r1 is fully inside r2 or vice versa)
     11         //let b1in2 = r1start >= r2start && r1end <= r2end;
     12         //let b2in1 = r2start >= r1start && r2end <= r1end;
     13         //return b1in2 || b2in1;
     14 
     15         // partially contained (i.e. at least one end of r1 is contained in r2, or vice versa)
     16         let e1 = r1start >= r2start && r1start <= r2end;
     17         let e2 = r1end >= r2start && r1end <= r2end;
     18         let e3 = r2start >= r1start && r2start <= r1end;
     19         let e4 = r2end >= r1start && r2end <= r1end;
     20         let res = e1 || e2 || e3 || e4;
     21         //println!("{}: {}", line, res);
     22         res
     23     }) 
     24         .filter(|x| {*x})
     25         .count();
     26     println!("Day 4: {}", count_overlaps);
     27 }