8
\$\begingroup\$

There's an old story about a stonecutter who was never satisfied with what he was. He wished he could become the sun, and so he was. Blocked by the clouds, he wished to be - and became - a cloud. When the wind blew, he wished himself into being the wind. Stopped by the mountain, he wished to be a mountain, and thus became one. Soon, however, he was being hacked at by a stonecutter, and wished to be a stonecutter.

Similarly, your task is to write a program which is never satisfied with the language it is in. Your program must output a set of regex replacements to turn it into a different language. And so on.

Rules

  1. Write a program in a language of your choice.
  2. The program should output a series of at least two segments. A segment is a bunch of text, separated by semicolons. However, if a semicolon is contained within a regex, such that breaking it up there will result in invalid syntax, it does not separate segments. See the example.
  3. The first segment is a regex pattern to find, and the second segment is the replacement pattern. The third is another "find" pattern, and the fourth is a "replace" pattern, and so on.
  4. Apply the regex patterns to the program. Replace the pattern on the first segment with one on the second; replace the third pattern with the one on the fourth, and so on.
  5. The result should be a program in a different language, which, itself, follows rules 2 to 4.
  6. The languages used must form an infinite, repeating cycle.
    • For example, Python -> PHP -> C++ -> Python -> PHP -> C++ -> Python -> ...
  7. Your score is the period of the cycle. Ties are broken by shortest initial code length.
    • In the above example, the score is three.
  8. In each iteration of the cycle, no language may be used more than once.
  9. For rules 5 and 7, compatible languages (C and C++) and different versions of the same language (Python 2 and Python 3) are considered the same.
  10. The programs themselves do not need to repeat.
    • In the above example, the first and fourth programs may be different.
  11. Any version of regex is acceptable, but the same must be used for all the programs.
  12. The total output for each program may not exceed 100 characters.
  13. Each output must contain instructions to actually change the program. That is, no two consecutive programs in the cycle may be the same.

Example

Python -> Ruby -> Python -> ...

print "uts;Z;rint;uts;Z(?=;Z);rint"

Outputs:

uts;Z;rint;uts;Z(?=;Z);rint

The segments are:

FIND     ;    REPLACE
uts      ;    Z
rint     ;    uts
Z(?=;Z   ;    rint   (breaking the first segment in two would result in invalid syntax)

Applying the regex replacements, in order, gives us:

print "Z;Z;rint;Z;Z(?=;Z);rint"      # replace each "uts" with "Z"
puts "Z;Z;uts;Z;Z(?=;Z);uts"         # replace each "rint" with "uts"
puts "rint;Z;uts;rint;Z(?=;Z);uts"   # replace each "Z" followed by ";Z" with "rint"

Running the last line gives us the instructions to turn this back into Python code.

\$\endgroup\$
  • \$\begingroup\$ If you're using code size as tie breaker, do we really need the 100 character limit? \$\endgroup\$ – Martin Ender Oct 23 '14 at 21:45
  • \$\begingroup\$ @MartinBüttner Yes; why not? \$\endgroup\$ – Ypnypn Oct 23 '14 at 21:46
  • \$\begingroup\$ Sure, but it's a character longer than \; ;) \$\endgroup\$ – Martin Ender Oct 23 '14 at 21:46
  • \$\begingroup\$ C++ isn't necessarily compatible with C. \$\endgroup\$ – golfer9338 Oct 24 '14 at 0:25
  • 8
    \$\begingroup\$ I think you probably want to require that all the programs in the cycle be different. If I'm wrong, please state so - and I claim priority on the obvious solution. \$\endgroup\$ – Peter Taylor Oct 24 '14 at 9:59
6
\$\begingroup\$

2 Languages: Python, Ruby; 33 29 bytes

Here is another way to do Python and Ruby, that's a bit shorter than the one in the challenge:

Python:   print'^;puts"^.*?\\42#\\73"#'
prints:   ^;puts"^.*?\42#\73"#

Ruby:     puts"^.*?\42#\73"#print'^;puts"^.*?\\42#\\73"#'
prints:   ^.*?"#;

It shouldn't be too difficult to add PHP into the mix.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

2 languages: Python 2 and Befunge-93, 77 bytes

After carefully reading the rules this time, I came up with a real answer. It won't win any prizes, but Befunge is just too fun to program in.

u=u">>>>>:#,_@;Z;print;>>>>>:#,_@;Z(?=;Z);print;<v;Y;u;<v;Y(?=;Y);u"
print u

This Python program outputs:

>>>>>:#,_@;Z;print;>>>>>:#,_@;Z(?=;Z);print;<v;Y;u;<v;Y(?=;Y);u

Which yields these replacements:

FIND         REPLACE
>>>>>:#,_@   Z
print        >>>>>:#,_@
Z(?=;Z)      print
<v           Y
u            <v
Y(?=;Y)      u

Which turns the program into this Befunge program:

<v=<v"print;Z;>>>>>:#,_@;print;Z(?=;Z);>>>>>:#,_@;u;Y;<v;u;Y(?=;Y);<v"
>>>>>:#,_@ <v

Maybe I'll see if I can make it one line. Frankly, I'm a little surprised Befunge works for this sort of problem at all.

(Sorry for deleting and undeleting a bunch; I was panicking for a second because I thought the program might not have worked.)

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.