From ae2f4846c192cfc0f52539d3447bf618ebbd04a5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Fri, 20 Dec 2019 08:55:25 +0100 Subject: [PATCH] Cleaning --- src/day12.rs | 7 ++++--- src/day17.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/day12.rs b/src/day12.rs index caf57f6..fbfe5de 100644 --- a/src/day12.rs +++ b/src/day12.rs @@ -1,4 +1,5 @@ use std::ops::AddAssign; +use std::cmp::Ordering; #[derive(Debug, Copy, Clone)] pub struct Vector3D { @@ -35,9 +36,9 @@ fn next_step(moons: &mut Vec) { 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 }; } } diff --git a/src/day17.rs b/src/day17.rs index 8907e78..6998995 100644 --- a/src/day17.rs +++ b/src/day17.rs @@ -1,11 +1,29 @@ use super::intcode; use std::collections::HashSet; +#[derive(PartialEq, Eq, Copy, Clone)] +enum Direction { Up, Left, Down, Right } + +impl Direction { + fn from_char(c: char) -> Option { + match c { + '^' => Some(Direction::Up), + '<' => Some(Direction::Left), + 'v' => Some(Direction::Down), + '>' => Some(Direction::Right), + _ => None + } + } +} + +#[derive(PartialEq, Eq, Copy, Clone)] +struct MovementCommand { dir: Direction, steps: u32 } + pub struct RobotTrackingSystem { output: Vec, board: Vec>, start_position: (i32, i32), - start_dir: char, + start_dir: Direction, } impl RobotTrackingSystem { @@ -14,7 +32,7 @@ impl RobotTrackingSystem { output: Vec::new(), board: Vec::>::new(), start_position: (0, 0), - start_dir: '^', + start_dir: Direction::Up, } } @@ -41,10 +59,11 @@ impl RobotTrackingSystem { current_x = 0; } else { let c = (*c as u8) as char; - if let '^' | '<' | 'v' | '>' = c { + if let Some(dir) = Direction::from_char(c) { self.start_position = (current_x, self.board.len() as i32); - self.start_dir = c; + self.start_dir = dir } + current_line.push(c); current_x += 1; } @@ -80,7 +99,7 @@ pub fn scaffold_intersections(code: &[i64]) -> i32 { visited_locations.insert((x, y)); 'main: loop { - let positions = [('^', (x, y - 1)), ('<', (x - 1, y)), ('>', (x + 1, y)), ('v', (x, y + 1))]; + let positions = [(Direction::Up, (x, y - 1)), (Direction::Left, (x - 1, y)), (Direction::Right, (x + 1, y)), (Direction::Down, (x, y + 1))]; let next_position = positions.iter().find(|(d, _)| *d == dir).unwrap().1; -- 2.45.2