class program {

define faceWest()
   {
       while(notFacingWest)
          turnleft();
   }

define turnRight()
   {
      iterate(3)
         turnleft();
   }



define pickRow()
   {
      if(nextToABeeper)
         {
            pickbeeper();
            if(nextToABeeper)
               {
                  putbeeper();
                  if(frontIsClear)
                     {
                        move();
                        pickRow();
                     }
                  else
                     {
                        turnleft();
                        turnleft();
                        while(frontIsClear)
                           move();
                        turnRight();
                        move();
                        turnRight();
                        move();
                     }
               }
            else
               {
                  if(frontIsClear)
                     {
                        move();
                        pickRow();
                     }
                  else
                     {
                        turnleft();
                        turnleft();
                        while(frontIsClear)
                           move();
                        turnRight();
                        move();
                        turnRight();
                        move();
                     }
                  putbeeper();
               }
         }
      else
         {
                  if(frontIsClear)
                     {
                        move();
                        pickRow();
                     }
                  else
                     {
                        turnleft();
                        turnleft();
                        while(frontIsClear)
                           move();
                        turnRight();
                        move();
                        turnRight();
                        move();
                     }
         }


   }


define decreseInOne()
   {
      if(nextToABeeper)
         pickbeeper();
      while(frontIsClear)
         {
            move();
            if(nextToABeeper)
               pickbeeper();
         }
      faceWest();
      while(frontIsClear)
         move();
      turnleft();
      turnleft();
   }
define check()
   {
      turnleft();
      move();
      turnRight();
      move();
      while(nextToABeeper)
         pickbeeper();
      turnleft();
      turnleft();
      move();
      putbeeper();
      turnleft();
      move();
      turnleft();
   }

define checkEquals()
   {
      if(nextToABeeper)
         {
            pickbeeper();
            move();
            if(notNextToABeeper)
               {
                  turnleft();
                  turnleft();
                  move();
                  putbeeper();
                  turnleft();
                  turnleft();
               }
            else
               {
                  pickbeeper();
                  turnleft();
                  turnleft();
                  checkEquals();
                  putbeeper();
                  move();
                  putbeeper();
                  turnleft();
                  turnleft();
               }
         }
      else
         {
            move();
            if(notNextToABeeper)
               turnoff();
            else
               {
                  turnleft();
                  turnleft();
                  move();
                  turnleft();
                  turnleft();
               }

         }
   }

program() {
    while(anyBeepersInBeeperBag)
       {
          check();
          pickRow();
          faceWest();
          checkEquals();
          faceWest();
          while(frontIsClear)
             move();
          turnleft();
          move();
          turnleft();
          decreseInOne();
       }
    turnoff();
}

}
