Winner srach (tweaky bird and pushy cat 21)
The Wiring Contest is the 17th MATLAB Online Programming Contest.
This contest is inspired by the problem of wiring printed circuit boards. It was suggested to us by a contest participant, Edward Meyer. This is our first contest theme based on community input, and we hope it will be the first of many. We've already sent Edward a MATLAB baseball cap to thank him for the idea. Send us your ideas for future contests to contest@mathworks.com.
Here is the challenge. Given a board with numbered pins, your job is to connect each pin to all other pins with the same number. Thus in the diagram above, the pins labeled 11 should be connected to each other. Similarly, the pins labeled 8 and 3 should be connected to their respective partners. Every time you connect a group of pins, your score improves (is reduced by) the total value of the pins connected.
To connect these pins, imagine that you have a box filled with unit-length connector wires. You can use as many as you like, but each one also has an associated cost.
Five connectors were required to link these pins. At the same time, we reduced the total score by 8 + 8 or 16 points. The net value of this connection is 5 - (8 + 8), or -11. Since a lower score is better, that's moving us in the right direction.
The total cost for this connection is 10. Notice that it's okay for the connecting wires to pass across one of the pins. The net value of this wire is 10 - (3 + 3 + 3), or 1. The fact that the result is positive indicates this isn't a wise move.
This is a legal move, but it has a bad result on the score. By shorting the 3 circuit and the 11 circuit together, we lose the credit you gained by connecting them. So we would be paying for the connectors, but they wouldn't be doing us any good. A better idea is to use a "bridge". The bridge is indicated below by the red arrow.
Bridges are expensive: they cost 25 points each. In this diagram we've saved 22 points by connecting the two 11 pins, but we had to buy three connectors and a bridge, for a total cost of 28. The net score for this move is 6, making it a really bad move.
It's not hard to see that there's a much better way to connect up this particular board. This bad solution just served as an introductory example.
W = solver(B)The board B is a matrix. You return the wiring list, which is an n-by-4 matrix with as many rows as you have connectors. Each row has the following form
[ r1 c1 r2 c2 ]Each line segment is one unit in length. There is no directionality to the lines, so it doesn't matter which end comes first.
The example shown above would have the following B (for board) matrix. Note that dots are being used instead of zeros for greater clarity.
B = [ . . . . . . ]
[ . . 8 . 3 . ]
[ . . 11 . . . ]
[ . 3 . . . 8 ]
[ . . . 3 . . ]
[ . . 11 . . . ]
The segments for the connector between the 8 pins could be written like so.
w = [ 2 3 2 4 ]
[ 2 4 3 4 ]
[ 3 4 3 5 ]
[ 3 5 4 5 ]
[ 4 5 4 6 ]
Note that a great many legal and equivalent permutations of this instruction list exist. The order of the segments, for example, is unimportant.
To indicate a bridge, you need to lay down all the segments as above. Then in addition you enter a line like so at the point where the bridge is needed.
[ r1 c1 r1 c1 ]Bridges only support perpendicular crossovers. The segment between the two 11 pins could be written like this.
w = [ 3 3 4 3 ]
[ 4 3 5 3 ]
[ 5 3 6 3 ]
[ 5 3 5 3 ]
[ . . . . ]
[ 3 --- 3 4 ]
[ | . . | ]
[ 8 . . 4 ]
In the next diagram, there are five 3s. To receive full credit, they must all be connected. But in fact, there are two separate "islands". How is this scored? The answer is that we give you credit for the largest island only. The score for this board is 3 + 3 for the pins in the small island and 1 + 1 + 1 for the connectors, for a total of 9. The obvious lesson is that it's a waste of time and material to connect isolated islands of pins.
[ . . . . . ]
[ 3 --- 3 . 3 ]
[ | . . . | ]
[ 3 . . . 3 ]
[ . . . . . ]
If you short-circuit any of the pins of a given number, you get no credit for any of those pins. So the board below would give no bonus.
[ . . . . . ]
[ 3 --- 3 . 3 ]
[ | . . . | ]
[ 3 . 5 --- 3 ]
[ . . . . . ]
Since a file can contain multiple functions, the complexity for any given file is defined as the MAXIMUM complexity of any functions contained in it. A good practice is to keep the complexity for each function below 10, so for this contest your overall score will increase according to the complexity in excess of 10. So there is no complexity penalty for submissions in which all functions have a complexity of 10 or less.
You can measure the cyclomatic (or McCabe) complexity of any function in MATLAB using the "cyc" switch for mlint. Try this, for example:
>> mlint -cyc magic.mWe have also included a getComplexity.m function with the contest distribution to make it easier to find the complexity for your entry.
t = mtree('solver.m','-file');
length(t.nodesize)
The node count penalty is designed to have a secondary impact on the score. Its multiplying constant will be low. It has been added to the scoring function only to motivate the removal of stale code.
The code is limited in size by the database architecture. The column in our MySQL database that stores the M-code is of type text, which is limited of 65535 characters. Submissions longer than this limit will fail.
>> runcontest
We also encourage you to discuss your solutions and strategies with others. You can do this by posting to the newsgroup thread that we've started from our newsreader.
The following are prohibited:
Extraction of puzzles in the test suite by manipulating the score, runtime, or error conditions is also forbidden. In the small scale, this has been an element of many past contests, but in the Blockbuster Contest, Alan Chalker turned this into a science. Tuning the entry to the contest test suite via tweak bombing or other techniques is still allowed, but we ask that you not overwhelm the queue.
Contests are divided into segments where some or all of the scores and code may be hidden for some users. Here are the segments for this contest: