summaryrefslogtreecommitdiff
path: root/2018/src/day25.rs
diff options
context:
space:
mode:
Diffstat (limited to '2018/src/day25.rs')
-rw-r--r--2018/src/day25.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/2018/src/day25.rs b/2018/src/day25.rs
new file mode 100644
index 0000000..1d992ec
--- /dev/null
+++ b/2018/src/day25.rs
@@ -0,0 +1,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()))
+}