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
|
use advent_lib::prelude::*;
//use std::convert::TryFrom;
#[derive(Debug)]
struct ShipState {
x_offset: i64, // positive is east
y_offset: i64, // positive is north
heading: i64, // 0 is east; 90 is north
}
#[derive(Debug)]
struct NavState {
waypoint_x_offset: i64,
waypoint_y_offset: i64,
ship_x_offset: i64,
ship_y_offset: i64,
}
impl ShipState {
fn new() -> ShipState {
ShipState {
x_offset: 0,
y_offset: 0,
heading: 0,
}
}
}
impl NavState {
fn new() -> NavState {
NavState {
waypoint_x_offset: 10,
waypoint_y_offset: 1,
ship_x_offset: 0,
ship_y_offset: 0,
}
}
}
fn main() -> Result<()> {
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("Usage: advent input");
}
let _ = args.next();
let filename = args.next().unwrap();
let input = advent_lib::read_lines_file(&filename)?;
let mut ship = ShipState::new();
let mut nav = NavState::new();
for line in &input {
let (command, parameter_str) = line.split_at(1);
let parameter = parameter_str.parse::<i64>()?;
match command {
"N" => {
ship.y_offset += parameter;
nav.waypoint_y_offset += parameter;
},
"S" => {
ship.y_offset -= parameter;
nav.waypoint_y_offset -= parameter;
},
"E" => {
ship.x_offset += parameter;
nav.waypoint_x_offset += parameter;
},
"W" => {
ship.x_offset -= parameter;
nav.waypoint_x_offset -= parameter;
},
"L" => {
ship.heading = (ship.heading + parameter) % 360;
rotate_waypoint(&mut nav, parameter);
},
"R" => {
ship.heading = (ship.heading - parameter + 360) % 360;
rotate_waypoint(&mut nav, -parameter);
},
"F" => {
while ship.heading < 0 {
ship.heading += 360;
}
ship.heading = ship.heading % 360;
if ship.heading == 0 {
ship.x_offset += parameter;
} else if ship.heading == 90 {
ship.y_offset += parameter;
} else if ship.heading == 180 {
ship.x_offset -= parameter;
} else if ship.heading == 270 {
ship.y_offset -= parameter;
}
nav.ship_x_offset += parameter * nav.waypoint_x_offset;
nav.ship_y_offset += parameter * nav.waypoint_y_offset;
},
_ => { },
}
}
let ship_distance = ship.x_offset.abs() + ship.y_offset.abs();
let nav_distance = nav.ship_x_offset.abs() + nav.ship_y_offset.abs();
println!("{}", ship_distance);
println!("{}", nav_distance);
Ok(())
}
fn rotate_waypoint(nav: &mut NavState, parameter: i64) {
let mut rotation = parameter;
while rotation < 0 {
rotation += 360;
}
rotation = rotation % 360;
if rotation == 90 {
// counterclockwise
let x_tmp = nav.waypoint_x_offset;
let y_tmp = nav.waypoint_y_offset;
nav.waypoint_y_offset = x_tmp;
nav.waypoint_x_offset = -y_tmp;
} else if rotation == 270 {
// clockwise
let x_tmp = nav.waypoint_x_offset;
let y_tmp = nav.waypoint_y_offset;
nav.waypoint_y_offset = -x_tmp;
nav.waypoint_x_offset = y_tmp;
} else if rotation == 180 {
let x_tmp = nav.waypoint_x_offset;
let y_tmp = nav.waypoint_y_offset;
nav.waypoint_x_offset = -x_tmp;
nav.waypoint_y_offset = -y_tmp;
}
}
|