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cvv<^^^^^^>
>^>v>^^<^^>cv<^^^^^^>
>^>v>^^<^^>vc<^^^^^^>
>^>v>^^cvv<<^<^^>^>^^^^^>
>^>v>^cv<<^<^^>^>^^^^^>
>^>v>c<<^<^^>^>^^^^^>
c>v<cvc>v<cvcvc^>vv<cvc>vv<^cvc
>^^<^>^^<^>^<^>^cv><^
>^^<^>^^<^>^<^>c^v><^
>^^<^>^^<^>^cv<vvv>v<<^^^^^^>>^
>^^<^>^^<^>c<vvv>v<<^^^^^^>>^
>^^<^>^^c^<vvv>v<<^^^^^^>>^
>^^<^>^c^^<vvv>v<<^^^^^^>>^
>^^<^>cv<<^^^^^^>>^
>^^c<<^^^^^^>>^
>^cv<<<^<<<^>>>>^^^^^^>>^
>c<<<^<<<^>>>>^^^^^^>>^
>^^<^>^^<^>^>^^c<>
>^^<^>^^<^>^>^c^<>
>^^<^>^^<^>^>c^^<>
>^^<^>^^<^>>c^^^
>^^<^>^^>cvv<^^^^^^>
>^>^^^cv<^^^^^^>
>^>^^c<^^^^^^>
>^>^cvv<<^<^^>^>^^^^^>
>^>cv<<^<^^>^>^^^^^>
>^>vc<<^<^^>^>^^^^^>
cvc<<vvvvvv>>^<^^^>^cvcvcvc^^<vvv>cvc<<<<<<v>>>v>>>^cvc
<<<^<<<^>>>>^^^^^^>>>^c<>
<<<^<<<^>>>>^^^^^^>>>c^<>
<<<^<<<^>>>>^^^^^^>>>vc^^<>
<<<^<<<^>>>>>>^<^^^>v>^c^^^
<<<^<<<^>>>>>>^<^^^>v>cvv<^^^^^^>
<<<^<<<^>>>>>>^>^cv<^^^^^^>
<<<^<<<^>>>>>>^>c<^^^^^^>
<<<^<<<^>>>>>>^>vcvv<<^<^^>^>^^^^^>
^>cv<<^<^^>^>^^^^^>
>c<<^<^^>^>^^^^^>
cvcvcvc^^^<vvvvvv>^^cvcvc<^<v<vv>v>>^^cvcvc
This may come as a surprise, but I didn't write this by hand... the code was generated by the following Mathematica program:
layouts = Graph /@ {Labeled[DirectedEdge[#, #2], #3] & @@@ {{0, 1, ">"}, ... };
path[layout_, a_, b_] :=
StringJoin[
PropertyValue[{layouts[[layout + 1]], #}, EdgeLabels] & /@
DirectedEdge @@@
Partition[FindShortestPath[layouts[[layout + 1]], a, b], 2, 1]]
safetyCheck[layout_, target_] = "";
safetyCheck[0, 1] = safetyCheck[0, 11] = "v<>^";
safetyCheck[0, 2] = "v^";
safetyCheck[0, 3] = safetyCheck[0, 13] = "<>";
safetyCheck[0, 4] = "<<>>";
safetyCheck[0, 5] = "v^";
safetyCheck[0, 6] = "<v^>";
safetyCheck[0, 7] = "><";
safetyCheck[0, 8] = safetyCheck[0, 18] = "<>";
safetyCheck[0, 9] = "v^";
safetyCheck[1, 2] = "v^";
safetyCheck[1, 3] = safetyCheck[1, 13] = safetyCheck[1, 23] = "<<>>";
safetyCheck[1, 4] = "<v<>^>";
safetyCheck[1, 5] = "v^";
safetyCheck[1, 6] = "<v^>";
safetyCheck[1, 7] = "<v^>";
safetyCheck[1, 8] = "v^";
safetyCheck[1, 9] = safetyCheck[1, 19] = "<v^>";
safetyCheck[2, 3] = safetyCheck[2, 13] = "<>";
safetyCheck[2, 4] = "<<>>";
safetyCheck[2, 5] = safetyCheck[2, 15] = "v<>^";
safetyCheck[2, 6] = safetyCheck[2, 16] = "<<<<>>>>";
safetyCheck[2, 7] = "><";
safetyCheck[2, 8] = safetyCheck[2, 18] = "<>";
safetyCheck[2, 9] = safetyCheck[2, 19] = safetyCheck[2, 29] = "<>";
safetyCheck[3, 4] = "<>";
safetyCheck[3, 5] = "v^";
safetyCheck[3, 6] = ">><<";
safetyCheck[3, 7] = safetyCheck[3, 17] = "<<>>";
safetyCheck[3, 8] = safetyCheck[3, 18] = "v><^";
safetyCheck[3, 9] = safetyCheck[3, 19] = safetyCheck[3, 29] = "vvv^^^";
safetyCheck[4, 5] = safetyCheck[4, 15] = "<>";
safetyCheck[4, 6] = safetyCheck[4, 16] = "<<>>";
safetyCheck[4, 7] = ">v^<";
safetyCheck[4, 8] = "v^";
safetyCheck[4, 9] = safetyCheck[4, 19] = safetyCheck[4, 29] = "<>";
safetyCheck[5, 6] = "<>";
safetyCheck[5, 7] = "><";
safetyCheck[5, 8] = "<>";
safetyCheck[5, 9] = safetyCheck[5, 19] = "<<>>";
safetyCheck[6, 7] = "><";
safetyCheck[6, 8] = safetyCheck[6, 18] = "<>";
safetyCheck[6, 9] = "v^";
safetyCheck[7, 8] = safetyCheck[7, 18] = "v><^";
safetyCheck[7, 9] = safetyCheck[7, 19] = safetyCheck[7, 29] = "<>";
safetyCheck[8, 9] = safetyCheck[8, 19] = safetyCheck[8, 29] = "<>";
minions = {};
For[i = 0, i < 10, ++i,
collector = "c";
For[j = i, j < 90, j += 10,
collector = collector <> path[i, j, j + 10] <> "c"
];
AppendTo[minions, collector];
For[newI = i + 1, newI < 10, ++newI,
For[k = 0, k < 10, ++k,
AppendTo[minions,
path[i, j, 10 k + newI] <> "c" <> path[newI, 10 k + newI, newI] <>
safetyCheck[i, 10 k + newI]]
]
]
];
StringRiffle[minions, "\n"]
I actually wrote all those safetyCheck
lines by hand. But the first line of that Mathematica code is actually about 28,000 characters long and was itself generated by the following CJam code:
'{o
q~]{-1:W;
2b200Te[W%2/{W):W;~\{
"{"W+","W)++",\">\"}"+
"{"W)+","W++",\"<\"}"+
@
}*{
"{"W+","WA+++",\"v\"}"+
"{"WA++","W++",\"^\"}"+
}*}%", "*"Labeled[DirectedEdge[#,#2],#3]&@@@{ }"S/\*
]o',oNoNo}/'}
(Which takes as input the 10 layouts hardcoded into the interpreter. You can run the code online.)
Code-generation-ception!
Explanation
For a start, have a look at this CJam script to see what the mazes look like.
My solution is based on one important observation: as long as we pick up items along a single column, we won't change between layouts, regardless of whether the cells are filled or not. In particular, as long as we move along the left-most column we'll remain in layout 0
. As long as we move along the next column, we'll remain in layout 1
.
The tricky bit is how to ensure that we've changed between layouts, because we don't know which cells in column 1
have items on them (if any!).
So here's the algorithm (starting on cell 0
in layout 0
):
- Collect all items along the current column, ending up on the bottom row. This minion will never die.
Now for each cell to the right of the current column (trying them in column-major order), try moving there in the current layout, pick up an item there, then move to the top row in that new column using the new layout.
If the attempted cell contained an item, the layout will have changed and we will successfully reach the new column and layout. Because the new (safe) position is at the top row, but all the attempts of finding the next column include 10 net upward moves, all other attempts will fail, so we can ignore them.
If the attempted cell did not contain an item, in most cases, the minion will die during the attempt to reach the top row using the wrong layout, hence discarding this attempt. However, this is not always the case. For instance, the attempted cell might already be on the top row, so no moves were made on the new layout. Likewise, in some cases, the path from the attempted cell to the top row is short enough to be valid on both layouts. I've collected all the cases where this is an issue by hand, and determined a set of moves which is only valid on the new layout (but which moves the minion back to the target cell, so it's effectively a no-op on the new layout). After each attempt where this can be an issue, I perform this set of moves to kill off any minions which didn't collect an item on the attempted cell.
We've now successfully moved to the top of the next column which contains at least one item. Go back to step 1.
You may notice that the structure of solution is as follows:
Line with 10 "c"s
90 lines with 1 "c"
Line with 10 "c"s
80 lines with 1 "c"
Line with 10 "c"s
70 lines with 1 "c"
Line with 10 "c"s
60 lines with 1 "c"
...
Line with 10 "c"s
10 lines with 1 "c"
Line with 10 "c"s
As for the Mathematica code, the safetyCheck
strings are those hand-picked moves which ensure that we've reached the new layout. The first parameter to the lookup is the layout we're starting from and the second one is the cell we've attempted. Any combinations which aren't mentioned explicitly just give an empty safety check (because none is necessary).
In addition to that, I'm simply setting up the 10 mazes as Graph
objects, where there are two directed edges between any adjacent (and connected) cells, where each edge is annotated with move required to traverse the edge. With that in place, I can simply find the paths using FindShortestPath
and then extract the corresponding edge labels with PropertyValue[..., EdgeLabels]
.
The rest of the code just makes use of that to implement the above algorithm fairly directly.
The actual graph data is stored in layouts
and was generated with the CJam script, which decodes the numbers as described in the cop post and turns them into a Mathematica list, which can easily be transformed into a graph.
1It's funny that you used each of the five registers in exactly the same way that I did. – Liam – 2015-10-28T20:00:19.017