use std::fs; use std::collections::HashSet; use std::collections::HashMap; fn main() { let file = fs::read_to_string("input").expect("Unable to read file"); let lines = file.split("\n").collect::>(); let mut anchorpoints = HashSet::new(); let mut points_nearest_to_anchor = HashMap::new(); let mut min_x = std::i32::MAX; let mut min_y = std::i32::MAX; let mut max_x = 0; let mut max_y = 0; for line in lines { let coords = line.split(|c| c == ',' || c == ' ').collect::>(); let x = coords[0].parse::().unwrap(); let y = coords[2].parse::().unwrap(); anchorpoints.insert((x,y)); if x < min_x { min_x = x.clone(); } if y < min_y { min_y = y.clone(); } if x > max_x { max_x = x.clone(); } if y > max_y { max_y = y.clone(); } } for i in min_x..(max_x+1) { for j in min_y..(max_y+1) { let mut winning_x = 0; let mut winning_y = 0; let mut min_dist = std::i32::MAX; let mut second_min_dist = std::i32::MAX; for (x,y) in &anchorpoints{ let dist = (x - i + y - j).abs(); if dist < min_dist{ min_dist = dist; winning_x = x.clone(); winning_y = y.clone(); } else if dist < second_min_dist { second_min_dist = dist; } } if min_dist != second_min_dist { *points_nearest_to_anchor.entry((winning_x,winning_y)).or_insert(1) += 1; } } } let mut largest_area = 0; for area in points_nearest_to_anchor.values() { if *area > largest_area { largest_area = *area; } println!("{}", *area); } println!("{}", largest_area); }