Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
Last active November 27, 2018 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CompSciRocks/fa88930de068f4bfea04ffcefc8f4a23 to your computer and use it in GitHub Desktop.
Save CompSciRocks/fa88930de068f4bfea04ffcefc8f4a23 to your computer and use it in GitHub Desktop.
public int clearHall() {
int cnt = 0;
while (!hallIsClear()) {
move();
cnt++;
}
return cnt;
}
private boolean forwardMoveBlocked() {
if (facingRight && pos >= hall.length - 1)
return true;
else if (!facingRight && pos == 0)
return true;
return false;
}
private void move() {
if (hall[pos] > 0)
hall[pos]--;
if (hall[pos] == 0) {
if (forwardMoveBlocked()) {
facingRight = !facingRight;
}
else if (facingRight) {
pos++;
}
else {
pos--;
}
}
}
public class Robot {
private int[] hall;
private int pos;
private boolean facingRight;
public boolean forwardMoveBlocked() { // part A }
private void move() { // part B }
public int clearHall() { // part C }
private boolean hallIsClear() {
// implementation not shown
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment