diff options
author | tomsmeding <tom.smeding@gmail.com> | 2018-12-25 09:45:18 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2018-12-25 09:45:18 +0100 |
commit | 89d19c68bdd29e06fad53d61f13df1124b98ea6a (patch) | |
tree | 86ee5904641b925dc46bc4d93008cdddd1f66af4 /2018/src | |
parent | ef0344356e8305c49da75293a5ac738b9a263de1 (diff) |
Christmas! 🎄
Diffstat (limited to '2018/src')
-rw-r--r-- | 2018/src/day25.rs | 52 | ||||
-rw-r--r-- | 2018/src/main.rs | 4 |
2 files changed, 55 insertions, 1 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())) +} 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<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> { match day { @@ -59,6 +60,7 @@ fn day_switch<T: BufRead>(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")) } } |