Advertisement
tech_hutch

Untitled

Dec 7th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.84 KB | None | 0 0
  1. extern crate chrono;
  2.  
  3. use chrono::DateTime;
  4. use chrono::offset::Utc;
  5.  
  6. fn fmt_secs(start: DateTime<Utc>) -> String {
  7.     format!("⏱ {}s", (Utc::now() - start).num_milliseconds() as f64 / 1000_f64)
  8. }
  9.  
  10. fn main() {
  11.     let time = Utc::now();
  12.     const COORDS_STR: &str = include_str!("../../input");
  13.     let points: Vec<[u16; 2]> = COORDS_STR
  14.         .trim()
  15.         .lines()
  16.         .map(|line| if let [x, y] = line
  17.             .splitn(2, ", ")
  18.             .map(|s| s.parse::<u16>().unwrap())
  19.             .collect::<Vec<u16>>()[..] { [x, y] }
  20.         else { panic!("u wot m8"); })
  21.         .collect();
  22.     // let points: Vec<[u16; 2]> = vec![
  23.     //     [1, 1],
  24.     //     [1, 6],
  25.     //     [8, 3],
  26.     //     [3, 4],
  27.     //     [5, 5],
  28.     //     [8, 9],
  29.     // ];
  30.     println!("Prep: {}", fmt_secs(time));
  31.  
  32.     let time = Utc::now();
  33.     println!("Part 1: {} {}", part1(&points), fmt_secs(time));
  34. }
  35.  
  36. fn part1(points: &Vec<[u16; 2]>) -> u32 {
  37.     let max_x = *points.iter().map(|[x, _]| x).max().unwrap();
  38.     let max_y = *points.iter().map(|[_, y]| y).max().unwrap();
  39.  
  40.     let excl_x = [0, max_x];
  41.     let excl_y = [0, max_y];
  42.     let mut areas: Vec<Option<u32>> = vec![Some(0); points.len()];
  43.     for x in 0..=max_x {
  44.         for y in 0..=max_y {
  45.             let point_i = closest_point_pos(x, y, points);
  46.             if let Some(point_i) = point_i {
  47.                 if let Some(n) = areas[point_i] {
  48.                     areas[point_i] = if excl_x.contains(&x) || excl_y.contains(&y) {
  49.                         None
  50.                     } else {
  51.                         Some(n + 1)
  52.                     };
  53.                 }
  54.             }
  55.         }
  56.     }
  57.  
  58.     areas.into_iter().filter_map(|maybe_area| maybe_area).max().unwrap()
  59. }
  60.  
  61. fn closest_point_pos(x: u16, y: u16, points: &Vec<[u16; 2]>) -> Option<usize> {
  62.     let max_dists: Vec<u16> = points.into_iter().map(|&[x2, y2]| (
  63.         (x as i32 - x2 as i32).abs() +
  64.         (y as i32 - y2 as i32).abs()
  65.     ) as u16).collect();
  66.     let max = max_dists.iter().max().unwrap();
  67.     let maxes: Vec<(usize, &u16)> = max_dists
  68.         .iter()
  69.         .enumerate()
  70.         .filter(|&(_, dist)| dist == max)
  71.         .collect();
  72.     assert_ne!(maxes.len(), 0);
  73.     if maxes.len() == 1 {
  74.         Some(maxes[0].0)
  75.     } else {
  76.         None
  77.     }
  78.     // let mut close_p = usize::max_value();
  79.     // let mut min_dist = u16::max_value();
  80.     // for (i, &[x2, y2]) in points.into_iter().enumerate() {
  81.     //     let dist = (
  82.     //         (x as i32 - x2 as i32).abs() +
  83.     //         (y as i32 - y2 as i32).abs()
  84.     //     ) as u16;
  85.     //     if dist < min_dist {
  86.     //         min_dist = dist;
  87.     //         close_p = i;
  88.     //         // If this is one of the given points, we can break early
  89.     //         if min_dist == 0 { break; }
  90.     //     }
  91.     // }
  92.     // close_p
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement