Instantly share code, notes, and snippets.

Embed
What would you like to do?
A rust implementation of a simple exercise
fn main() {
println!("{:?}", robot_paths(6, &mut Board::new(6), None, None));
}
#[derive(Clone)]
struct Board {
squares: Vec<Vec<bool>>,
}
impl Board {
fn new(n: i32) -> Board {
let mut squares = Vec::new();
for i in 0..n {
squares.push(Vec::new());
for _ in 0..n {
squares[i as usize].push(false);
}
}
Board { squares }
}
fn toggle_piece(&mut self, i: i32, j: i32) {
let i = i as usize;
let j = j as usize;
self.squares[i][j] = !self.squares[i][j];
}
fn has_been_visited(&mut self, i: i32, j: i32) -> bool {
let i = i as usize;
let j = j as usize;
self.squares[i][j]
}
}
fn robot_paths(n: i32, board: &mut Board, i: Option<i32>, j: Option<i32>) -> i32 {
let i = i.unwrap_or(0);
let j = j.unwrap_or(0);
if !(i >= 0 && i < n && j >= 0 && j < n) || board.has_been_visited(i, j) {
return 0;
}
if i == n - 1 && j == n - 1 {
return 1;
}
board.toggle_piece(i, j);
let result = robot_paths(n, board, Some(i), Some(j + 1))
+ robot_paths(n, board, Some(i), Some(j - 1))
+ robot_paths(n, board, Some(i + 1), Some(j))
+ robot_paths(n, board, Some(i - 1), Some(j));
// Return the board to its original state
board.toggle_piece(i, j);
result
}
@ErichDonGubler

This comment has been minimized.

Copy link

ErichDonGubler commented Jan 8, 2019

You're doing a LOT of cloning that seems unnecessary here. Let's fix that:

--- old.rs      2019-01-08 13:26:59.201134000 -0700
+++ new.rs      2019-01-08 13:26:44.644846000 -0700
@@ -34,8 +34,12 @@
     }
 }

-fn robot_paths(n: i32, board: Option<Board>, i: Option<i32>, j: Option<i32>) -> i32 {
-    let mut board = board.unwrap_or(Board::new(n));
+fn robot_paths(n: i32, board: Option<&mut Board>, i: Option<i32>, j: Option<i32>) -> i32 {
+    let board = match board {
+        None => return robot_paths(n, Some(&mut Board::new(n)), i, j),
+        Some(b) => b,
+    };
+
     let i = i.unwrap_or(0);
     let j = j.unwrap_or(0);
     if !(i >= 0 && i < n && j >= 0 && j < n) || board.has_been_visited(i, j) {
@@ -45,10 +49,10 @@
         return 1;
     }
     board.toggle_piece(i, j);
-    let result = robot_paths(n, Some(board.clone()), Some(i), Some(j + 1))
-        + robot_paths(n, Some(board.clone()), Some(i), Some(j - 1))
-        + robot_paths(n, Some(board.clone()), Some(i + 1), Some(j))
-        + robot_paths(n, Some(board.clone()), Some(i - 1), Some(j));
+    let result = robot_paths(n, Some(board), Some(i), Some(j + 1))
+        + robot_paths(n, Some(board), Some(i), Some(j - 1))
+        + robot_paths(n, Some(board), Some(i + 1), Some(j))
+        + robot_paths(n, Some(board), Some(i - 1), Some(j));
     // Return the board to its original state
     board.toggle_piece(i, j);
     result
@josalhor

This comment has been minimized.

Copy link

josalhor commented Jan 8, 2019

i and j being Options seems completly unnecessary, I believe that could be slowing it down.

@BartMassey

This comment has been minimized.

Copy link

BartMassey commented Jan 9, 2019

Some dumb little things:

  • Board no longer needs to derive Clone. In general, don't unless you need it: it will ensure that you don't put expensive clones in without thinking about it.

  • has_been_visited() can borrow self immutably.

  • If you make the inner robot_paths function a method of Board it will clean up the code a bit.

@ThomasdenH

This comment has been minimized.

Copy link

ThomasdenH commented Jan 10, 2019

@BartMassey

In general, don't unless you need it: it will ensure that you don't put expensive clones in without thinking about it.

I'm really not sure about this. The API guidelines recommend the opposite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment