diff --git a/2019/11/src/main.rs b/2019/11/src/main.rs index d102327..d072785 100644 --- a/2019/11/src/main.rs +++ b/2019/11/src/main.rs @@ -9,24 +9,6 @@ struct Robot { } impl Robot { - fn turn(&mut self, turn_value: i64) { - self.direction = match turn_value { - 0 => match self.direction { - Direction::Up => Direction::Left, - Direction::Right => Direction::Up, - Direction::Down => Direction::Right, - Direction::Left => Direction::Down, - }, - 1 => match self.direction { - Direction::Up => Direction::Right, - Direction::Right => Direction::Down, - Direction::Down => Direction::Left, - Direction::Left => Direction::Up, - }, - _ => unreachable!("Illegal turn value"), - } - } - fn mov(&mut self) { self.position.mov(&self.direction); } @@ -50,7 +32,7 @@ fn start_with_input(input: Vec, color: i64) -> Robot { while let IntComputerResult::Output(o) = pc.run() { robot.paint(o); let turn_int = pc.run().unwrap(); - robot.turn(turn_int); + robot.direction.turn(turn_int * 2 - 1); robot.mov(); pc.params.push(robot.current_color()); } diff --git a/2019/grid/src/lib.rs b/2019/grid/src/lib.rs index d2722eb..f9224cd 100644 --- a/2019/grid/src/lib.rs +++ b/2019/grid/src/lib.rs @@ -10,6 +10,7 @@ pub struct Position2D { pub y: i64, } +#[derive(Clone, Copy)] pub enum Direction { Up, Down, @@ -53,6 +54,27 @@ pub fn draw_ascii( ) } +impl Direction { + pub fn turn(&mut self, turn_value: i64) { + *self = match turn_value { + -1 => match self { + Direction::Up => Direction::Left, + Direction::Right => Direction::Up, + Direction::Down => Direction::Right, + Direction::Left => Direction::Down, + }, + 1 => match self { + Direction::Up => Direction::Right, + Direction::Right => Direction::Down, + Direction::Down => Direction::Left, + Direction::Left => Direction::Up, + }, + 0 => *self, + n => unreachable!(format!("Illegal turn value: {}", n)), + } + } +} + impl Position2D { pub fn mov(&mut self, dir: &Direction) { *self = *self @@ -90,6 +112,3 @@ impl From<(i64, i64)> for Position2D { } } } - -#[cfg(test)] -mod tests {}