use std::ops::AddAssign;
+use std::cmp::Ordering;
#[derive(Debug, Copy, Clone)]
pub struct Vector3D {
let moons_copy = moons.clone();
for m1 in moons.iter_mut() {
for m2 in &moons_copy {
- m1.velocity.x += if m2.position.x > m1.position.x { 1 } else if m2.position.x < m1.position.x { -1 } else { 0 };
- m1.velocity.y += if m2.position.y > m1.position.y { 1 } else if m2.position.y < m1.position.y { -1 } else { 0 };
- m1.velocity.z += if m2.position.z > m1.position.z { 1 } else if m2.position.z < m1.position.z { -1 } else { 0 };
+ m1.velocity.x += match m2.position.x.cmp(&m1.position.x) { Ordering::Greater => 1, Ordering::Less => -1, Ordering::Equal => 0 };
+ m1.velocity.y += match m2.position.y.cmp(&m1.position.y) { Ordering::Greater => 1, Ordering::Less => -1, Ordering::Equal => 0 };
+ m1.velocity.z += match m2.position.z.cmp(&m1.position.z) { Ordering::Greater => 1, Ordering::Less => -1, Ordering::Equal => 0 };
}
}
use super::intcode;\r
use std::collections::HashSet;\r
\r
+#[derive(PartialEq, Eq, Copy, Clone)]\r
+enum Direction { Up, Left, Down, Right }\r
+\r
+impl Direction {\r
+ fn from_char(c: char) -> Option<Self> {\r
+ match c {\r
+ '^' => Some(Direction::Up),\r
+ '<' => Some(Direction::Left),\r
+ 'v' => Some(Direction::Down),\r
+ '>' => Some(Direction::Right),\r
+ _ => None\r
+ }\r
+ }\r
+}\r
+\r
+#[derive(PartialEq, Eq, Copy, Clone)]\r
+struct MovementCommand { dir: Direction, steps: u32 }\r
+\r
pub struct RobotTrackingSystem {\r
output: Vec<i64>,\r
board: Vec<Vec<char>>,\r
start_position: (i32, i32),\r
- start_dir: char,\r
+ start_dir: Direction,\r
}\r
\r
impl RobotTrackingSystem {\r
output: Vec::new(),\r
board: Vec::<Vec<char>>::new(),\r
start_position: (0, 0),\r
- start_dir: '^',\r
+ start_dir: Direction::Up,\r
}\r
}\r
\r
current_x = 0;\r
} else {\r
let c = (*c as u8) as char;\r
- if let '^' | '<' | 'v' | '>' = c {\r
+ if let Some(dir) = Direction::from_char(c) {\r
self.start_position = (current_x, self.board.len() as i32);\r
- self.start_dir = c;\r
+ self.start_dir = dir\r
}\r
+\r
current_line.push(c);\r
current_x += 1;\r
}\r
visited_locations.insert((x, y));\r
\r
'main: loop {\r
- let positions = [('^', (x, y - 1)), ('<', (x - 1, y)), ('>', (x + 1, y)), ('v', (x, y + 1))];\r
+ let positions = [(Direction::Up, (x, y - 1)), (Direction::Left, (x - 1, y)), (Direction::Right, (x + 1, y)), (Direction::Down, (x, y + 1))];\r
\r
let next_position = positions.iter().find(|(d, _)| *d == dir).unwrap().1;\r
\r