1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
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<T: BufRead>(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::<Vec<_>>();
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()))
}
|