summaryrefslogtreecommitdiff
path: root/2021/src/bin/day_19.rs
blob: 3fd8291be1eaae8919c8a1543be3740fcf4b5cf5 (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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use nom::{
    bytes::complete::tag,
    character::complete::{char as nom_char, i32 as nom_i32, line_ending, not_line_ending},
    combinator::map,
    multi::{many1, separated_list1},
    sequence::tuple,
    IResult,
};
use std::{collections::BTreeSet, fs};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input = fs::read_to_string("inputs/day_19.txt")?;
    let mut scanner_data = parse_scanner_cloud(&input).unwrap().1;
    scanner_data.align_scanners();
    let beacons = scanner_data.combine_beacons();
    dbg!(&beacons.len());
    dbg!(scanner_data.max_aligned_sensor_distance());
    Ok(())
}

#[derive(Default, Debug)]
struct ScannerCloud {
    aligned_scanners: Vec<Scanner>,
    aligned_checking_for_neighbours_scanners: Vec<Scanner>,
    unaligned_scanners: Vec<Scanner>,
}

impl ScannerCloud {
    fn new(mut scanners: Vec<Scanner>) -> ScannerCloud {
        if let Some(first_aligned_scanner) = scanners.pop() {
            ScannerCloud {
                aligned_scanners: Vec::new(),
                aligned_checking_for_neighbours_scanners: vec![first_aligned_scanner],
                unaligned_scanners: scanners,
            }
        } else {
            ScannerCloud::default()
        }
    }

    fn align_scanners(&mut self) {
        while let Some(current_aligned_scanner) =
            self.aligned_checking_for_neighbours_scanners.pop()
        {
            let mut to_remove_indices = Vec::new();
            for i in 0..self.unaligned_scanners.len() {
                if let Some(aligned) =
                    self.unaligned_scanners[i].try_align_with(&current_aligned_scanner)
                {
                    to_remove_indices.push(i);
                    self.aligned_checking_for_neighbours_scanners.push(aligned);
                }
            }
            for i in to_remove_indices.into_iter().rev() {
                self.unaligned_scanners.remove(i);
            }

            self.aligned_scanners.push(current_aligned_scanner);
        }

        assert_eq!(
            self.unaligned_scanners.len(),
            0,
            "Not all scanners were aligned"
        );
        assert_eq!(
            self.aligned_checking_for_neighbours_scanners.len(),
            0,
            "Not all aligned scanners were processed"
        );
    }

    fn combine_beacons(&self) -> BTreeSet<Point> {
        let mut combined_beacons = BTreeSet::new();
        for scanner in &self.aligned_scanners {
            combined_beacons.append(&mut scanner.beacons.clone())
        }
        combined_beacons
    }

    fn max_aligned_sensor_distance(&self) -> i32 {
        let mut max_distance = 0;
        for a in &self.aligned_scanners {
            for b in &self.aligned_scanners {
                let distance = a.position.manhattan_distance(&b.position);
                if distance > max_distance {
                    max_distance = distance;
                }
            }
        }
        max_distance
    }
}

#[derive(Debug, Clone)]
struct Scanner {
    position: Point,
    beacons: BTreeSet<Point>,
}

impl Scanner {
    fn try_align_with(&self, other: &Scanner) -> Option<Scanner> {
        for (roll, pitch) in [(0, 0), (1, 0), (2, 0), (3, 0), (0, 1), (0, 3)] {
            for yaw in [0, 1, 2, 3] {
                let candidate = self.spin(roll, pitch, yaw);
                for candidate_position in candidate.beacons.iter() {
                    for other_position in other.beacons.iter() {
                        let aligned_candidate =
                            candidate.position(*other_position - *candidate_position);
                        if aligned_candidate.count_overlap(other) >= 12 {
                            return Some(aligned_candidate);
                        }
                    }
                }
            }
        }
        None
    }

    fn spin(&self, roll: u8, pitch: u8, yaw: u8) -> Scanner {
        let beacons = self
            .beacons
            .clone()
            .into_iter()
            .map(|mut beacon| {
                for _ in 0..roll {
                    beacon = beacon.roll();
                }
                beacon
            })
            .map(|mut beacon| {
                for _ in 0..pitch {
                    beacon = beacon.pitch();
                }
                beacon
            })
            .map(|mut beacon| {
                for _ in 0..yaw {
                    beacon = beacon.yaw();
                }
                beacon
            })
            .collect();
        Scanner {
            position: self.position, // this is wrong, but doesn't matter because we spin then position
            beacons,
        }
    }

    fn position(&self, offset: Point) -> Scanner {
        Scanner {
            position: self.position + offset,
            beacons: self.beacons.iter().map(|b| *b + offset).collect(),
        }
    }

    fn count_overlap(&self, other: &Scanner) -> usize {
        self.beacons.intersection(&other.beacons).count()
    }
}

#[derive(
    Debug, Default, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, derive_more::Sub, derive_more::Add,
)]
struct Point {
    x: i32,
    y: i32,
    z: i32,
}

impl Point {
    fn roll(self) -> Self {
        Point {
            x: self.x,
            y: self.z,
            z: -self.y,
        }
    }

    fn pitch(self) -> Self {
        Point {
            x: -self.z,
            y: self.y,
            z: self.x,
        }
    }

    fn yaw(self) -> Self {
        Point {
            x: -self.y,
            y: self.x,
            z: self.z,
        }
    }

    fn manhattan_distance(&self, other: &Point) -> i32 {
        (self.x - other.x).abs() + (self.y - other.y).abs() + (self.z - other.z).abs()
    }
}

fn parse_scanner_cloud(input: &str) -> IResult<&str, ScannerCloud> {
    map(
        separated_list1(many1(line_ending), parse_scanner),
        ScannerCloud::new,
    )(input)
}

fn parse_scanner(input: &str) -> IResult<&str, Scanner> {
    let (input, _) = tuple((tag("--- scanner"), not_line_ending, line_ending))(input)?;
    map(separated_list1(line_ending, parse_point), |beacons| {
        Scanner {
            position: Point::default(),
            beacons: beacons.into_iter().collect(),
        }
    })(input)
}

fn parse_point(input: &str) -> IResult<&str, Point> {
    map(
        tuple((nom_i32, nom_char(','), nom_i32, nom_char(','), nom_i32)),
        |(x, _, y, _, z)| Point { x, y, z },
    )(input)
}