use std::io; use std::io::BufRead; fn dist(a: (i32, i32), b: (i32, i32)) -> i32 { (b.0 - a.0).abs() + (b.1 - a.1).abs() } pub fn main(reader: T) -> io::Result<(String, String)> { let pts: Vec<(i32, i32)> = reader.lines().map(|l| l.unwrap()).map(|line| { let mut spl = line.split(", ").map(|s| s.parse().unwrap()); let x = spl.next().unwrap(); let y = spl.next().unwrap(); (x, y) }).collect(); let &max_x = pts.iter().map(|(x, _)| x).max().unwrap(); let &max_y = pts.iter().map(|(_, y)| y).max().unwrap(); let min_x = -1; let min_y = -1; let mut borderpts = vec![false; pts.len()]; let mut size = vec![0; pts.len()]; let mut nsafe = 0; for y in min_y..max_y+1 { for x in min_x..max_x+1 { let mut closest = 0; let mut closest_dist = dist(pts[0], (x, y)); for i in 1..pts.len() { let d = dist(pts[i], (x, y)); if d < closest_dist { closest_dist = d; closest = i; } else if d == closest_dist { closest = usize::max_value(); } } if closest != usize::max_value() { if x == min_x || x == max_x || y == min_y || y == max_y { borderpts[closest] = true; } else { size[closest] += 1; } } let distsum: i32 = pts.iter().map(|&p| dist(p, (x, y))).sum(); if distsum < 10000 { nsafe += 1; } } } let part1 = size[(0..pts.len()).max_by_key(|&i| if borderpts[i] { -1 } else { size[i] }).unwrap()].to_string(); let part2 = nsafe.to_string(); Ok((part1, part2)) }