10
\$\begingroup\$

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: 
\$\endgroup\$
  • \$\begingroup\$ Cool stuff, that game! Don't really gave time to golf these days but will remember this. \$\endgroup\$ – tomsmeding Jun 13 '13 at 21:02
6
\$\begingroup\$

Score 3:29

Might as well special case dividing by 1,2,3,4. Makes it a lot faster.

enter image description here

http://pleasingfungus.com/Manufactoria/?lvl=34&code=c9:13f2;c10:13f2;c11:13f2;p12:2f7;y13:2f0;p11:2f0;c11:4f3;c10:3f0;p11:5f0;c10:5f0;c9:5f0;c11:6f3;p11:7f0;c10:7f0;p11:3f0;r10:6f1;q8:5f4;q8:6f1;q9:6f1;c7:5f3;c7:6f3;q10:2f0;r10:1f3;r10:4f1;q9:3f4;q9:4f1;c9:2f3;c8:3f3;c8:4f3;r10:8f1;q10:9f6;q9:9f1;q9:8f4;q9:7f4;c8:8f3;c8:9f3;c8:10f3;c8:11f3;c8:12f3;c8:13f2;c7:7f2;c8:7f3;r11:9f3;r11:10f0;r10:10f0;r9:10f3;r9:11f2;p11:11f6;r11:12f1;g11:8f3;b10:11f2;c16:10f2;q17:10f6;q17:11f3;g18:11f0;c12:11f1;c12:10f2;c13:10f2;c14:10f2;c15:10f2;c17:12f0;p16:12f4;c16:11f3;c16:13f1;q15:12f0;r15:13f1;c14:12f3;c14:13f0;c13:13f0;q17:7f6;q17:9f1;q18:9f6;q18:8f5;q17:6f1;g16:6f2;y18:6f0;p17:5f5;r18:5f0;c16:5f0;p15:5f0;r15:6f3;b15:7f2;p16:7f6;r16:8f1;q14:5f0;y14:4f3;g14:6f1;p13:5f0;p13:6f0;p13:7f0;p13:8f0;p13:9f1;g12:9f0;c17:8f1;&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;

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – PhiNotPi Jun 13 '13 at 13:15
  • 1
    \$\begingroup\$ Sure, I like them. \$\endgroup\$ – Keith Randall Jun 13 '13 at 22:45
4
\$\begingroup\$

Score: 15:51

enter image description here

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.

http://pleasingfungus.com/Manufactoria/?lvl=34&code=c11:13f2;g12:2f3;p12:3f7;c13:3f3;p13:4f3;b12:4f2;r14:4f3;p14:7f7;r13:7f2;q14:8f7;g13:8f2;p14:9f4;r13:10f2;p14:10f7;b15:10f0;q14:11f7;p15:11f3;r16:11f1;p15:8f0;r15:9f1;c16:8f0;c13:2f0;c15:2f0;c16:2f0;c17:2f0;c11:3f3;c11:4f3;c11:6f3;c11:7f3;c11:8f3;c11:9f3;c11:5f3;p11:10f7;q11:11f6;q11:12f7;r10:12f2;c10:10f2;q16:10f5;y14:6f3;q14:5f3;g15:5f1;c15:4f1;c15:3f1;c17:9f1;c17:8f1;c17:7f1;c17:6f1;c17:5f1;c17:4f1;c17:3f1;y16:9f1;g17:10f1;q14:2f4;g14:1f3;&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;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.