aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

common.rs (653B)


      1 fn get_ints(line: &str) -> ((i64, i64), (i64, i64)) {
      2     let i = line.find('-').unwrap();
      3     let j = line.find(',').unwrap();
      4     let k = line[j..].find('-').unwrap() + j;
      5     ((line[..i].parse().unwrap(), line[i+1..j].parse().unwrap()),
      6      (line[j+1..k].parse().unwrap(), line[k+1..line.len()-1].parse().unwrap()))
      7 }
      8 
      9 pub fn count<F: Fn((i64, i64), (i64, i64)) -> bool>(condition: F) -> i64 {
     10     let mut line = String::new();
     11     let mut sum = 0;
     12     while std::io::stdin().read_line(&mut line).unwrap() > 0 {
     13         let (a, b) = get_ints(&line);
     14         if condition(a, b) {
     15             sum += 1;
     16         }
     17         line.clear();
     18     }
     19     sum
     20 }