33
4
An ant walks along the edges (not faces) of a wireframe cube. Each vertex it encounters presents it with a fork from which two new edges branch off. The ant chooses which way to turn -- left
or right
. These direction are relative to the ant, who is facing the vertex and is outside the cube. Your goal is to determine, from the sequence of left
/right
choices the ant took, whether it ends at the same position that it started.
For example, if the ant turns left four times (left left left left
), it will have traversed a square counterclockwise and ended at the same place it started. But, if it goes left left left left right
, it will end on a different spot on the cube. Also, if it goes left right right right left
, it ends on its starting edge but facing the opposite vertex, which does not count as the same position.
The ant's path might repeat edges, including the edge it started at, but what matters is where it ends after the whole sequence.
Write a named function that takes in the ant's sequence of turns and outputs whether the ant is back at its start position after the sequence. Assigning an unnamed function to a variable is enough to make it a named function.
(Edit: If your language can't make a named function, it can instead implement the function with inputs and outputs through STDIN/printing or the stack. If that's not possible, make it a snippet in which the input and output are saved in variables.)
Input
A sequence of left
/right
decisions of length 0
to 31
inclusive, represented in a format of your choice. This might be a string of letters R
/L
, a list of numbers 1
/-1
, or an array of Booleans. Nothing cheesy like having them be method names or strings useful for your code.
Please post the test cases in your format if it's different from the test cases below.
Output
True
/False
, 0
/1
, or the analogues in your language.
Winning criteria
Fewest bytes wins. Remember, you need to give a named function. You can have code outside the function, but those bytes count too. Your function should behave correctly if called multiple times.
Test cases
True
cases (one per line, second one is empty list):
1 1 1 1
-1 -1 -1 -1
1 -1 1 -1 1 -1
1 1 -1 -1 1 1 -1 -1
-1 1 1 -1 -1 1 1 -1
1 1 1 -1 -1 -1 -1 1
1 -1 -1 1 -1 -1
1 1 1 1 -1 -1 -1 -1 1 -1 -1 1 -1 -1
-1 -1 -1 1 -1 -1 1 1 -1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
False
cases (one per line):
1
1 1
1 1 1
-1 1
1 -1 -1 -1 1
1 -1 -1 1 1
-1 1 -1 1
1 1 1 1 -1
-1 -1 1 -1 1 -1 -1 1
1 -1 1 1 1 1 -1 -1 -1 1 1 -1 -1 -1
Here's the same test cases with L
's and R
's.
True
cases:
RRRR
LLLL
RLRLRL
RRLLRRLL
LRRLLRRL
RRRLLLLR
RLLRLL
RRRRLLLLRLLRLL
LLLRLLRRLRLRRRRRRRRRRRRRRRRR
False
cases:
R
RR
RRR
LR
RLLLR
RLLRR
LRLR
RRRRL
LLRLRLLR
RLRRRRLLLRRLLL
Extra credit challenge
Same thing, but with a dodecahedron rather than a cube. See Hunt the Wumpus for ideas.
-1, I don't think requiring a named function adds anything interesting to this challenge. – Erik the Outgolfer – 2018-03-10T18:58:13.437
@xnor does f←train in apl pass as a named function? (discussion)
– ngn – 2018-03-10T19:42:46.000@EriktheOutgolfer I agree that it's better to allow unnamed functions, but in 2014 named functions were pretty much standard, and changing it at this point would be unfair to the old answers. – xnor – 2018-03-10T21:08:07.277
@ngn Looking at the chat discussion, that looks like a named function to me. Sorry about the outdated restriction, it's a hazard of old challenges. – xnor – 2018-03-10T21:10:45.897
@xnor Oh, so assigning to a variable is enough? That is kind of different from what is considered a named function in e.g. Python, so I'll clarify. – Erik the Outgolfer – 2018-03-11T10:14:46.487
@EriktheOutgolfer Yes, assigning to a variable is fine. – xnor – 2018-03-11T21:21:34.970
How flexible is input, was a format of you choice intended to mean a choice from the list that you give? – H.PWiz – 2018-03-12T21:28:26.653
@xnor Can we output 0 for "the ant is back at the start position" and 1 otherwise? Can we use any non-zero integer instead of 1? – ngn – 2018-03-13T01:18:39.400
@ngn No, you should use 1 for yes and 0 for no if you use integers. – xnor – 2018-03-13T01:31:56.473
@H.PWiz Input as flexible, any two simple distinct tokens would be fine. – xnor – 2018-03-13T03:51:38.297
@xnor "If your language can't make a named function, it can instead implement the function with inputs and outputs through STDIN/printing or the stack." - What if the language can make a named function? Must the function accept the input as an argument or is stdin still allowed? – ngn – 2018-03-16T19:11:21.880
@ngn No, the function should accept function argument. Unfortunately this challenge was from before the standard of not making allowances based on language capabilities. Also, feel free to make whatever APL-specific interpretation you want for your bounty; I think it would be better not to just have someone win it just by taking a more liberal interpretation of the IO format. – xnor – 2018-03-16T21:17:08.777
Do
same position
meansame vertex
orsame edge, same direction
? – l4m2 – 2018-03-19T04:45:21.303@l4m2 Same edge, same direction. – xnor – 2018-03-19T05:27:38.893
Does this preclude the use of languages without named functions? – Mike Precup – 2014-08-09T18:34:40.447
@MikePrecup Can you give me some examples of such languages? I'll look into alternatives. – xnor – 2014-08-09T18:36:13.217
I do all my code golf submissions in ><>, which is why I ask. It has a stack that you can load the args onto the top of, and then leave the result on the stack, but it's not exactly a named function.
– Mike Precup – 2014-08-09T19:00:54.670@MikePrecup OK, I put in an allowance for that. If there still an issue for some language, please tell me, I don't want to exclude any languages. – xnor – 2014-08-09T19:07:53.917
I can think of befunge and ><> and this kind of languages – proud haskeller – 2014-08-09T19:24:23.420
Maybe add RLRLRL to the True cases – proud haskeller – 2014-08-09T19:26:16.527
@proudhaskeller OK, added. – xnor – 2014-08-09T19:34:03.827
SO what are those extra credits for? :P – Martin Ender – 2014-08-09T19:52:32.557