use std::io; use std::io::BufRead; type Point = (i32, i32, i32, i32); fn dist(a: &Point, b: &Point) -> i32 { (a.0 - b.0).abs() + (a.1 - b.1).abs() + (a.2 - b.2).abs() + (a.3 - b.3).abs() } fn flood(pts: &[Point], comp: &mut [i32], at: usize, id: i32) { if comp[at] != -1 { assert!(comp[at] == id); return; } comp[at] = id; for i in 0..pts.len() { if dist(&pts[at], &pts[i]) <= 3 { flood(pts, comp, i, id); } } } pub fn main(reader: T) -> io::Result<(String, String)> { let pts = reader.lines().map(|l| { let line = l.unwrap(); let mut spl = line.split(','); let x = spl.next().unwrap().parse().unwrap(); let y = spl.next().unwrap().parse().unwrap(); let z = spl.next().unwrap().parse().unwrap(); let w = spl.next().unwrap().parse().unwrap(); (x, y, z, w) }).collect::>(); let mut comp = vec![-1; pts.len()]; let mut ncomps = 0; for i in 0..pts.len() { if comp[i] != -1 { continue; } flood(&pts, &mut comp, i, ncomps); ncomps += 1; } let part1 = ncomps; Ok((part1.to_string(), String::new())) }