-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.rs
185 lines (167 loc) · 4.79 KB
/
19.rs
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
#![feature(test)]
use std::{
hash::Hash,
ops::{Add, Sub},
};
use rustc_hash::{FxHashMap, FxHashSet};
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
struct Pos {
x: i32,
y: i32,
z: i32,
}
impl Pos {
fn rotation(&self, i: usize) -> Pos {
let &Pos { x, y, z } = self;
match i {
0 => Pos { x, y, z },
1 => Pos { x, y: -z, z: y },
2 => Pos { x, y: -y, z: -z },
3 => Pos { x, y: z, z: -y },
4 => Pos { x: -x, y: -y, z },
5 => Pos {
x: -x,
y: -z,
z: -y,
},
6 => Pos { x: -x, y, z: -z },
7 => Pos { x: -x, y: z, z: y },
8 => Pos { x: y, y: -x, z },
9 => Pos { x: y, y: -z, z: -x },
10 => Pos { x: y, y: x, z: -z },
11 => Pos { x: y, y: z, z: x },
12 => Pos { x: -y, y: x, z },
13 => Pos { x: -y, y: -z, z: x },
14 => Pos {
x: -y,
y: -x,
z: -z,
},
15 => Pos { x: -y, y: z, z: -x },
16 => Pos { x: z, y: -y, z: x },
17 => Pos { x: z, y: -x, z: -y },
18 => Pos { x: z, y, z: -x },
19 => Pos { x: z, y: x, z: y },
20 => Pos { x: -z, y, z: x },
21 => Pos { x: -z, y: -x, z: y },
22 => Pos {
x: -z,
y: -y,
z: -x,
},
23 => Pos { x: -z, y: x, z: -y },
_ => panic!(),
}
}
fn distance(self, other: Pos) -> i32 {
let diff = self - other;
diff.x.pow(2) + diff.y.pow(2) + diff.z.pow(2)
}
fn manhatten_distance(self, other: Pos) -> i32 {
let diff = self - other;
diff.x.abs() + diff.y.abs() + diff.z.abs()
}
}
impl Add for Pos {
type Output = Pos;
fn add(self, rhs: Self) -> Self::Output {
Pos {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl Sub for Pos {
type Output = Pos;
fn sub(self, rhs: Self) -> Self::Output {
Pos {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}
type Scanner = (Vec<Pos>, FxHashSet<i32>);
type Input = Vec<Scanner>;
fn setup(input: &str) -> Input {
input
.split("\n\n")
.map(|scanner| {
let poss: Vec<Pos> = scanner
.lines()
.skip(1)
.map(|line| {
let mut res = line.split(',').map(|x| x.parse().unwrap());
Pos {
x: res.next().unwrap(),
y: res.next().unwrap(),
z: res.next().unwrap(),
}
})
.collect();
let distances: FxHashSet<i32> = poss
.iter()
.flat_map(|a| poss.iter().map(|b| a.distance(*b)))
.collect();
(poss, distances)
})
.collect()
}
fn match_scanner(
beacons: &mut FxHashSet<Pos>,
scanner_positions: &mut Vec<Pos>,
scanner: &Scanner,
) {
for i in 0..24 {
let mut counter: FxHashMap<Pos, usize> = FxHashMap::default();
for rel in &scanner.0 {
let rel = rel.rotation(i);
for abs in beacons.iter() {
let k = *abs - rel;
let cnt = counter.get(&k).cloned().unwrap_or(0) + 1;
counter.insert(k, cnt);
if cnt >= 12 {
for x in &scanner.0 {
let x = x.rotation(i) + k;
beacons.insert(x);
}
scanner_positions.push(k);
return;
}
}
}
}
}
fn solve(input: &Input) -> (usize, i32) {
let mut remaining: Vec<Scanner> = input.to_vec();
let first = remaining.remove(0);
let mut beacons: FxHashSet<Pos> = FxHashSet::from_iter(first.0);
let mut distances: FxHashSet<i32> = FxHashSet::from_iter(first.1);
let mut scanners: Vec<Pos> = vec![];
while !remaining.is_empty() {
let i = (0..remaining.len())
.max_by_key(|&i| remaining[i].1.intersection(&distances).count())
.unwrap();
let s = remaining.remove(i);
match_scanner(&mut beacons, &mut scanners, &s);
for x in &s.1 {
distances.insert(*x);
}
}
(
beacons.len(),
scanners
.iter()
.flat_map(|x| scanners.iter().map(|&y| x.manhatten_distance(y)))
.max()
.unwrap(),
)
}
fn part1(input: &Input) -> usize {
solve(input).0
}
fn part2(input: &Input) -> i32 {
solve(input).1
}
aoc::main!(2021, 19, ex: 1);