use std::io; use std::io::BufRead; const W: i32 = 800; 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 mut borderpts = vec![false; pts.len()]; let mut size = vec![0; pts.len()]; let mut nsafe = 0; for y in -W..W+1 { for x in -W..W+1 { let closest = (0..pts.len()).min_by_key(|&i| dist(pts[i], (x, y))).unwrap(); if x == -W || x == W || y == -W || y == W { 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)) }