# BeeprBot Unary Subtraction
# This program requires AUXILIARY MODE
# set this in your menu, under
#  - under robot select settings
#  - in the pop-up: 
#  - - click the button for auxiliary mode
#  - -  click the save button

# unary subtraction example, not destroying inputs
# a beeper to the left of the result indicates it is
# negative
define main {
    # get the first input
    move
    get_and_restore
    # we have to keep the two inputs separate
    move_1_to_2

    # get the second input
    move
    get_and_restore

    # output the result
    move
    display_difference
    turn_south
    move
}

# pick up the beepers and put them in 
# bag 1, at the same time recreating
# the pile
define get_and_restore {
    while (next_to_a_beeper) {
        pick_beeper
    }
    while (has_beeper(0)) {
        move_beeper(0,1)
        create_beeper
    }
}

# moves contents of bag 1 to bag 2
define move_1_to_2 {
    while (has_beeper(1)) {
        move_beeper(1,2)
    }
}

# recall the fist value is in bag 2
# and the second is in bag 1
define display_difference {
    while (has_beeper(2)) {
        if (has_beeper(1)) {
            # this part does matching
            # bag 9 is used for trash
            move_beeper(1,9)
            move_beeper(2,9)
        } else {
            # if bag 2 had more, result is
            # positive so put it down
            move_beeper(2,0)
            put_beeper
        }
    }
    # if bag 2 became empty before bag 1
    # we have a negative result
    if (has_beeper(1)) {
        turn_west
        move
        # this beeper is the negative sign
        create_beeper
        turn_east
        move
    }
    # put down beepers in bag 1
    # note: if result is postiive
    # this loop does nothing.
    while (has_beeper(1)) {
        move_beeper(1,0)
        put_beeper
    }
}

# turn functions from library
define turn_north {
    while (not_facing_north) {
        turn_left
    }
}

define turn_west {
    while (not_facing_west) {
        turn_left
    }
}

define turn_east {
    while (not_facing_east) {
        turn_left
    }
}

define turn_south {
    while (not_facing_south) {
        turn_left
    }
}

