24 19 bytes
- Input goes in the input field, output is printed to the output field
Try it here
First, let's expand the cube
A B q - ! w p U
p ) W . @ ; ; .
The steps in the execution can be split up in three phases:
- Parse input
- Compare characters
- Print result
Phase 1: Input
The first two characters that are executed are
A reads all input and pushes it as character codes to the stack. Note that this is done in reverse, the first character ends up on top of the stack, the last character almost at the bottom. At the very bottom,
EOF) is placed, which will be used as a counter for the amount of consecutive characters at the end of the string. Since we need the top of the stack to contain the last two characters, we reverse the stack, before entering the loop. Note that the top part of the stack now looks like:
..., C[n-1], C[n], -1.
The IP's place on the cube is where the
E is, and it's pointing right. All instructions that have not yet been executed, were replaced by no-ops (full stops).
A B E . . . . .
. . . . . . . .
Phase 2: Character comparison
The stack is
..., C[a-1], C[a], counter, where
counter is the counter to increment when the two characters to check (
C[a-1]) are equal. The IP first enters this loop at the
S character, moving right. The
E character is the position where the IP will end up (pointing right) when
C[a-1] do not have the same value, which means that subtracting
C[a-1] does not yield
0, in which case the instruction following the
! will be skipped (which is a
. S q - ! w E .
p ) W . . ; ; .
Here are the instructions that are executed during a full loop:
q-!;;p) # Explanation
q # Push counter to the bottom of the stack
# Stack (counter, ..., C[a-1], C[a])
- # Subtract C[a] from C[a-1], which is 0 if both are equal
# Stack (counter, ..., C[a-1], C[a], C[a-1]-C[a])
! # Leave the loop if C[a-1]-C[a] does not equal 0
;; # Remove result of subtraction and C[a] from stack
# Stack (counter, ..., C[a-1])
p # Move the bottom of the stack to the top
# Stack (..., C[a-1], counter)
) # Increment the counter
# Stack (..., C[a-1], counter + 1)
And then it loops around.
Phase 3: Print result
Since we left the loop early, the stack looks like this:
counter, ..., C[a-1]-C[a]. It's easy to print the counter, but we have to increment the counter once because we didn't do it in the last iteration of the loop, and once more because we started counting at
-1 instead of
0. The path on the cube looks like this, starting at
S, pointing right. The two no-ops that are executed by the IP are replaced by arrows that point in the direction of the IP.
. B . . . S p U
. ) . . @ . . .
The instructions are executed in the following order. Note that the
B) instructions at the end change the stack, but don't affect the program, since we are about to terminate it, and we do not use the stack anymore.
p))OB)@ # Explanation
p # Pull the counter to the top
# Stack: (..., counter)
)) # Add two
# Stack: (..., counter + 2)
O # Output as number
B) # Reverse the stack and increment the top
@ # End the program
Alea iacta est.