|
@@ -0,0 +1,418 @@
|
|
1
|
+#[macro_use]
|
|
2
|
+extern crate lazy_static;
|
|
3
|
+
|
|
4
|
+use std::collections::HashSet;
|
|
5
|
+use std::error::Error;
|
|
6
|
+use std::fs::File;
|
|
7
|
+use std::io::{prelude::*, BufReader};
|
|
8
|
+use std::ops::AddAssign;
|
|
9
|
+use std::ops::Index;
|
|
10
|
+use std::result;
|
|
11
|
+use std::str::FromStr;
|
|
12
|
+
|
|
13
|
+use num::integer::lcm;
|
|
14
|
+use regex::Regex;
|
|
15
|
+
|
|
16
|
+type Result<T> = result::Result<T, Box<dyn Error>>;
|
|
17
|
+
|
|
18
|
+const INPUT: &str = "input/input.txt";
|
|
19
|
+
|
|
20
|
+#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
|
21
|
+struct Vector {
|
|
22
|
+ x: i64,
|
|
23
|
+ y: i64,
|
|
24
|
+ z: i64,
|
|
25
|
+}
|
|
26
|
+
|
|
27
|
+#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
|
28
|
+struct Body {
|
|
29
|
+ position: Vector,
|
|
30
|
+ velocity: Vector,
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
|
34
|
+struct NBody {
|
|
35
|
+ bodies: Vec<Body>,
|
|
36
|
+}
|
|
37
|
+
|
|
38
|
+impl FromStr for Body {
|
|
39
|
+ type Err = Box<dyn Error>;
|
|
40
|
+
|
|
41
|
+ fn from_str(s: &str) -> Result<Body> {
|
|
42
|
+ lazy_static! {
|
|
43
|
+ static ref RE: Regex =
|
|
44
|
+ Regex::new(r"<x=(?P<x>-?\d+), y=(?P<y>-?\d+), z=(?P<z>-?\d+)>").unwrap();
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ let captures = match RE.captures(s) {
|
|
48
|
+ None => {
|
|
49
|
+ return Err(From::from("Malformed scan, no positions could be found"));
|
|
50
|
+ }
|
|
51
|
+ Some(captures) => captures,
|
|
52
|
+ };
|
|
53
|
+
|
|
54
|
+ Ok(Body {
|
|
55
|
+ position: Vector {
|
|
56
|
+ x: captures["x"].parse()?,
|
|
57
|
+ y: captures["y"].parse()?,
|
|
58
|
+ z: captures["z"].parse()?,
|
|
59
|
+ },
|
|
60
|
+ velocity: Vector::new(),
|
|
61
|
+ })
|
|
62
|
+ }
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+impl Vector {
|
|
66
|
+ fn new() -> Vector {
|
|
67
|
+ Vector { x: 0, y: 0, z: 0 }
|
|
68
|
+ }
|
|
69
|
+}
|
|
70
|
+
|
|
71
|
+impl AddAssign for Vector {
|
|
72
|
+ fn add_assign(&mut self, other: Self) {
|
|
73
|
+ *self = Self {
|
|
74
|
+ x: self.x + other.x,
|
|
75
|
+ y: self.y + other.y,
|
|
76
|
+ z: self.z + other.z,
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+}
|
|
80
|
+
|
|
81
|
+impl Index<&str> for Vector {
|
|
82
|
+ type Output = i64;
|
|
83
|
+
|
|
84
|
+ fn index(&self, index: &str) -> &i64 {
|
|
85
|
+ match index {
|
|
86
|
+ "x" => &self.x,
|
|
87
|
+ "y" => &self.y,
|
|
88
|
+ "z" => &self.z,
|
|
89
|
+ _ => panic!("unknown field: {}", index),
|
|
90
|
+ }
|
|
91
|
+ }
|
|
92
|
+}
|
|
93
|
+
|
|
94
|
+impl Body {
|
|
95
|
+ fn add_gravity(&self, gravity: &mut Vector, other: &Self) {
|
|
96
|
+ if self.position.x > other.position.x {
|
|
97
|
+ gravity.x -= 1;
|
|
98
|
+ } else if self.position.x < other.position.x {
|
|
99
|
+ gravity.x += 1;
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ if self.position.y > other.position.y {
|
|
103
|
+ gravity.y -= 1;
|
|
104
|
+ } else if self.position.y < other.position.y {
|
|
105
|
+ gravity.y += 1;
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ if self.position.z > other.position.z {
|
|
109
|
+ gravity.z -= 1;
|
|
110
|
+ } else if self.position.z < other.position.z {
|
|
111
|
+ gravity.z += 1;
|
|
112
|
+ }
|
|
113
|
+ }
|
|
114
|
+}
|
|
115
|
+
|
|
116
|
+impl NBody {
|
|
117
|
+ fn run_step(&mut self) {
|
|
118
|
+ let mut gravities = Vec::new();
|
|
119
|
+ for body in self.bodies.iter() {
|
|
120
|
+ let mut gravity = Vector::new();
|
|
121
|
+ for other_body in self.bodies.iter() {
|
|
122
|
+ body.add_gravity(&mut gravity, other_body);
|
|
123
|
+ }
|
|
124
|
+ gravities.push(gravity);
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ for (index, gravity) in gravities.into_iter().enumerate() {
|
|
128
|
+ self.bodies[index].velocity += gravity;
|
|
129
|
+ let velocity = self.bodies[index].velocity;
|
|
130
|
+ self.bodies[index].position += velocity;
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ fn total_energy(&self) -> i64 {
|
|
135
|
+ let mut total_energy = 0;
|
|
136
|
+ for body in self.bodies.iter() {
|
|
137
|
+ let potential_energy =
|
|
138
|
+ body.position.x.abs() + body.position.y.abs() + body.position.z.abs();
|
|
139
|
+ let kinetic_energy =
|
|
140
|
+ body.velocity.x.abs() + body.velocity.y.abs() + body.velocity.z.abs();
|
|
141
|
+ total_energy += potential_energy * kinetic_energy;
|
|
142
|
+ }
|
|
143
|
+ total_energy
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ fn state(&self, component: &str) -> [(i64, i64); 4] {
|
|
147
|
+ [
|
|
148
|
+ (
|
|
149
|
+ self.bodies[0].position[component],
|
|
150
|
+ self.bodies[0].velocity[component],
|
|
151
|
+ ),
|
|
152
|
+ (
|
|
153
|
+ self.bodies[1].position[component],
|
|
154
|
+ self.bodies[1].velocity[component],
|
|
155
|
+ ),
|
|
156
|
+ (
|
|
157
|
+ self.bodies[2].position[component],
|
|
158
|
+ self.bodies[2].velocity[component],
|
|
159
|
+ ),
|
|
160
|
+ (
|
|
161
|
+ self.bodies[3].position[component],
|
|
162
|
+ self.bodies[3].velocity[component],
|
|
163
|
+ ),
|
|
164
|
+ ]
|
|
165
|
+ }
|
|
166
|
+}
|
|
167
|
+
|
|
168
|
+fn read_moon_scan(filename: &str) -> Result<NBody> {
|
|
169
|
+ let file = File::open(filename)?;
|
|
170
|
+ let reader = BufReader::new(file);
|
|
171
|
+ let mut moons = vec![];
|
|
172
|
+
|
|
173
|
+ for line in reader.lines() {
|
|
174
|
+ moons.push(line?.parse()?);
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ Ok(NBody { bodies: moons })
|
|
178
|
+}
|
|
179
|
+
|
|
180
|
+fn solve_part1(filename: &str) -> Result<i64> {
|
|
181
|
+ let mut nbody = read_moon_scan(filename)?;
|
|
182
|
+ for _ in 0..1000 {
|
|
183
|
+ nbody.run_step();
|
|
184
|
+ }
|
|
185
|
+ Ok(nbody.total_energy())
|
|
186
|
+}
|
|
187
|
+
|
|
188
|
+fn solve_part2(filename: &str) -> Result<u64> {
|
|
189
|
+ let mut step_count = 0;
|
|
190
|
+ let mut x_states: HashSet<[(i64, i64); 4]> = HashSet::new();
|
|
191
|
+ let mut y_states: HashSet<[(i64, i64); 4]> = HashSet::new();
|
|
192
|
+ let mut z_states: HashSet<[(i64, i64); 4]> = HashSet::new();
|
|
193
|
+ let mut x_repeated_step_count = None;
|
|
194
|
+ let mut y_repeated_step_count = None;
|
|
195
|
+ let mut z_repeated_step_count = None;
|
|
196
|
+ let mut nbody = read_moon_scan(filename)?;
|
|
197
|
+ while x_repeated_step_count == None
|
|
198
|
+ || y_repeated_step_count == None
|
|
199
|
+ || z_repeated_step_count == None
|
|
200
|
+ {
|
|
201
|
+ if x_repeated_step_count == None {
|
|
202
|
+ let x_state = nbody.state("x");
|
|
203
|
+ if x_states.contains(&x_state) {
|
|
204
|
+ x_repeated_step_count = Some(step_count);
|
|
205
|
+ } else {
|
|
206
|
+ x_states.insert(x_state);
|
|
207
|
+ }
|
|
208
|
+ }
|
|
209
|
+
|
|
210
|
+ if y_repeated_step_count == None {
|
|
211
|
+ let y_state = nbody.state("y");
|
|
212
|
+ if y_states.contains(&y_state) {
|
|
213
|
+ y_repeated_step_count = Some(step_count);
|
|
214
|
+ } else {
|
|
215
|
+ y_states.insert(y_state);
|
|
216
|
+ }
|
|
217
|
+ }
|
|
218
|
+
|
|
219
|
+ if z_repeated_step_count == None {
|
|
220
|
+ let z_state = nbody.state("z");
|
|
221
|
+ if z_states.contains(&z_state) {
|
|
222
|
+ z_repeated_step_count = Some(step_count);
|
|
223
|
+ } else {
|
|
224
|
+ z_states.insert(z_state);
|
|
225
|
+ }
|
|
226
|
+ }
|
|
227
|
+
|
|
228
|
+ nbody.run_step();
|
|
229
|
+ step_count += 1;
|
|
230
|
+ }
|
|
231
|
+
|
|
232
|
+ Ok(lcm(
|
|
233
|
+ x_repeated_step_count.unwrap(),
|
|
234
|
+ lcm(
|
|
235
|
+ y_repeated_step_count.unwrap(),
|
|
236
|
+ z_repeated_step_count.unwrap(),
|
|
237
|
+ ),
|
|
238
|
+ ))
|
|
239
|
+}
|
|
240
|
+
|
|
241
|
+fn main() -> Result<()> {
|
|
242
|
+ println!("Part 1: {}", solve_part1(INPUT)?);
|
|
243
|
+ println!("Part 2: {}", solve_part2(INPUT)?);
|
|
244
|
+
|
|
245
|
+ Ok(())
|
|
246
|
+}
|
|
247
|
+
|
|
248
|
+#[cfg(test)]
|
|
249
|
+mod tests {
|
|
250
|
+ use super::*;
|
|
251
|
+
|
|
252
|
+ const TEST_INPUT1: &str = "input/test1.txt";
|
|
253
|
+ const TEST_INPUT2: &str = "input/test2.txt";
|
|
254
|
+ fn nbody_1() -> NBody {
|
|
255
|
+ NBody {
|
|
256
|
+ bodies: vec![
|
|
257
|
+ Body {
|
|
258
|
+ position: Vector { x: -1, y: 0, z: 2 },
|
|
259
|
+ velocity: Vector { x: 0, y: 0, z: 0 },
|
|
260
|
+ },
|
|
261
|
+ Body {
|
|
262
|
+ position: Vector {
|
|
263
|
+ x: 2,
|
|
264
|
+ y: -10,
|
|
265
|
+ z: -7,
|
|
266
|
+ },
|
|
267
|
+ velocity: Vector { x: 0, y: 0, z: 0 },
|
|
268
|
+ },
|
|
269
|
+ Body {
|
|
270
|
+ position: Vector { x: 4, y: -8, z: 8 },
|
|
271
|
+ velocity: Vector { x: 0, y: 0, z: 0 },
|
|
272
|
+ },
|
|
273
|
+ Body {
|
|
274
|
+ position: Vector { x: 3, y: 5, z: -1 },
|
|
275
|
+ velocity: Vector { x: 0, y: 0, z: 0 },
|
|
276
|
+ },
|
|
277
|
+ ],
|
|
278
|
+ }
|
|
279
|
+ }
|
|
280
|
+ fn nbody_1_after_10_steps() -> NBody {
|
|
281
|
+ NBody {
|
|
282
|
+ bodies: vec![
|
|
283
|
+ Body {
|
|
284
|
+ position: Vector { x: 2, y: 1, z: -3 },
|
|
285
|
+ velocity: Vector { x: -3, y: -2, z: 1 },
|
|
286
|
+ },
|
|
287
|
+ Body {
|
|
288
|
+ position: Vector { x: 1, y: -8, z: 0 },
|
|
289
|
+ velocity: Vector { x: -1, y: 1, z: 3 },
|
|
290
|
+ },
|
|
291
|
+ Body {
|
|
292
|
+ position: Vector { x: 3, y: -6, z: 1 },
|
|
293
|
+ velocity: Vector { x: 3, y: 2, z: -3 },
|
|
294
|
+ },
|
|
295
|
+ Body {
|
|
296
|
+ position: Vector { x: 2, y: 0, z: 4 },
|
|
297
|
+ velocity: Vector { x: 1, y: -1, z: -1 },
|
|
298
|
+ },
|
|
299
|
+ ],
|
|
300
|
+ }
|
|
301
|
+ }
|
|
302
|
+ fn nbody_2() -> NBody {
|
|
303
|
+ NBody {
|
|
304
|
+ bodies: vec![
|
|
305
|
+ Body {
|
|
306
|
+ position: Vector {
|
|
307
|
+ x: -8,
|
|
308
|
+ y: -10,
|
|
309
|
+ z: 0,
|
|
310
|
+ },
|
|
311
|
+ velocity: Vector { x: 0, y: 0, z: 0 },
|
|
312
|
+ },
|
|
313
|
+ Body {
|
|
314
|
+ position: Vector { x: 5, y: 5, z: 10 },
|
|
315
|
+ velocity: Vector { x: 0, y: 0, z: 0 },
|
|
316
|
+ },
|
|
317
|
+ Body {
|
|
318
|
+ position: Vector { x: 2, y: -7, z: 3 },
|
|
319
|
+ velocity: Vector { x: 0, y: 0, z: 0 },
|
|
320
|
+ },
|
|
321
|
+ Body {
|
|
322
|
+ position: Vector { x: 9, y: -8, z: -3 },
|
|
323
|
+ velocity: Vector { x: 0, y: 0, z: 0 },
|
|
324
|
+ },
|
|
325
|
+ ],
|
|
326
|
+ }
|
|
327
|
+ }
|
|
328
|
+ fn nbody_2_after_100_steps() -> NBody {
|
|
329
|
+ NBody {
|
|
330
|
+ bodies: vec![
|
|
331
|
+ Body {
|
|
332
|
+ position: Vector {
|
|
333
|
+ x: 8,
|
|
334
|
+ y: -12,
|
|
335
|
+ z: -9,
|
|
336
|
+ },
|
|
337
|
+ velocity: Vector { x: -7, y: 3, z: 0 },
|
|
338
|
+ },
|
|
339
|
+ Body {
|
|
340
|
+ position: Vector {
|
|
341
|
+ x: 13,
|
|
342
|
+ y: 16,
|
|
343
|
+ z: -3,
|
|
344
|
+ },
|
|
345
|
+ velocity: Vector {
|
|
346
|
+ x: 3,
|
|
347
|
+ y: -11,
|
|
348
|
+ z: -5,
|
|
349
|
+ },
|
|
350
|
+ },
|
|
351
|
+ Body {
|
|
352
|
+ position: Vector {
|
|
353
|
+ x: -29,
|
|
354
|
+ y: -11,
|
|
355
|
+ z: -1,
|
|
356
|
+ },
|
|
357
|
+ velocity: Vector { x: -3, y: 7, z: 4 },
|
|
358
|
+ },
|
|
359
|
+ Body {
|
|
360
|
+ position: Vector {
|
|
361
|
+ x: 16,
|
|
362
|
+ y: -13,
|
|
363
|
+ z: 23,
|
|
364
|
+ },
|
|
365
|
+ velocity: Vector { x: 7, y: 1, z: 1 },
|
|
366
|
+ },
|
|
367
|
+ ],
|
|
368
|
+ }
|
|
369
|
+ }
|
|
370
|
+
|
|
371
|
+ #[test]
|
|
372
|
+ fn reads_moon_scan_file() {
|
|
373
|
+ assert_eq!(read_moon_scan(TEST_INPUT1).unwrap(), nbody_1());
|
|
374
|
+ assert_eq!(read_moon_scan(TEST_INPUT2).unwrap(), nbody_2());
|
|
375
|
+ }
|
|
376
|
+
|
|
377
|
+ #[test]
|
|
378
|
+ fn runs_10_steps() {
|
|
379
|
+ let mut nbody = read_moon_scan(TEST_INPUT1).unwrap();
|
|
380
|
+ for _ in 0..10 {
|
|
381
|
+ nbody.run_step();
|
|
382
|
+ }
|
|
383
|
+ assert_eq!(nbody, nbody_1_after_10_steps());
|
|
384
|
+ }
|
|
385
|
+
|
|
386
|
+ #[test]
|
|
387
|
+ fn runs_100_steps() {
|
|
388
|
+ let mut nbody = read_moon_scan(TEST_INPUT2).unwrap();
|
|
389
|
+ for _ in 0..100 {
|
|
390
|
+ nbody.run_step();
|
|
391
|
+ }
|
|
392
|
+ assert_eq!(nbody, nbody_2_after_100_steps());
|
|
393
|
+ }
|
|
394
|
+
|
|
395
|
+ #[test]
|
|
396
|
+ fn calculates_total_energy_after_10_steps() {
|
|
397
|
+ let mut nbody = read_moon_scan(TEST_INPUT1).unwrap();
|
|
398
|
+ for _ in 0..10 {
|
|
399
|
+ nbody.run_step();
|
|
400
|
+ }
|
|
401
|
+ assert_eq!(nbody.total_energy(), 179);
|
|
402
|
+ }
|
|
403
|
+
|
|
404
|
+ #[test]
|
|
405
|
+ fn calculates_total_energy_after_100_steps() {
|
|
406
|
+ let mut nbody = read_moon_scan(TEST_INPUT2).unwrap();
|
|
407
|
+ for _ in 0..100 {
|
|
408
|
+ nbody.run_step();
|
|
409
|
+ }
|
|
410
|
+ assert_eq!(nbody.total_energy(), 1940);
|
|
411
|
+ }
|
|
412
|
+
|
|
413
|
+ #[test]
|
|
414
|
+ fn finds_repeated_states() {
|
|
415
|
+ assert_eq!(solve_part2(TEST_INPUT1).unwrap(), 2772);
|
|
416
|
+ assert_eq!(solve_part2(TEST_INPUT2).unwrap(), 4686774924);
|
|
417
|
+ }
|
|
418
|
+}
|