The Lost Mind of Dr. Brain/Motor Programming

Motor Programming is the programming puzzle of the game. You make a program for Dr. Brain to follow in on a board where he has to pick up all the brains.

Note that the turn instructions are relative to Dr. Brain's current facing direction and do not move him to another cell on the grid.

Higher difficulties add in subroutines, extra programs that your code can call in order to perform repeated actions. On the highest difficulty Sub 1 can call Sub 2.

Strategy
This programming language has very liberal error handling. The only thing that will end the program is running out of code. Running into walls or enemies just wastes time and trying to pick up a brain that isn't there just makes a less happy sound.

Subroutines are for performing repeated tasks. You can usually see a pattern of movement on the board that is repeated several times. As part of reusing subroutines you may need to "undo" part of the subroutine after calling it in order to reach a more convenient state.