# Quickly divide in Manufactoria

## Background

Manufactoria has been marketed as a game, but we code-golfers can see it for what it really is: a two-dimensional programming language. The Manufactoria programming language is based around a single queue, which contains a series of colorful markers. The instruction pointer moves around the game board using conveyor belts, and it encounters a series of writers and branches which read from and write to the queue.

The language is very easy to understand, so the quickest way to learn it is to play the first few levels of the game (linked above).

# Challenge

Your challenge is to create a program that can divide one number by another number in the least amount of time.

The input to the program will be a string of X blue markers followed by Y red markers. The required output will be a string of red markers with a length of X/Y.

The game board to be used is found in this official contest level:

http://pleasingfungus.com/Manufactoria/?ctm=Divide_and_Conquer;Input_will_be_X_blues_followed_by_Y_reds,_output_X/Y_reds;bbbbbbrr:rrr|bbbrrr:r|bbbbr:rrrr|r:|bbbbbbbbbbbbrrrr:rrr|bbbbbbbbbbbbrrr:rrrr|bbbbbbbbbrrr:rrr|bbbbbbbbbbrr:rrrrr;13;3;0

It is 13x13 (the maximum size) and it is pre-equipped with the correct tests (see the scoring section).

# Scoring

The score of your program is the total amount of time that it takes for the program to pass all of the tests in the official contest level. The total time is given on the level-complete screen.

While running the tests, you will most likely have to use the 50x accelerate slider in the bottom left in order to receive the results quickly (time acceleration does not affect the score).

Here is a list of division problems that are involved in the tests:

 6/2 = 3
3/3 = 1
4/1 = 4
0/1 = 0
12/4 = 3
12/3 = 4
9/3 = 3
10/2 = 5


# Example I/O

12/3=4
in:  BBBBBBBBBBBBRRR
out: RRRR

10/2=5
in:  BBBBBBBBBBRR
out: RRRRR

9/3=3
in:  BBBBBBBBBRRR
out: RRR

0/1=0
in:  R
out:

• Cool stuff, that game! Don't really gave time to golf these days but will remember this. – tomsmeding Jun 13 '13 at 21:02

• I should have predicted this, but anyways good job. I wish the game engine allowed me to create more than 8 test cases. Should I create more Manufactoria challenges in the future? – PhiNotPi Jun 13 '13 at 13:15
• Sure, I like them. – Keith Randall Jun 13 '13 at 22:45

## Score: 15:51

Does division by repeated subtraction. Uses a Y amongst the R to keep track of how much divisor we've subtracted so far. Uses Gs to count the quotient.

For instance, the state at the start of each outer loop (right after the initial G writer) for 12/4 is:

BBBBBBBBBBBB RRRR G
BBBBBBBB RRRR GG
BBBB RRRR GGG
RRRR GGGG


When there are no Bs left, the gadget at the bottom left strips Rs and then outputs #G-1 Rs.

The inner loop strips one B at a time and uses Y to keep track of position. Starting at the outer loop:

BBBBBBBB RRRR GG
BBBBBBB RYRRR GG
BBBBBB RRYRR GG
BBBBB RRRYR GG
BBBB RRRR GG


The inner loop is the 3x4 box in the lower right. The layout of the rest can probably be improved a bit, but the inner loop is tight.

• By drastically rearranging the parts of your design, I have been able to reduce the score to 13:28 with 53 parts. – PhiNotPi Jun 12 '13 at 15:30