I thought I'd use this opportunity to show off a new Retina feature: multi-stage loops. This should shorten many tasks considerably (especially conditional replacement).
Retina is my own regex-based programming language. The source code can be grouped into stages: each stage consists of two lines where the first contains the regex (and potentially some configuration) and the second line is the replacement string. The stages are then applied to STDIN in order and the final result is printed to STDOUT.
You can use the above directly as a source file with the
-s command-line switch. However, I'm not counting the switch, because you can also just put each line in a separate file (then you lose 15 bytes for the newlines, but add +15 for the additional files).
The new thing about this solution is the
) in the penultimate stage. This closes a multi-stage loop. There is no matching
(, which means that the loop implicitly starts at the first stage. Hence, the first 7 stages are repeated until a full pass through all 7 of them stops changing the result. These 7 stages simply perform various transformations which gradually reduce the number of matrices in the string and combine phases. Once we reach the final result, none of the seven patterns matches any more and the loop ends. Afterwards, we append a 0 if there is no digit in the result yet (as the above stages simply drop all identities, including the result).
Here is what the individual stages do:
Combines all pairs of
- to reduce the phase characters.
Now if there are two consecutive identical characters left, it's either
-- or two identical matrices. In either case, multiplying them gives the identity. But we don't need identities, so we just remove all of them, and the explicit identities (the
0s) as well. This stage is repeated in itself with
+ until the result stops changing. This ensures that things like
123321 get resolved completely, such that the next step can assume that all pairs of digits are distinct.
This is actually two separate transformations in one (for golfitude). Note that if the first alternative matches,
$3 are empty, and if the second one matches
$1 is empty. So this can be decomposed into these two steps:
This just swaps all pairs of digits and adds a minus sign. Since we removed all
0s and all identical pairs, this will only match
13. This step may seem weird, but it allows me to only check for half of these cases later on, because the ones I can't process then will be swapped here in the next iteration.
The other part of the above stage was:
This gradually moves
- signs all the way to the left (one position per iteration). I do this such that ultimately they are all next to each other and get resolved in the earlier step.
These three stages now simply resolve the three pairs of products. As I said above, this will only catch half of the relevant cases, but the other half will be taken care of in the next iteration, after the previous step swapped all pairs.
This is the last stage of the loop. It's similar to the one that shifts
- to the left, except for
i. The main difference is that this one swaps
i only with digits. If I used
(.)i then in cases where I get a
i- the two would be swapped indefinitely and the program wouldn't terminate. So this only swaps them to the right of the
- signs. This is sufficient - as long as all
i appear together at some point, they can be resolved correctly.
The final step (outside of the loop). Remember that we always deleted all identities, so if the result is actually the identity (times a phase), then we won't have the required digit in the output any more, so we add it back.
As an example, here are all the intermediate forms of
0223202330203313021301011023230323 (skipping stages that don't perform any changes):
321321312 # Remove identities
-23-31-12-132 # Swap all pairs
-23-31-i3-132 # Resolve 12
-i1-31-i3-132 # Resolve 23
-i1-i2-i3-132 # Resolve 31
-i-1i-2i-3-312 # Move - to the left and swap pairs
-i-1i-2i-3-3i3 # Resolve 12
-i-i1-i2-3-i33 # Move i to the left
-i-i1-i2-3-i # Remove identities
--ii-1i-2-3i # Move - to the left
--ii-i1-2-i3 # Move i to the left
----i1-2-i3 # Resolve ii
i1-2-i3 # Remove identities
i-1-2i3 # Move - to the left
i-1-i23 # Move i to the left
-i-1i-32 # Move - to the left and swap pairs
-i-i1-32 # Move i to the left
--ii-1-23 # Move - to the left and swap pairs
--ii-1-i1 # Resolve 23
----1-i1 # Resolve ii
1-i1 # Remove identities
-1i1 # Move - to the left
-i11 # Move i to the left
-i # Remove identities. Now the loop can't change this any longer.
-i0 # Fix the result by adding in the 0.