# Mod 7 in Manufactoria

A simple Manufactoria challenge. Compute the input modulo 7. The input will be in big-endian binary (blue=1, red=0). The output should be in the same format.

Test cases provided. The smallest part count wins.

(if the input mod 7 is 0, output nothing.)

• "code-golf" in this case means "fewest parts"? – John Dvorak Jan 21 '14 at 6:02
• Since I haven't even solved the incrementation problem, I have no idea how to solve this. Manufactoria is fun. – Justin Jan 21 '14 at 6:15
• @JanDvorak: yes. – Keith Randall Jan 21 '14 at 7:02
• @KeithRandall We never tagged code-golf with manufactoria. We should either remove the tag here or add it to the other questions. – Howard Jan 21 '14 at 8:26
• @Howard: I'd say add it (or fastest-code or busy-beaver or code-challenge or whatever best describes the scoring), and let manufactoria be a simple language tag. – Ilmari Karonen Jan 28 '14 at 14:15

The algorithm is quite straightforward: Compute the modulus using a state machine (the largest part with eight branches - one of the states is duplicated for logistical purposes), then encode and collect the results. Since almost every result contains a one digit, an extra compression step is employed to reduce the amount of parts.

Designed in yEd, then transcribed to Manufactoria.

Uses way too many conveyer belts, in my opinion.

# 58 43 parts

Keith Randall's idea of first converting the input to unary was pretty good, so I stole it. ;-) Conveniently, I'd just spent some time optimizing small binary-to-unary converters in Manufactoria, so just picked one of my almost-working solutions* from that challenge and combined it with a quickly optimized mod-7 counter.

This design is now at the point where just getting the robots from the top to the bottom is starting to require otherwise useless extra conveyors. Any significant further parts reductions will probably come from redesigning the layout to be taller and narrower.

(* That challenge required a) the design to fit onto a 7×7 board, and b) the unary output to be in red markers. If you look at the binary-to-unary converter part of the machine above, you'll note that, with one or two extra parts, it can easily satisfy either requirement, but alas, not both.)

Here's the previous 58-part version:

Like Jan Dvorak's solution, this is also based on a 7-state FSM. I've labeled the gates corresponding to each state in the screenshot to make it easier to read. The state machine itself, however, is really the easy part; the tricky part is generating the final output with a minimal number of gates.

One trick I found useful was the final copy loop that barrel-shifts everything written before the yellow marker to the end (while also stripping off the green marker): this allowed me to make use of the repetition in the high-order output bits by generating the outputs as:

0:  Y   ->
1: BY   ->   B
2:  YBR ->  BR
3:  YBB ->  BB
4: RYBR -> BRR
5: BYBR -> BRB
6: RYBB -> BBR


This lets me mostly combine the output paths for outputs 2, 4 and 5 (which all begin with BR) and 3 and 6 (which begin with BB).

• I have found no flaw in your design. Good job on saving space. – John Dvorak Feb 1 '14 at 18:24
• Ps. Here's a nice test for state-machine based implementations like this: the number 8890 = BRRRBRBRBBBRBR should give the output 0, visiting every state twice (and state 0 once more at the end) and taking every possible state transition once. – Ilmari Karonen Feb 1 '14 at 21:43

## 60 parts

My solution is quite a bit different. It converts binary to unary first, then does the mod 7. I can't quite beat Ilmari.

I actually had no idea what I'm doing but it works and I might be the winner (if only the test-cases would be proof enough). :D

EDIT: Optimized it 2 times, a bit smaller now.(Removed rubbish)

• Actually I don't know if it'll be working with bigger numbers (or different one than the test cases). – Leo Pflug Jan 21 '14 at 10:17
• -1 only works for the test cases. If the number starts with 111, it will always be reported as being divisible by 7. This is simply not true. – John Dvorak Jan 21 '14 at 10:30