++++++++ # counter @0 to 8
##################################################
# Initialization
##################################################
[
    >>++++++        # @2 is '.'
    >++++++++++++++ # @3 is 'o'
    >+++++++++++    # @4 is 'X'
    >+              # @5 is '\n'
    >+++++++++      # @6 is 'L'
    >++++++++++     # @7 is 'R'
    <<<<<<<-        # decrement counter @0
]
>>-->->>++>++++>++ # adjusting @2, @3, @4, @5, @6, @7
# @7
>+  # @8 is the direction
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # 50
# grid is 20 by 20
# x_max is 20
# y_max is 20
# init first position to (x: 10, y: 10) so 'y * x_max + x + 1 = 101'
> +++++ +++++
[
    -<+++++ +++++ +++++ +++++ +>
]  # @51
<+ # @50
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  # @9
##################################################
# Description
#   @0, @1 : unused / counter / temporary / flag
#   @2 : value '.'
#   @3 : value 'o'
#   @4 : value 'X'
#   @5 : value '\n'
#   @6 : value 'L'
#   @7 : value 'R'
#   @8 : head's direction (value in [1, 4]) 
#   @9 : input
#   @10 to @14 : unused / counter / temporary / flag
#   @15 : parsed input + 1 (0 mean no input)
#   @16 : need to move ?
#   @50 and more : stack of position
##################################################

##################################################
# Input Loop
##################################################
, # @9 is the input
[
    ##################################################
    # Case number
    ##################################################

    # set the 'move' flag @16 to 1
    >>>>>>>+<<<<<<<

    # 48 <= number <= 57
    # copy @9 to @10 and @11
    [
        ->+>+<<
    ] # @9
    # move back @11 to @9
    >>
    [
        -<<+>>
    ] # @11
    < # @10
    ----------
    ----------
    ----------
    ----------
    -------    # @10 -= 47
    [ # @10 >= 48
        [ # copy @10 to @12 and @13
            ->>+>+<<<
        ]
        >>> # @13
        [ # move @13 to @10
            -<<<+>>>
        ]
        <<< # @10
        ---------- # @10 -= 10

        >+< # @11 is the flag
        [ # @10 >= 58
            >[-]<
            [-]
        ]
        > # @11
        [ # @10 is a number (it's parsed into @12)
            # reset the 'move' flag @16 only if it's the first input (@15)
            >>>> [ - <+<+>> ] # @15 to @13 and @14
            << [ - >> + << ] # @13 to @15
            > # @14
            [
                [-] # reset it
                >> - << # reset @16
            ]
            <<
            [ # move @12 to @15
                ->>>+<<<
            ]
            <
            [-] # reset the flag
        ]
        >[-] # @12 
        <<   # @10
    ] # @10

    ##################################################
    # Move
    ##################################################
    >>>>>> # @16
    [ # need to move
        <
        [ # @15
            -
            # copy @8 to @18 and @19 then move back @19 to @8
            <<<<<<< # @8
            [ ->>>>>>>>>>+>+ <<<<<<<<<<< ]
            >>>>>>>>>>> # @19
            [ -<<<<<<<<<<<+ >>>>>>>>>>> ]
            # @19
            < # @18
            -
            >>+<< # @20 is the flag
            [
                -
                [
                    -
                    [ # if 4
                        [-]
                        <+> # right so '+1'
                        >>[-]<< # reset the flag
                    ]
                    >>
                    [ # if 3
                        <<<+++++ +++++ +++++ +++++>>> # down so '+20'
                        [-] # reset the flag
                    ]
                    <<
                ]
                >>
                [ # if 2
                    <+> # left so '-1'
                    [-] # reset the flag
                ]
                <<
            ]
            >>
            [ # if 1
                <+++++ +++++ +++++ +++++> # up so '-20'
                [-] # reset the flag
            ]
            << # @18

            # @17 is the amount to add for each move
            # @19 is the amount to remove for each move

            <<< # @15
            [ # move 1 tiles in the direction of @17
                # put the result in @20
                >> # @17
                [->+>>+<<<] # @17 to @18 and @20
                >>>[-<<<+>>>] # @20 to @17
                < # @19
                [->+>+<<] # @19 to @20 and @21
                >>[-<<+>>] # @ 21 to @19
                <# @20

                # move back the stack
                >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> # @50
                [>] # back of the stack
                <
                [
                    [
                        ->+<
                    ]
                    <
                ]
                >>
                [- <+<+ >>]
                <<[->>+<<]
                > # @50
                <<<<< <<<<< <<<<< <<<<< <<<<< <<<<< # @20
                # add @18 to @50
                << [- >> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> + <<<<< <<<<< <<<<< <<<<< <<<<< <<<<< <<]
                # remove @20 to @50
                >> [- >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> - <<<<< <<<<< <<<<< <<<<< <<<<< <<<<<]
                <<<<<- # @15
            ]

            # reset flags
            >>[-]> [-]> [-]> [-]> [-] <<<<<<
        ]
        >

        [-]
    ] # @16

    ##################################################
    # Compare to 'L' and 'R'
    ##################################################
    <<<<< <<<<< # @6
    [ # move @6 to @10 and @11
        ->>>>+>+<<<<<
    ] # ptr to @6
    >>>>>
    #move back @11 to @6
    [
        -
        <<<<<+
        >>>>>
    ]
    <<<<< # @6

    >
    [ # move @7 to @11 and @12
        ->>>>+>
        +<<<<<
    ] # ptr to @7
    >>>>>

    #move back @12 to @7
    [
        -
        <<<<<+
        >>>>>
    ]
    <<<<<

    >> # 9
    # increment @10 and @11 to check if exactly 0
    >+>+<<
    # @9
    [ # check if input is L or R
        ->->-<<
    ] # ptr to @9

    ##################################################
    # Case 'L'
    ##################################################
    > # 10 

    [ # if it's L or more
        -
        # put flag @12 to 1
        >>+<<
        [ # if it's more than L
            [-]
            # reset flag @12 to 0
            >>-<<
        ]
        >>
        [ # if it's L
            # @12
            - # stop loop
            <<<< # @8 (direction)
            +
            >>>>
        ] # @12
        [-] # reset @12
        <<
    ] # @10

    > # 11

    ##################################################
    # Case 'R'
    ##################################################
    [ # if it's R or more
        -
        # put flag @12 to 1
        >+<
        [ # if it's more than R
            [-]
            # reset flag @12 to 0
            >-<
        ]
        >
        [ # if it's R
            # @12
            - # stop loop
            <<<< # @8 (direction)
            -
            >>>>
        ] # @12
        [-] # reset @12
        <
    ] # @11

    ##################################################
    # Wrap direction left ( D < 1 ? D = 4 )
    ##################################################
    <<< # @8
    # copy @8 to @13 and @14
    [
        ->>>>>+>+<<<<<<
    ] # @8
    # copy back @14 to @8
    >>>>>>
    [
        -<<<<<<+>>>>>>
    ] # @ 14
    < # @13
    # if @13 is 0 set @8 to 4
    <+> # flag @12 to 1
    [ 
        <[-]>
        [-]
    ] # @13
    < # @12
    [ # if @8 is 0
        <<<<
        [-]++++
        >>>>
        [-]
    ] # @12

    ##################################################
    # Wrap direction right ( D > 4 ? D = 1 )
    ##################################################
    >+++++ # @13 to 5
    <<<<< # @8

    # copy @8 to @14 and remove it from @13
    [
        ->>>>>->+<<<<<<
    ] # @8
    # copy back @14 to @8
    >>>>>>
    [
        -<<<<<<+>>>>>>
    ] # @ 14
    < # @13
    # if @13 is 0 set @8 to 1
    <+> # flag @12 to 1
    [ 
        <[-]>
        [-]
    ] # @13
    < # @12
    [ # if @8 is 5
        <<<<
        [-]+
        >>>>
        [-]
    ] # @12
    <<<
    , # @9
] ######################### End loop

