Image

Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Identifying returning paths.

+2
−0

Let's imagine a point in three dimensional space. We will start it located at $(0,0,0)$. Then we will receive a series of instructions. x, y, and z indicate moving unit distance in the positive $x$, $y$, and $z$ directions respectively. The capital letters X, Y, and Z indicate moving the same distance in the opposite directions. This way we perform a walk on the cubic lattice.

I want to know if a given path returns to the origin, and for each edge in the lattice the number of times the walk crosses it in the positive direction and the negative direction are the same. In other words, every step that you take is done in reverse at a future point.

We give some examples at the end.

Your task is to write a computer program which takes a string containing only xyzXYZ and outputs one of two distinct values. The first value if the input satisfies the condition the second if it fails the condition.

This is code-golf, so the goal is to minimize the size of your source code as measured in bytes.

Math

This puzzle doesn't come out of nowhere, it actually emerges out of some group theory. This isn't at all necessary to solve the problem, but I think it's interesting, and maybe it will be helpful for someone.

We first observe that the set of valid paths almost forms a group. If we concatenate two valid paths we always get a valid path, but we lack inverses. This can be fixed by allowing cancellation. We say that e.g. xX and  are the same. That is, immediately undoing an edge is the same as having never crossed it in the first place. With this equivalence notion we see that every valid path has an inverse, simply trace the path backwards.

This might be recognizable as homotopy equivalence. Two valid paths are equivalent if you can continuously deform one into the other and vice versa.

The collection of all loops returning to the origin under the operation of concatenation with cancellation is the fundamental group of the cubic lattice. However, not everything in the fundamental group is equivalent to a valid path. For example xyXY is a loop, but it's not a valid path. So this is a subgroup of the fundamental group.

I think figuring out a description for the subgroup on your own is a fun exercise, so I will put the rest of this in spoilers.

Spoilers This subgroup is precisely the commutator subgroup of the fundamental group!

This is especially interesting if you are already aware that the fundamental group of the cubic lattice is the commutator subgroup of the free group on three generators, $F_3$. Thus this question can really be stated as:

Identify if an element of $F_3$ is in the second derived subgroup $[[F_3,F_3],[F_3,F_3]]$.

Of course this is a normal subgroup, so identifying its elements is the same as solving the word problem for $F_3\setminus G$. This group is the free metabelian group on three generators, $M_3$. Thus this question can really be stated as:

Identify if a word is equal to the identity in $M_3$.

These aren't just cool, but they offer a small insight. For this we use a theorem of Fox:

Theorem (Fox)

Let $N\lhd F_r$ with the natural map $\mu : F_r\rightarrow F_r\setminus N$. A word $w\in F_r$ is in $[N,N]$ if and only if the Fox derivative of $w$ with respects to every generator of $F_r$, is zero under $\mu$. That is: \begin{equation} \forall i \left(\dfrac{\partial w}{\partial x_i}\right)^\mu = 0 \end{equation}

Since in our case $N$ is the commutator subgroup of the initial group itself. Thus our puzzle is equivalent to the following:

Check if all the second Fox derivatives of a word are trivial. i.e. \begin{equation} \forall i,j \dfrac{\partial^2}{\partial x_i\partial x_j} w = 0 \end{equation}

Examples

  • xyXY

    this path returns to the origin tracing out a square in the anticlockwise direction. However it crosses 4 edges of the lattice and each exactly once. While it makes the same number of steps in the positive direction as in the negative direction (this is required to return to the origin), those steps are across different edges. Thus this fails.
    xyXY

  • xyXYzxyXYZyxYXzyxYXZ

    this path is more complicated. We can break it down as follows:

    1. xyXY: it traces an anticlockwise square returning to the origin. The same as above.
    2. z: it takes a step in the positive $z$ direction.
    3. xyXY: it traces the same square as before but at this new location.
      xyXYzxyXY
    4. Z: it returns to the origin.
      Z
    5. yxYX: it traces a clockwise square. This is the reverse of the square it traced in step 1.
    6. z: it takes a step in the positive $z$ direction.
    7. yxYX: it reverses the square taken in step 3.
      yxYXzyxYX
    8. Z: it returns to the origin.
      Z

    This passes.

Test cases

xxYZ -> False
xyXY -> False
xyXXYxxyXXYx -> False
yXYxYXyyxYXYxy -> True
YXyxxYXyXYxxyX -> True
xyXXYxyxYXXyxY -> True
yXYxzXZyxYXzxZ -> True
XyxYxYXyyXYxYxyX -> True
xyXYzxyXYZyxYXzyxYXZ -> True
xyXzxYXZxzyZXYzxyXZxYXyxzYZXYzyZxxzXXYZyzxxZXX -> True
History

1 comment thread

An extra test case (1 comment)

1 answer

+3
−0

Perl, 122 bytes

Reads line from stdin, exits 0 if true; 1 if false.

perl -nE'@s=(0)x 3;map{@e=($x+=/x/-/X/,$y+=/y/-/Y/,$z+=/z/-/Z/);$c{"@s @e"}++;$c{"@e @s"}--;@s=@e}/./g;exit(%c<grep$_,%c)'

Store start/end (+1) and end/start (-1) move counters.
If any counter ends up non-zero there is an imbalance.

perl -nE'
    @s = (0) x 3;
    map {
        @e = ($x+=/x/-/X/, $y+=/y/-/Y/, $z+=/z/-/Z/);

        $c{"@s @e"}++;
        $c{"@e @s"}--;

        @s = @e
    } /./g;

    exit( %c < grep $_,%c )  # grep returns keys and non-zero values
                             # which is >= keys alone
'

Try it online!

History

1 comment thread

Are extra test cases needed? (3 comments)

Sign up to answer this question »