2020: day12: refacto moving towards waypoint

This commit is contained in:
Antoine Martin 2020-12-12 17:35:59 +01:00
parent fb981e8103
commit 8d34969eaa

View file

@ -22,7 +22,7 @@ fn part1(input: &str) -> aoc::Result<i64> {
let mut ship = Ship::new(); let mut ship = Ship::new();
for a in actions { for a in actions {
ship.process(a); ship.process(&a);
} }
Ok(ship.manhattan_distance()) Ok(ship.manhattan_distance())
@ -37,7 +37,7 @@ fn part2(input: &str) -> aoc::Result<i64> {
let mut ship = Ship::new(); let mut ship = Ship::new();
for a in actions { for a in actions {
ship.process_with_waypoint(a); ship.process_with_waypoint(&a);
} }
Ok(ship.manhattan_distance()) Ok(ship.manhattan_distance())
@ -186,7 +186,7 @@ impl Ship {
self.coordinates.x.abs() + self.coordinates.y.abs() self.coordinates.x.abs() + self.coordinates.y.abs()
} }
fn process(&mut self, action: Action) { fn process(&mut self, action: &Action) {
match action.kind { match action.kind {
ActionKind::Move(dir) => self.coordinates.move_towards(dir, action.arg), ActionKind::Move(dir) => self.coordinates.move_towards(dir, action.arg),
@ -194,18 +194,13 @@ impl Ship {
self.direction = self.direction.rotate(turn_dir, action.arg); self.direction = self.direction.rotate(turn_dir, action.arg);
} }
ActionKind::Forward => self ActionKind::Forward => self.coordinates.move_towards(self.direction, action.arg),
.coordinates
.move_towards(self.direction, action.arg),
} }
} }
fn process_with_waypoint(&mut self, action: Action) { fn process_with_waypoint(&mut self, action: &Action) {
match action.kind { match action.kind {
ActionKind::Move(dir) => self ActionKind::Move(dir) => self.waypoint.coordinates.move_towards(dir, action.arg),
.waypoint
.coordinates
.move_towards(dir, action.arg),
ActionKind::Turn(turn_dir) => { ActionKind::Turn(turn_dir) => {
debug_assert!(action.arg % 90 == 0, "only right angles are supported"); debug_assert!(action.arg % 90 == 0, "only right angles are supported");
@ -216,13 +211,9 @@ impl Ship {
} }
ActionKind::Forward => { ActionKind::Forward => {
let (west_east, north_south) = self.waypoint.as_dirs(); for mv in self.waypoint.as_moves(action.arg).iter() {
let (dx, dy) = self.waypoint.diff(); self.process(mv);
}
self.coordinates
.move_towards(west_east, dx * action.arg);
self.coordinates
.move_towards(north_south, dy * action.arg);
} }
} }
} }
@ -240,7 +231,11 @@ impl Waypoint {
} }
} }
fn as_dirs(&self) -> (Direction, Direction) { /// as_moves returns Actions with ActionKind::Move representing the moves the ship should take
/// to reach the waypoint
///
/// this allows reusing the Forward logic of part 1 to move the ship towards the waypoint
fn as_moves(&self, steps: i64) -> [Action; 2] {
let west_east = if self.coordinates.x < 0 { let west_east = if self.coordinates.x < 0 {
Direction::West Direction::West
} else { } else {
@ -252,14 +247,16 @@ impl Waypoint {
Direction::South Direction::South
}; };
(west_east, north_south) [
} Action {
kind: ActionKind::Move(west_east),
fn diff(&self) -> (i64, i64) { arg: self.coordinates.x.abs() * steps,
( },
self.coordinates.x.abs(), Action {
self.coordinates.y.abs(), kind: ActionKind::Move(north_south),
) arg: self.coordinates.y.abs() * steps,
},
]
} }
fn turn(&mut self, turn_dir: TurnDirection, quadrants: usize) { fn turn(&mut self, turn_dir: TurnDirection, quadrants: usize) {