##################################################
# Draw stack
#   @0 : position to check
#   @-1 : current screen position
#   @-2 : copy of @0
#   @-3 : copy of @-1
#   @-4 : unused / temporary
#   @-5 : flag, has found a position
#   @-6 : screen position remaining
#   @-7 : new line counter
#   @-10 : back of the stack (copy of @0)
##################################################

>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>> # @0 (@50)
<[-]+ # set first position
<[-]  # @-2
<[-]  # @-3
<[-]  # @-4
<[-]+ # @-5
<[-]  # @-6
<[-] +++++ +++++ +++++ +++++ # @-7 (every 20 characters)
<[-]  # @-8
<[-]  # @-9
<[-]  # @-10
<[-]  # stop case
>>>>>>>>>>> # @0
<<<<<<<< # @-8
+++++ +++++ +++++ +++++
[ #
    ->>+++++ +++++ +++++ +++++<< # @-6 = 400 number of screen position
]>> # @-6

##################################################
# Start loop
##################################################
[ # while there is something on the stack
    # @-6
    >>>>>> # @0
    # $
    [
        ##################################################
        # Init values  and copy to the back
        ##################################################
        # copy @0 to @-2 and @-10
        [-<<+<<<<<<<<+>>>>>>>>>>]
        # @0
        # copy @-1 to @-3 and @-4
        <[-<<+<+>>>]
        # move back @-4 to @-1
        <<<[->>>+<<<]
        >>>> # @0
        ##################################################
        # Process : Compare and print
        ##################################################

        # substract @-3 from @-2
        <<<-[->-<]
        > # @-2
        # @-3 become unused
        # check if @-2 is 1
        [ 
            -
            <+> # @-3 is the flag
            [
                <->
                [-]
            ]
            # @-2
            < # @-3
            [ # Position correct
                # check flag @-5
                <<
                [ # if not already printed
                    >
                    ++++++++++++++++++++
                    ++++++++++++++++++++
                    ++++++++++++++++++++
                    ++++++++++++++++++++
                    ++++++++++++++++++++
                    +++++++++++ # value of 'o'
                    .[-]
                    <
                    [-]
                ]
                >> # @-3
                [-]
            ]
            >
            [-]
        ]
        +<>>> # @0
        ##################################################
        # Move all back
        ##################################################
        # move @-1 to @0
        <[->+<]
        # reset @-2 and @-3
        <[-]<[-]
        # move @-5 to @-4
        <<[->+<]
        # move @-6 to @-5
        <[->+<]
        # move @-7 to @-6
        <[->+<]
        # reset @-8
        <[-]
        >>>>>>>>

        ##################################################
        # Continue to the next
        ##################################################
        >
    ] # @0

    ##################################################
    # Rebuild stack
    ##################################################

    <<<<<<<<<<< # @-11
    [
        # move @-9 to @-10
        >>[-<+>]
        # move @-8 to @-9
        >[-<+>]
        # move @-7 to @-8
        >[-<+>]
        # move @-6 to @-7
        >[-<+>]
        # move @-5 to @-6
        >[-<+>]
        # move @-4 to @-5
        >[-<+>]
        # move @-3 to @-4
        >[-<+>]
        # move @-2 to @-3
        >[-<+>]
        # move @-1 to @-2
        >[-<+>]
        <<<<<<<<<<
        # move @-11 to @-1
        [->>>>>>>>>>+<<<<<<<<<<]
        <
    ]
    >>>>>>>>>>> # @0

    ##################################################
    # Reset state and continue
    ##################################################

    # increment @-1
    <+
    # reset flag @-5
    <<<<
    [ # if nothing was printed, print '.'
        [-]
        ++++++++++
        ++++++++++
        ++++++++++
        ++++++++++
        ++++++.
        [-]
    ]
    [-]+
    # decrement @-6
    <-
    # check if it's a new line
    <- # @-7
    # copy @-7 to @-8 and @-9
    [-<+<+>>]
    # move back @-8 to @-7
    <[->+<]<
    # @-9
    >[-]+< # @-8 is the flag
    [>-<[-]]
    >
    [
        # reset @-7 to 20 and print the new line
        > +++++ +++++ . +++++ +++++
        <[-] # reset flag
    ]
    >>
]
++++++++++.[-]