summaryrefslogtreecommitdiff
path: root/2018/src/bin/day_25.rs
blob: 9e74f49338fb6865e51d9ea24eaecd68aaaed8c4 (plain)
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
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<Error>> {
    let input = read_file(&PathBuf::from("inputs/25.txt"))?;

    let mut constellations: Vec<Vec<Vec<i32>>> = input.iter()
        .map(|line| line.split(',').map(|x| x.parse().unwrap()).collect::<Vec<_>>())
        .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<Vec<Vec<i32>>>) -> 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
}