Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate chrono;
- use chrono::DateTime;
- use chrono::offset::Utc;
- fn fmt_secs(start: DateTime<Utc>) -> String {
- format!("⏱ {}s", (Utc::now() - start).num_milliseconds() as f64 / 1000_f64)
- }
- fn main() {
- let time = Utc::now();
- const COORDS_STR: &str = include_str!("../../input");
- let points: Vec<[u16; 2]> = COORDS_STR
- .trim()
- .lines()
- .map(|line| if let [x, y] = line
- .splitn(2, ", ")
- .map(|s| s.parse::<u16>().unwrap())
- .collect::<Vec<u16>>()[..] { [x, y] }
- else { panic!("u wot m8"); })
- .collect();
- // let points: Vec<[u16; 2]> = vec![
- // [1, 1],
- // [1, 6],
- // [8, 3],
- // [3, 4],
- // [5, 5],
- // [8, 9],
- // ];
- println!("Prep: {}", fmt_secs(time));
- let time = Utc::now();
- println!("Part 1: {} {}", part1(&points), fmt_secs(time));
- }
- fn part1(points: &Vec<[u16; 2]>) -> u32 {
- let max_x = *points.iter().map(|[x, _]| x).max().unwrap();
- let max_y = *points.iter().map(|[_, y]| y).max().unwrap();
- let excl_x = [0, max_x];
- let excl_y = [0, max_y];
- let mut areas: Vec<Option<u32>> = vec![Some(0); points.len()];
- for x in 0..=max_x {
- for y in 0..=max_y {
- let point_i = closest_point_pos(x, y, points);
- if let Some(point_i) = point_i {
- if let Some(n) = areas[point_i] {
- areas[point_i] = if excl_x.contains(&x) || excl_y.contains(&y) {
- None
- } else {
- Some(n + 1)
- };
- }
- }
- }
- }
- areas.into_iter().filter_map(|maybe_area| maybe_area).max().unwrap()
- }
- fn closest_point_pos(x: u16, y: u16, points: &Vec<[u16; 2]>) -> Option<usize> {
- let max_dists: Vec<u16> = points.into_iter().map(|&[x2, y2]| (
- (x as i32 - x2 as i32).abs() +
- (y as i32 - y2 as i32).abs()
- ) as u16).collect();
- let max = max_dists.iter().max().unwrap();
- let maxes: Vec<(usize, &u16)> = max_dists
- .iter()
- .enumerate()
- .filter(|&(_, dist)| dist == max)
- .collect();
- assert_ne!(maxes.len(), 0);
- if maxes.len() == 1 {
- Some(maxes[0].0)
- } else {
- None
- }
- // let mut close_p = usize::max_value();
- // let mut min_dist = u16::max_value();
- // for (i, &[x2, y2]) in points.into_iter().enumerate() {
- // let dist = (
- // (x as i32 - x2 as i32).abs() +
- // (y as i32 - y2 as i32).abs()
- // ) as u16;
- // if dist < min_dist {
- // min_dist = dist;
- // close_p = i;
- // // If this is one of the given points, we can break early
- // if min_dist == 0 { break; }
- // }
- // }
- // close_p
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement