Python 2, 79 bytes
lambda s:all(n==reduce(lambda n,c:-~[n,n%3+n%4][c>'L']%5,s,n)for n in range(5))
This uses the group-theoretic approach from Ilmari Karonen's GolfScript answer to my 2014 challenge, in particular to the bonus question.
Instead of the ant crawling around the dodecahedron, we imagine it stays in place while the dodecahedron turns under its feet. We must determine whether the sequence of rotations returns the dodecahedron to the initial position.
There are 60 rotation of the dodecahedron -- 12 faces times 5 positions per face. These rotations can be expressed as permutations of 5 elements. Specifically, they permute 5 colored tetrahedra shown below that partition the 20 vertices of the dodecahedron.
We could take each rotation and write down which color tetrahedron moves onto which color's position to obtain a permutation of the five colors. Specifically, these are the even permutations, which are half of the 5!=120 permutations of five elements, or 60, forming the alternating group \$A_5\$ on five elements. The other 60 odd permutations would involve reflecting the dodecahedron and well as rotating it, so they do not figure in this problem.
We want to choose two even permutations that represent the rotations from the ant turning left and right. As noted by Ilmari Karonen, the two permutations must have order 5, corresponding to five left or five right turns brining the ant back to the same place. Moreover, their product must have order 5 as well, and neither can be a power of the other.
There are many choices for the two permutations. If we think of them as permuting the values 0 to 4, the pair our code uses is 12340
and 13042
. The first one simply adds one modulo 5. From here, there's 5 choices for the other one: 13042, 14302, 24310, 32041, 32410
. The code uses 13042
because it happens to have a short expression in Python 1+(n%3+n%4)%5
, or just n%3+n%4
if we "share" the +1
and %5
for the other case`. I found this via some brute-forcing.
Now let's take another look at the code:
lambda s:all(n==reduce(lambda n,c:-~[n,n%3+n%4][c>'L']%5,s,n)for n in range(5))
To check that the sequence of permutations combines to the identity, we test each starting value n
from range(5)
, apply the sequence of permutations to it using reduce
, and check that all
of them end up at the initial value. The -~
is a shortcut to add one.
An alternative approach might be to implement the permutation composition directly using bit operations as I do in my 2018 answer to my challenge with the cube. The idea here would be store the state as a 15-bit number split into five 3-bit fields, and implement the two permutations of the 5 bit fields using bit operations. At least one of the bit operations would be fairly involved, but it would parallelize the current solution's applying permutations to 5 starting values, so I'm not sure which would win out.
Python 2, 77 bytes
lambda s:all(n==reduce(lambda n,c:-~[n,48/~n][c>'L']%5,s,n)for n in range(5))
I found 48/~n
as a shorter alternative for the second permutation when it is incremented and taken mod 5.