An Ant on a Cube

GolfScript, 24 chars (19 for function body only)

Math FTW!

{3,.@{[+~@\{@}*~]}/=}:f;

Test this solution online.

This function takes as input a binary array (0 for left, 1 for right) and returns 1 for true and 0 for false.

Conceptually, it works by rotating the cube so that the ant always maintains the same position and orientation, and checking whether the cube finally ends up in the same orientation as it began in.

In particular, we can represent the left and right turns as two linear maps in three dimensions, where a left turn corresponds to a 90° rotation around the x axis, i.e. the map (x, y, z) → (x, z, −y), and a right turn corresponds to a 90° rotation around the y axis, i.e. the map (x, y, z) → (z, y, −x).

At the beginning of the function, we simply set up a three-element vector containing the distinct positive values (1, 2, 3), apply the sequence of rotation maps to it, and check whether the resulting vector equals the initial one.

(In fact, to save a few chars, I actually transform the coordinates so that the initial vector is (0, 1, 2) and the maps are (x, y, z) → (x, z, −1−y) and (x, y, z) → (z, y, −1−x), but the end result is the same.)

Ps. Thanks to proud haskeller for spotting the bug in the original version of this solution.


Perl, 58 chars

As requested in the comments, here's the same solution ported to Perl. (This version actually uses the untransformed coordinates, since the transformation saves no chars in Perl.)

sub f{@a=@b=1..3;@a[$_,2]=($a[2],-$a[$_])for@_;"@a"eq"@b"}

Test this solution online.


Bonus: Ant on a Dodecahedron (GolfScript, 26 chars)

{5,.@{{2*2%[~\]}*(+}/=}:f;

Test this solution online.

Like the ant-on-a-cube function above, this function takes as input a binary array (0 for left, 1 for right) and returns 1 if the ant ends up at the same position and orientation as it started in, or 0 otherwise.

This solution uses a slightly more abstract representation than the cube solution above. Specifically, it makes use of the fact that the rotational symmetry group of the dodecahedron is isomorphic to the alternating group A5, i.e. the group of even permutations of five elements. Thus, each possible rotation of the dodecahedron (that maps edges to edges and vertices to vertices) can be uniquely represented as a permutation of a five-element array, with consecutive rotations corresponding to applying the corresponding permutations in sequence.

Thus, all we need to do is find two permutations L and R that can represent the left and right rotations. Specifically, these permutations need to be 5-cycles (so that applying them five times returns to the original state), they must not be powers of each other (i.e. RLn for any n), and they need to satisfy the relation (LR)5 = (1), where (1) denotes the identity permutation. (In effect, this criterion states that the path LRLRLRLRLR must return to the original position.)

Fixing the L permutation to be a simple barrel shift to the left, i.e. mapping (a, b, c, d, e) → (b, c, d, e, a), since it can be implemented in GolfScript in just two chars ((+), we find that there are five possible choices for the R permutation. Out of those, I chose the mapping (a, b, c, d, e) → (c, e, d, b, a), since it also has a relatively compact GolfScript implementation. (In fact, I implement it by first interleaving the elements with 2*2% to obtain (a, c, e, b, d), then swapping the last two elements with [~\], and finally applying the L permutation unconditionally to move a to the end.)

The online demo link above includes some test cases of valid paths on a Dodecahedron that return to the origin, such as:

           # empty path
1 1 1 1 1  # clockwise loop
0 0 0 0 0  # counterclockwise loop
1 0 0 0 0 1 1 0 0 0 0 1  # figure of 8
1 0 1 0 1 0 1 0 1 0      # grand circle
1 0 0 0 1 0 0 0          # loop around two faces 
1 0 0 0 1 1 1 0 1 0 1 0 0 0 1 1 1 0 1 0  # Hamilton cycle

Python, 68

Takes a list of 1 and -1. Based on 3D rotations: checks if the point (3,2,1) ends up at the same position after applying a series of rotations. There are two possible rotations, corresponding to 1 and -1. Each one is done by permuting two coordinates and changing the sign of one of them. The exact coordinates to change and which sign to permute is not important.

def f(l):
 p=[3,2,1]
 for d in l:p[d],p[0]=-p[0],p[d]
 return[3,2]<p

EDIT: this is actually mostly the same solution as "Perl, 58".


Mathematica

Inspired by Ilmari Karonen's solution. The rotational symmetry group of a cube is isomorphic to S4.

Cube, 51 bytes

Fold[Part,r=Range@4,{{2,3,4,1},{3,4,2,1}}[[#]]]==r&

Takes a list of 1s and -1s as input.

Try it online!

Dodecahedron, 55 bytes

Fold[Part,r=Range@5,{{2,3,4,5,1},{3,5,4,2,1}}[[#]]]==r&

Takes a list of 1s and -1s as input.

Try it online!

Tags:

Code Golf