Skip to content

Instantly share code, notes, and snippets.

@aderchox
Last active August 30, 2022 06:39
Show Gist options
  • Save aderchox/9e8444d29c58a5ffa6bd605b5925e527 to your computer and use it in GitHub Desktop.
Save aderchox/9e8444d29c58a5ffa6bd605b5925e527 to your computer and use it in GitHub Desktop.
karel the robot add fast problem solution
/*
0-0-0-0-1-1-1-1
0-0-1-1-0-0-1-1
0-1-0-1-0-1-0-1
—-1-1-c-1-c-c-1c
*/
void addFast(){
repeat(8){
addColumn();
}
}
void addColumn(){
if(!onBeeper() && !beeperAhead()){
moveForward();
if(beeperAhead()){
/* Uncommenting the two lines below will make the job more beautiful, but will also unfortunately fail the tests */
/* moveForward();
pickBeeper(); */
add1AtTheBottom();
}
moveEnd();
moveToNextColumn();
moveEnd();
turnAround();
}
else if((onBeeper() && !beeperAhead())
|| (!onBeeper() && beeperAhead())){
moveForward();
if(beeperAhead()){
dropCarry();
} else {
add1AtTheBottom();
moveToNextColumn();
}
moveEnd();
turnAround();
} else {
/* Both digits are 1 */
moveForward();
if(beeperAhead()){
/* Uncommenting the two lines below will make the job more beautiful, but will also unfortunately fail the tests */
/* moveForward();
pickBeeper(); */
add1AtTheBottom();
dropCarry();
} else {
dropCarry();
}
moveEnd();
turnAround();
}
}
void moveToNextColumn(){
turnRight();
moveForward();
turnRight();
}
void moveEnd(){
while(frontIsClear()){
moveForward();
}
}
void dropCarry(){
moveEnd();
moveToNextColumn();
moveForward();
dropBeeper();
}
void add1AtTheBottom(){
moveEnd();
dropBeeper();
}
void secureTheCave(){
turnLeft();
repeat(10){
goToEnd();
turnAround();
sweepBeepersToBottom();
situateInTheNextColumn();
}
}
void goToEnd(){
while(frontIsClear()){
moveForward();
}
}
void sweepBeepersToBottom(){
if(!onBeeper()){
goToEnd();
turnAround();
} else {
pickBeeper();
moveForward();
sweepBeepersToBottom();
dropBeeper();
moveForward();
}
}
void situateInTheNextColumn(){
turnRight();
if(frontIsClear()){
moveForward();
turnLeft();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment