aoc

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

b.rs (890B)


      1 use std::cmp::max;
      2 mod common;
      3 use common::*;
      4 
      5 #[allow(non_snake_case)]
      6 fn get_pos(sensors: &Vec<Sensor>, N: i64) -> Pos {
      7     for i in 0..=N {
      8         let mut ranges = sensors.iter()
      9             .map(|s| s.get_range(i))
     10             .filter(|r| r.left <= r.right)
     11             .collect::<Vec<_>>();
     12         ranges.sort();
     13         let mut lastr = ranges[0].right;
     14         for j in 1..ranges.len() {
     15             if lastr + 1 < ranges[j].left {
     16                 return Pos { x: lastr+1, y: i };
     17             }
     18             lastr = max(lastr, ranges[j].right);
     19         }
     20         if lastr < N {
     21             return Pos { x: N, y: i };
     22         }
     23     }
     24     panic!("Position not found");
     25 }
     26 
     27 fn main() {
     28     const N: i64 = 4000000;
     29     const M: i64 = 4000000;
     30     let sensors = read_sensors_from_stdin();
     31     let p = get_pos(&sensors, N);
     32     println!("x = {}, y = {} -> {}", p.x, p.y, p.x * M + p.y);
     33 }