advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.rs (973B)

    1 use regex::Regex;
    2 
    3 fn main() {
    4     let re = Regex::new(r"^([0-9]+)-([0-9]+),([0-9]+)-([0-9]+)$").unwrap();
    5     let input = std::fs::read_to_string("input.txt")
    6         .unwrap()
    7         .trim()
    8         .split('\n')
    9         .map(|x| {
   10             re.captures(x)
   11                 .unwrap()
   12                 .iter()
   13                 .skip(1)
   14                 .map(|c| c.unwrap().as_str().parse::<i32>().unwrap())
   15                 .collect()
   16         })
   17         .collect::<Vec<Vec<i32>>>();
   18     // 477
   19     println!("Part 1: {}", part_1(&input));
   20     // 830
   21     println!("Part 2: {}", part_2(&input));
   22 }
   23 
   24 fn part_1(input: &Vec<Vec<i32>>) -> i32 {
   25     input
   26         .iter()
   27         .filter(|rs| (rs[0] <= rs[2] && rs[1] >= rs[3]) || (rs[2] <= rs[0] && rs[3] >= rs[1]))
   28         .count() as i32
   29 }
   30 
   31 fn part_2(input: &Vec<Vec<i32>>) -> i32 {
   32     input
   33         .iter()
   34         .filter(|rs| (rs[0] <= rs[2] && rs[2] <= rs[1]) || (rs[2] <= rs[0] && rs[0] <= rs[3]))
   35         .count() as i32
   36 }