-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_12.rs
433 lines (372 loc) · 11.5 KB
/
day_12.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::{common, math};
// Day 12 - Rain Risk
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Instruction {
North(i32),
East(i32),
South(i32),
West(i32),
Left(i32),
Right(i32),
Forward(i32),
}
impl std::str::FromStr for Instruction {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let amount = s[1..].parse().unwrap();
Ok(match &s[..1] {
"N" => Instruction::North(amount),
"E" => Instruction::East(amount),
"S" => Instruction::South(amount),
"W" => Instruction::West(amount),
"L" => Instruction::Left(amount),
"R" => Instruction::Right(amount),
"F" => Instruction::Forward(amount),
_ => unreachable!("Invalid instruction"),
})
}
}
#[derive(Debug, PartialEq, Eq)]
struct Ship {
x: i32,
y: i32,
angle: i32,
}
impl Ship {
fn north(&mut self, step: &i32) {
self.y += step;
}
fn south(&mut self, step: &i32) {
self.y -= step;
}
fn east(&mut self, step: &i32) {
self.x += step;
}
fn west(&mut self, step: &i32) {
self.x -= step;
}
fn turn(&mut self, angle: &i32) {
self.angle += angle
}
fn forward(&mut self, distance: &i32) {
let radians = math::degrees_to_radians(self.angle);
self.x = (radians.cos() * *distance as f32 + self.x as f32).round() as i32;
self.y = (radians.sin() * *distance as f32 + self.y as f32).round() as i32;
}
fn move_by_waypoint(&mut self, waypoint: &Waypoint, value: &i32) {
self.x += waypoint.x * value;
self.y += waypoint.y * value;
}
fn new() -> Self {
Self {
x: 0,
y: 0,
angle: 0,
}
}
}
#[derive(Debug, Eq, PartialEq)]
struct Waypoint {
x: i32,
y: i32,
}
impl Waypoint {
fn north(&mut self, step: &i32) {
self.y += step;
}
fn south(&mut self, step: &i32) {
self.y -= step;
}
fn east(&mut self, step: &i32) {
self.x += step;
}
fn west(&mut self, step: &i32) {
self.x -= step;
}
fn rotate(&mut self, angle: &i32) {
let (nx, ny) = math::rotation_2d(self.x, self.y, *angle);
self.x = nx;
self.y = ny;
}
fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
}
#[aoc_generator(day12)]
pub fn input_generator(input: &str) -> Vec<Instruction> {
common::input_vec(input)
}
/* Part One
*
* Your ferry made decent progress toward the island, but the storm came in faster than anyone expected.
* The ferry needs to take evasive actions!
*
* Unfortunately, the ship's navigation computer seems to be malfunctioning;
* rather than giving a route directly to safety, it produced extremely circuitous instructions.
* When the captain uses the PA system to ask if anyone can help, you quickly volunteer.
*
* The navigation instructions (your puzzle input) consists of a sequence of single-character actions
* paired with integer input values. After staring at them for a few minutes, you work out what they probably mean:
*
* Action N means to move north by the given value.
* Action S means to move south by the given value.
* Action E means to move east by the given value.
* Action W means to move west by the given value.
* Action L means to turn left the given number of degrees.
* Action R means to turn right the given number of degrees.
* Action F means to move forward by the given value in the direction the ship is currently facing.
*
* The ship starts by facing east. Only the L and R actions change the direction the ship is facing.
* (That is, if the ship is facing east and the next instruction is N10, the ship would move north 10 units,
* but would still move east if the following action were F.)
*
* For example:
*
* F10
* N3
* F7
* R90
* F11
*
* These instructions would be handled as follows:
*
* F10 would move the ship 10 units east (because the ship starts by facing east) to east 10, north 0.
* N3 would move the ship 3 units north to east 10, north 3.
* F7 would move the ship another 7 units east (because the ship is still facing east) to east 17, north 3.
* R90 would cause the ship to turn right by 90 degrees and face south; it remains at east 17, north 3.
* F11 would move the ship 11 units south to east 17, south 8.
*
* At the end of these instructions, the ship's Manhattan distance (sum of the absolute values of
* its east/west position and its north/south position) from its starting position is 17 + 8 = 25.
*
* Figure out where the navigation instructions lead. What is the Manhattan distance
* between that location and the ship's starting position?
*/
#[aoc(day12, part1)]
pub fn solve_part_01(instructions: &[Instruction]) -> u32 {
let mut ship = Ship::new();
for instruction in instructions {
match instruction {
Instruction::North(x) => ship.north(x),
Instruction::South(x) => ship.south(x),
Instruction::West(x) => ship.west(x),
Instruction::East(x) => ship.east(x),
Instruction::Right(x) => ship.turn(&-x),
Instruction::Left(x) => ship.turn(x),
Instruction::Forward(x) => ship.forward(x),
}
}
math::manhattan_distance(ship.x, ship.y)
}
/* Part Two
*
* Before you can give the destination to the captain, you realize that the
* actual action meanings were printed on the back of the instructions the whole time.
*
* Almost all of the actions indicate how to move a waypoint which is relative to the ship's position:
*
* Action N means to move the waypoint north by the given value.
* Action S means to move the waypoint south by the given value.
* Action E means to move the waypoint east by the given value.
* Action W means to move the waypoint west by the given value.
* Action L means to rotate the waypoint around the ship left (counter-clockwise) the given number of degrees.
* Action R means to rotate the waypoint around the ship right (clockwise) the given number of degrees.
* Action F means to move forward to the waypoint a number of times equal to the given value.
*
* The waypoint starts 10 units east and 1 unit north relative to the ship.
* The waypoint is relative to the ship; that is, if the ship moves, the waypoint moves with it.
*
* For example, using the same instructions as above:
*
* F10 moves the ship to the waypoint 10 times (a total of 100 units east and 10 units north),
* leaving the ship at east 100, north 10. The waypoint stays 10 units east and 1 unit north of the ship.
* N3 moves the waypoint 3 units north to 10 units east and 4 units north of the ship. The ship remains at east 100, north 10.
* F7 moves the ship to the waypoint 7 times (a total of 70 units east and 28 units north),
* leaving the ship at east 170, north 38. The waypoint stays 10 units east and 4 units north of the ship.
* R90 rotates the waypoint around the ship clockwise 90 degrees, moving it to 4 units east and
* 10 units south of the ship. The ship remains at east 170, north 38.
* F11 moves the ship to the waypoint 11 times (a total of 44 units east and 110 units south),
* leaving the ship at east 214, south 72. The waypoint stays 4 units east and 10 units south of the ship.
*
* After these operations, the ship's Manhattan distance from its starting position is 214 + 72 = 286.
*
* Figure out where the navigation instructions actually lead. What is the Manhattan distance between that location and the ship's starting position?
*/
#[aoc(day12, part2)]
pub fn solve_part_02(instructions: &[Instruction]) -> u32 {
let mut waypoint = Waypoint::new(10, 1);
let mut ship = Ship::new();
for instruction in instructions {
match instruction {
Instruction::Forward(x) => ship.move_by_waypoint(&waypoint, x),
Instruction::Right(x) => waypoint.rotate(&-x),
Instruction::Left(x) => waypoint.rotate(x),
Instruction::North(x) => waypoint.north(x),
Instruction::South(x) => waypoint.south(x),
Instruction::East(x) => waypoint.east(x),
Instruction::West(x) => waypoint.west(x),
}
}
math::manhattan_distance(ship.x, ship.y)
}
#[cfg(test)]
mod tests {
use super::*;
// Test example data on part 1
#[test]
fn test_example_part_1() {
let data = "F10
N3
F7
R90
F11";
assert_eq!(solve_part_01(&input_generator(data)), 25)
}
// Test example data on part 2
#[test]
fn test_example_part_2() {
let data = "F10
N3
F7
R90
F11";
assert_eq!(solve_part_02(&input_generator(data)), 286)
}
#[test]
fn create_ship() {
assert_eq!(
Ship::new(),
Ship {
x: 0,
y: 0,
angle: 0
}
)
}
#[test]
fn move_ship_north() {
let mut ship = Ship::new();
ship.north(&1);
assert_eq!(
ship,
Ship {
x: 0,
y: 1,
angle: 0
}
)
}
#[test]
fn move_ship_south() {
let mut ship = Ship::new();
ship.south(&1);
assert_eq!(
ship,
Ship {
x: 0,
y: -1,
angle: 0
}
)
}
#[test]
fn move_ship_east() {
let mut ship = Ship::new();
ship.east(&1);
assert_eq!(
ship,
Ship {
x: 1,
y: 0,
angle: 0
}
)
}
#[test]
fn move_ship_west() {
let mut ship = Ship::new();
ship.west(&1);
assert_eq!(
ship,
Ship {
x: -1,
y: 0,
angle: 0
}
)
}
#[test]
fn turn_ship() {
let mut ship = Ship::new();
ship.turn(&90);
assert_eq!(
ship,
Ship {
x: 0,
y: 0,
angle: 90
}
)
}
#[test]
fn move_ship() {
let mut ship = Ship::new();
ship.forward(&10);
assert_eq!(
ship,
Ship {
x: 10,
y: 0,
angle: 0
}
)
}
#[test]
fn move_ship_by_waypoint() {
let wp = Waypoint::new(10, 4);
let mut ship = Ship::new();
ship.move_by_waypoint(&wp, &10);
assert_eq!(
ship,
Ship {
x: 100,
y: 40,
angle: 0
}
)
}
#[test]
fn create_waypoint() {
assert_eq!(Waypoint::new(10, 1), Waypoint { x: 10, y: 1 })
}
#[test]
fn move_waypoint_north() {
let mut wp = Waypoint::new(0, 0);
wp.north(&1);
assert_eq!(wp, Waypoint { x: 0, y: 1 })
}
#[test]
fn move_waypoint_south() {
let mut wp = Waypoint::new(0, 0);
wp.south(&1);
assert_eq!(wp, Waypoint { x: 0, y: -1 })
}
#[test]
fn move_waypoint_east() {
let mut wp = Waypoint::new(0, 0);
wp.east(&1);
assert_eq!(wp, Waypoint { x: 1, y: 0 })
}
#[test]
fn move_waypoint_west() {
let mut wp = Waypoint::new(0, 0);
wp.west(&1);
assert_eq!(wp, Waypoint { x: -1, y: 0 })
}
#[test]
fn rotate_waypoint() {
let mut wp = Waypoint::new(10, 4);
wp.rotate(&90);
assert_eq!(wp, Waypoint { x: -4, y: 10 })
}
}