From 89d19c68bdd29e06fad53d61f13df1124b98ea6a Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 25 Dec 2018 09:45:18 +0100 Subject: Christmas! 🎄 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2018/src/day25.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2018/src/main.rs | 4 +++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 2018/src/day25.rs (limited to '2018/src') 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(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())) +} diff --git a/2018/src/main.rs b/2018/src/main.rs index 85021fe..9d26e8f 100644 --- a/2018/src/main.rs +++ b/2018/src/main.rs @@ -30,8 +30,9 @@ mod day21; mod day22; mod day23; mod day24; +mod day25; -static NUM_DAYS: i32 = 24; +static NUM_DAYS: i32 = 25; fn day_switch(day: i32, reader: T) -> io::Result<(String, String)> { match day { @@ -59,6 +60,7 @@ fn day_switch(day: i32, reader: T) -> io::Result<(String, String)> { 22 => day22::main(reader), 23 => day23::main(reader), 24 => day24::main(reader), + 25 => day25::main(reader), _ => Err(Error::new(ErrorKind::Other, "Invalid day")) } } -- cgit v1.2.3-70-g09d2