From cfd9b4f2ad1a09bedf7f764f84448a61faab54a3 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 19 Apr 2022 20:26:02 +0200 Subject: Refile for merging repos --- 2018/src/bin/day_25.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2018/src/bin/day_25.rs (limited to '2018/src/bin/day_25.rs') diff --git a/2018/src/bin/day_25.rs b/2018/src/bin/day_25.rs new file mode 100644 index 0000000..9e74f49 --- /dev/null +++ b/2018/src/bin/day_25.rs @@ -0,0 +1,47 @@ +extern crate advent_of_code_2018; +use advent_of_code_2018::*; + +use std::error::Error; +use std::path::PathBuf; + +// cargo watch -cs "cargo run --release --bin day_25" + +fn main() -> Result<(), Box> { + let input = read_file(&PathBuf::from("inputs/25.txt"))?; + + let mut constellations: Vec>> = input.iter() + .map(|line| line.split(',').map(|x| x.parse().unwrap()).collect::>()) + .map(|points| vec!(points)) + .collect(); + + // debug!(constellations); + + while join_constellations(&mut constellations) { + } + + debug!(constellations.len()); + + Ok(()) +} + +fn distance(xs: &[i32], ys: &[i32]) -> i32 { + xs.iter().zip(ys.iter()).map(|(x,y)| (x - y).abs()).sum() +} + +fn join_constellations(constellations: &mut Vec>>) -> bool { + for i in 0..constellations.len()-1 { + for j in i+1..constellations.len() { + let connected = constellations[i].iter().any(|ci| { + constellations[j].iter().any(|cj| { + distance(&ci, &cj) <= 3 + }) + }); + if connected { + let mut to_join = constellations.remove(j); + constellations[i].append(&mut to_join); + return true; + } + } + } + false +} -- cgit v1.2.3