summaryrefslogtreecommitdiff
path: root/2021/day19/src/main.rs
blob: 48882d2b81d2f69078d5e0c5b43ef8877eb2e13f (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::fs;
use std::path::Path;
use std::collections::HashSet;

type Point = (i32,i32,i32);
struct Scanner {
    beacons: Vec<Point>,
    diffs: HashSet<Point>
}

fn main() {
    let input = Path::new("resources").join("input.txt");
    let content = fs::read_to_string(input).expect("Unable to read from input");
    let scanners: Vec<Scanner> = content.split("\n\n").map(|s| parse_scanner(s)).collect();
    let perms: Vec<Box<dyn Fn((i32,i32,i32)) -> (i32,i32,i32)>> = vec![
        Box::new(|(x,y,z)| (x,y,z)),
        Box::new(|(x,y,z)| (-y,x,z)),
        Box::new(|(x,y,z)| (-x,-y,z)),
        Box::new(|(x,y,z)| (y,-x,z)),
        Box::new(|(x,y,z)| (-x,y,-z)),
        Box::new(|(x,y,z)| (y,x,-z)),
        Box::new(|(x,y,z)| (x,-y,-z)),
        Box::new(|(x,y,z)| (-y,-x,-z)),
        Box::new(|(x,y,z)| (-z,y,x)),
        Box::new(|(x,y,z)| (-y,-z,x)),
        Box::new(|(x,y,z)| (z,-y,x)),
        Box::new(|(x,y,z)| (y,z,x)),
        Box::new(|(x,y,z)| (z,y,-x)),
        Box::new(|(x,y,z)| (y,-z,-x)),
        Box::new(|(x,y,z)| (-z,-y,-x)),
        Box::new(|(x,y,z)| (-y,z,-x)),
        Box::new(|(x,y,z)| (x,-z,y)),
        Box::new(|(x,y,z)| (z,x,y)),
        Box::new(|(x,y,z)| (-x,z,y)),
        Box::new(|(x,y,z)| (-z,-x,y)),
        Box::new(|(x,y,z)| (x,z,-y)),
        Box::new(|(x,y,z)| (-z,x,-y)),
        Box::new(|(x,y,z)| (-x,-z,-y)),
        Box::new(|(x,y,z)| (z,-x,-y))
    ];
    for i in 0..scanners.len() {
        for j in i+1..scanners.len() {
            for perm in perms.iter() {
                let permset = &scanners[j].diffs.iter().map(|&p| perm(p)).collect();
                let common: HashSet<_> = scanners[i].diffs.intersection(&permset).collect();
                if common.len() > 11 {
                    println!("At least 12 points in common for scanners {}-{}", i, j)
                } 
            }
        }
    }
    let mut beacons = HashSet::new();
}

fn test(s1: &Scanner, s2: &Scanner, perm: &dyn Fn((i32,i32,i32)) -> (i32,i32,i32)) {
    let bs1 = s1.beacons;    
    let bs2: Vec<_> = s2.beacons.iter().map(|&p| perm(p)).collect();
    //let (p1, p2) = bs1.iter().map(|b1| bs2.iter().map(|b2| (b1,b2))).find(|((x1,y1,z1),(x2,y2,z2))| )<++>;
}

fn parse_scanner(s: &str) -> Scanner {
    let mut lines = s.lines();
    let _idx: u8 = lines.next().unwrap().split_whitespace().nth(2).unwrap().parse().unwrap();
    let beacons: Vec<Point> = lines.map(|l| {
        let mut coords = l.split(",");
        let x = coords.next().unwrap().parse().unwrap();
        let y = coords.next().unwrap().parse().unwrap();
        let z = coords.next().unwrap().parse().unwrap();
        (x,y,z)
    }).collect();
    let mut diffs = HashSet::new();
    for i in 1..beacons.len() {
        for j in 0..i {
            let (x1,y1,z1) = beacons[i];
            let (x2,y2,z2) = beacons[j];
            diffs.insert((x1-x2,y1-y2,z1-z2));
        }
    }
    Scanner { beacons, diffs }
}