26
This challenge is loosely inspired by the Zachtronics game Infinifactory.
You are given a top-down view of a rectangular grid of conveyors, represented by >v<^
. There may be cells without conveyors, represented by spaces. Here is an example:
> <vv <
v ^ >v v
>v^^>vv^
^>^ v
> v<v >>
>v v<^
This setup is implicitly surrounded by an infinite number of spaces.
Furthermore, you are given the dimensions of a rectangular piece of cargo which is placed onto the conveyors in the top left corner of the grid. Your task is to figure out whether the cargo ever comes to rest or whether it will end up moving in a loop.
Of course, the cargo is likely to cover several conveyors at once, so here are the rules for figuring out the direction of the cargo in each step:
Opposite conveyors cancel each other. So if a 3x2 cargo covers any of the following patches (outlined with hyphens and pipes for clarity), the result would be the same:
+---+ +---+ +---+ |>>^| | ^| |v^^| |^<<| |^ | |^^v| +---+ +---+ +---+
The same goes for these:
+---+ +---+ +---+ |v^<| | | |><>| |>>>| |>> | |>><| +---+ +---+ +---+
Since the exact position of a conveyor underneath the cargo is irrelevant, it doesn't matter which pairs you cancel.
This cancellation is applied before the other rules. Therefore, for the other rules there will only be conveyors in at most two directions.
- If the cargo doesn't cover any conveyors at all (either because all conveyors cancel, because it covers only spaces or because it moved fully off the grid), it comes to rest.
If the cargo covers more conveyors of one direction than of the other, the cargo moves in that direction. E.g., if a 3x2 cargo covered the following patch
>> ^>^
it would move to the right, because there are more
>
than^
. On the other hand, if it covered>>^ ^
this rule would not apply, because there's a tie between
>
and^
.This leaves only cases in which there is a tie between adjacent directions (a tie between opposite directions would have cancelled). In this case, the cargo keeps moving along the axis it is already moving in. E.g. if a right-moving or left-moving 3x2 cargo is now covering the patch
>>^ ^
it would move to the right. If it had arrived on this patch moving up or down, it would now move up instead. If this kind of conflict occurs on the very first step of the simulation, assume that the cargo had been moving to the right.
Detailed Examples
Consider the conveyor grid at the top and a 3x2 cargo. The following is a step-by-step visualisation of the process. Each step consists of the grid, with the cargo represented by #
, a small box which shows the conveyors covered by the cargo, another box with the conveyors after cancellation, and the rule that determines where the cargo moves:
###vv < > <vv < > <vv < > <vv < > <vv < > <vv <
###^ >v v ###^ >v v v ^ >v v v ^ >v v v ^ >v v v ^ >v v
>v^^>vv^ ###v^^>vv^ ###v^^>vv^ ###^^>vv^ ###^>vv^ >###>vv^
^>^ v ^>^ v ### ^>^ v ###^>^ v ###>^ v ###^ v
> v<v >> > v<v >> > v<v >> > v<v >> > v<v >> > v<v >>
>v v<^ >v v<^ >v v<^ >v v<^ >v v<^ >v v<^
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
|> <| | | | v | | v | | >| | >| | >v| | >v| |>v^| |> ^| |v^^| | ^^|
| v | | v | | >| | >| | | | | | | | | | ^| | | | ^>| | >|
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
Rule 3 Rule 4 Rule 3 Rule 4 Rule 4 Rule 3
================================================================================
> <vv < > <### < > <vv <
v ###v v v ###v v v ###v v
>###>vv^ >v^^>vv^ >###>vv^
^>^ v ^>^ v ^>^ v
> v<v >> > v<v >> > v<v >>
>v v<^ >v v<^ >v v<^
+---+ +---+ +---+ +---+ +---+ +---+
|^ >| | >| |vv | | v | |^ >| | >|
|v^^| | ^^| |^ >| | >| |v^^| | ^^|
+---+ +---+ +---+ +---+ +---+ +---+
Rule 3 Rule 4 Rule 3
At this point, the cargo enters a loop between the last two frames.
Now consider a 2x3 cargo instead:
##<vv < >##vv < > <vv < > <vv < > <vv < > <vv <
## ^ >v v ##^ >v v ##^ >v v v ^ >v v v ^ >v v v ^ >v v
##>v^^>vv^ ##v^^>vv^ ##v^^>vv^ ##v^^>vv^ ##^^>vv^ >v^^>vv^
^>^ v ^>^ v ## ^>^ v ## ^>^ v ##^>^ v ##^>^ v
> v<v >> > v<v >> > v<v >> >##v<v >> > ##<v >> > ##<v >>
>v v<^ >v v<^ >v v<^ >v v<^ >v v<^ ## v<^
+--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+
|> | |> | | <| | | |v | |v | | >| | >| |>v| |>v| | | | |
| v| | v| |v | |v | | >| | >| | | | | | | | | | v| | v|
| | | | | >| | | | | | | | | | | | v| | v| |>v| |>v|
+--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+
Rule 4 Rule 3 Rule 4 Rule 3 Rule 3 Rule 3
================================================================================
> <vv < > <vv < > <vv <
v ^ >v v v ^ >v v v ^ >v v
>v^^>vv^ >v^^>vv^ >v^^>vv^
^>^ v ^>^ v ^>^ v
> ##<v >> > v<v >> > v<v >>
## v<^ ## v<^ >v v<^
## ## ##
## ##
##
+--+ +--+ +--+ +--+ +--+ +--+
| v| | v| |>v| |>v| | | | |
|>v| |>v| | | | | | | | |
| | | | | | | | | | | |
+--+ +--+ +--+ +--+ +--+ +--+
Rule 3 Rule 4 Rule 2
In the last step, rule 2 applies because the cargo has moved off the grid, so it comes to rest and there won't be a loop.
Rules and Assumptions
Your input will be the conveyor grid as described above along with the width and height of the cargo. You may take these three parameters in any convenient order and format. For the grid, this means that you can read a single string with lines separated by newlines or other characters, or an array of strings, or an array of arrays of characters, as long as the individual grid cells are still represented by the characters >v<^
and spaces.
You should output a truthy value if the setup results in a loop of at least two frames or a falsy value if the cargo will come to rest.
You may assume that the grid will be padded to a rectangle with spaces, and that the cargo will initially fit into the grid.
You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.
This is code golf, so the shortest answer (in bytes) wins.
Test Cases
The test cases are grouped by grids.
Grid (2x2):
>v
^<
Width Height Loop?
1 1 True
1 2 True
2 1 True
2 2 False
Grid (3x3):
> v
^ <
Width Height Loop?
1 1 False
1 2 False
1 3 False
2 1 False
2 2 True
2 3 True
3 1 False
3 2 True
3 3 False
Grid (4x3):
>^>v
v^v
^ <<
Width Height Loop?
2 2 False
Grid (6x5):
>v>v>v
^v^v^v
^v^v^v
^>^>^v
^<<<<<
Width Height Loop?
1 1 True
1 2 False
2 1 True
2 2 True
2 4 True
2 5 False
3 1 False
3 2 True
3 3 True
3 5 True
6 2 False
6 3 True
6 5 False
Grid (10x6):
> <vv <
v ^ >v v
>v^^>vv^
^>^ v
> v<v >>
>v v<^
Width Height Loop?
1 1 False
2 3 False
2 6 False
3 2 True
5 4 False
6 1 True
10 6 False
As an additional set of test cases, consider that any input where the grid consists solely of spaces must yield a falsy result.
I've checked all the test cases manually, so let me know if you think I've made a mistake.
10Fitting music – Fatalize – 2015-08-16T11:22:44.203
4@Fatalize I'm getting Twitch Plays Pokemon flashbacks... – undergroundmonorail – 2015-08-16T11:34:06.330
"Your input will be the conveyor grid as described above along with the width and height of the cargo. You may take these three parameters in any convenient order and format." - does this mean we can have the conveyor grid taken in as an array of arrays? That is, a 2x2 grid with
[^^/v<]
becomes[[0,1] [0,1];[0,-1] [-1,0]]
? Or do you mean it's up to us whether it's STDIN, a string input, a char array input, etc, but it still has to be in the form of ^, v, >, and <? – Glen O – 2015-08-16T11:39:44.767@GlenO You can take a newline (or other character) separated string, an array of strings, or an array of arrays of characters, but each cell should still be represented by the characters
><^v
or a space. I'll clarify that. – Martin Ender – 2015-08-16T11:41:09.040So what happens if the cargo moves onto a conflict where the continued direction is not one of the choices? That is, if it was moving right, and now must choose between up and left. – Joshua – 2015-08-16T20:47:33.747
@Joshua What's important is the axis the cargo was moving on, not the direction. So in your example it would then move to the left, because it was moving along the left-right axis. – Martin Ender – 2015-08-16T20:48:31.460
At first sight, I had read "the direction", instead of "the axis". What matters is in fact the direction — in the maths sense. – Nicolas Barbulesco – 2015-08-17T09:57:55.690
Your task is to figure out whether the cargo ever comes to rest or whether it will end up moving in a loop.
Did you seriously just ask us to solve the Halting Problem in a Code Golf exercise? ;) – Mason Wheeler – 2015-08-17T18:11:21.463@MasonWheeler Nope, not really. :P – Martin Ender – 2015-08-17T18:39:56.437