31
\$\begingroup\$

Your job is quite simple, write a program that prints Hello, world!, that when twisted creates a program that prints Twister!.

How strings are twisted

The twisting algorithm is very simple. Each column is shifted down by its index (col 0 moves down 0, col 1 moves 1, ...). The column shift wraps to the top. It kinda looks like this:

a
ba
cba
----
 cba
  cb
   c

With everything under the line wrapping to the top. Real example:

Original:
\\\\\\\\\\\\
............
............
............

Twisted:
\...\...\...
.\...\...\..
..\...\...\.
...\...\...\

(Further examples and a twisters in your favorite language are here)

Scoring

Your program must be a padded rectangle. This is code-golf so lowest byte count wins!

Rules

  • Your first program must print Hello, world!. Only one trailing newline is allowed.
  • Your first and second programs must be in the same language.
  • Your second program must print Twister!. Only one trailing newline is allowed.
  • Your program must have at least 2 rows and 2 columns.
\$\endgroup\$
4
  • 3
    \$\begingroup\$ In Jelly's code page, the character that corresponds to (and, for all purposes, acts like) the linefeed has the code point 127 (ASCII DEL). The character with code point 10 (ASCII linefeed) has the glyph ½ and takes the square root of a number. Which one of the two should be considered the newline for this challenge? \$\endgroup\$
    – Dennis
    Feb 2, 2016 at 23:26
  • \$\begingroup\$ Darn, was I the only one that hoped "Twisted Hello World" was using "twisted" in the perverse sense so we'd be outputting something like "Goodbye Cruel World" instead.. \$\endgroup\$
    – DasBeasto
    Feb 3, 2016 at 13:49
  • \$\begingroup\$ @Dennis I suppose the better one to use in this case would be the jelly newline. \$\endgroup\$
    – J Atkin
    Feb 3, 2016 at 14:17
  • \$\begingroup\$ @JAtkin OK, thanks for clarifying. I've updated my answer accordingly. \$\endgroup\$
    – Dennis
    Feb 3, 2016 at 17:45

9 Answers 9

42
\$\begingroup\$

Python 2, 59 bytes

print "  Hello, world!"[ 2::]
#rint "T w i s t e r !"[ ::2]

Twisted:

print "T weils,twerrd!"[ ::2]
#rint "  H l o   o l !"[ 2::]

Basically, puts the Twister! data in the odd indices of the string and then changes from removing the first two (padding) characters to removing every other character instead.

\$\endgroup\$
5
  • \$\begingroup\$ This is so much smarter than my approach \$\endgroup\$
    – wnnmaw
    Feb 2, 2016 at 22:20
  • \$\begingroup\$ This is kinda ridiculous, 40 upvotes? \$\endgroup\$
    – J Atkin
    Feb 18, 2016 at 17:24
  • \$\begingroup\$ @JAtkin I've noticed that some Python solutions tend to get huge amounts of them. Not that I'm complaining :~) \$\endgroup\$ Feb 18, 2016 at 19:18
  • \$\begingroup\$ Cooler way: print " Hello, world!" [2::] \$\endgroup\$ Jun 15, 2016 at 14:48
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Works, but I'll just keep it as is for now. \$\endgroup\$ Jun 15, 2016 at 16:39
17
\$\begingroup\$

Fission, 215 162 56 53 50 bytes

Here is a start:

D1
\\
""
TH
we
il
sl
to
e,
r 
!w
"o
1r
;l
1d
;!
 "

Try it online!

When twisted:

D"
\1
"\
T"
wH
ie
sl
tl
eo
r,
! 
"w
1o
;r
1l
;d
 !

Try it online!

Explanation

The Hello, world! code is fairly simple:

  • D spawns a single atom, going downwards.
  • The two \ (mirrors) deflect it onto the second column.
  • "Hello, world!" prints the required string.
  • 1 is a portal. It teleports the atom to the next 1 in reading order, retaining its direction (that's the one next to the r).
  • The atom still moves down, into the ; which destroys the atom and terminates the program.

The control flow for the Twister! code is a bit more... twisted...

  • Again, D spawns the atom.
  • \ deflects it to the right, into the 1.
  • Now the portal sends the atom to the next 1. The atom hits the o which just changes its mass, but we can ignore that. The code wraps around so the atom hits the same 1 again, skipping down two rows. Again, we can ignore the l, the atom wraps around and hits the 1 again. Now there is no further 1 in the code so the atom skips all the way back to the 1 at the top.
  • After wrapping around the edges once more, the atom is deflected again by \, now going down again.
  • "Twister!" prints the required code.
  • 1 teleports the atom once more, past the first ;, but there is another ; waiting for it to terminate the program.
\$\endgroup\$
5
  • \$\begingroup\$ Wow, very close now! :) I'm sure there's a way to be shorter... \$\endgroup\$ Feb 2, 2016 at 18:48
  • \$\begingroup\$ Why do you guys like fission? \$\endgroup\$
    – J Atkin
    Feb 2, 2016 at 18:57
  • \$\begingroup\$ @JAtkin Why wouldn't we? :) \$\endgroup\$ Feb 2, 2016 at 18:59
  • \$\begingroup\$ @MartinBüttner I was thinking specifically why for this challenge? \$\endgroup\$
    – J Atkin
    Feb 2, 2016 at 19:02
  • 1
    \$\begingroup\$ @JAtkin 2D languages seemed suitable, and Fission seemed particularly simple because you can choose one or more arbitrary entry points into the program. \$\endgroup\$ Feb 2, 2016 at 19:03
15
\$\begingroup\$

Fission, 35 bytes

Fission approach #3 (#4 counting the one I edited out of my first post).

R"Hello, " \"tri"
T;L"!dlrow"/"es!w

Try it online!

R;H"ldor w /"er!"
T"Le!ll,o""\"tsiw

Try it online!

Explanation

This one is actually the simplest of the Fission solutions yet. In both programs there are two entry points: R creates a right-going atom and L a left-going atom. In either case, the ; destroys one of them immediately.

Now in the Hello, world! program, the atom first prints half the string with "Hello, ", then \ and / (which are mirrors) deflect the atom onto the second line going left. "world!" (read in the direction of the moving atom) prints the rest of the string. L is now a no-op and ; destroys this atom as well, terminating the program.

The Twister! program is essentially the same but rotated by 180 degrees. This time, the L atom survives, and starts printing with "Twist". The \ and / again deflect it onto the other line, now going right. "er! prints the remainder of the string, R is a no-op and ; terminates the program.

\$\endgroup\$
1
  • \$\begingroup\$ Amazing! I think this is about as small as it can get, the amount of reuse is impressive. \$\endgroup\$ Feb 2, 2016 at 22:26
10
\$\begingroup\$

Jelly, 33 31 29 bytes

Original

“ɗ⁻%OḶ$“¡¦ḟṠ»Ṫ
“ɗ⁻%OḶ$“¡¦ḟṠ»Ḣ

Try it online.

Twisted

“ɗ⁻%OḶ$“¡¦ḟṠ»Ḣ
“ɗ⁻%OḶ$“¡¦ḟṠ»Ṫ

Try it online!

How it works

In each program, each line defines a link. The last one is the main link, and it is executed when the program starts. Since there are no references to the first link, it is simply ignored.

For both programs, “ɗ⁻%OḶ$“¡¦ḟṠ» yields the list ['Hello, world!', 'Twister!'], using Jelly's static dictionary compression.

The only difference between the original and the twisted code is the last character of the main link. selects the first string of the list, and selects the last one.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ It looks like it's screaming "Holy Sh*t" \$\endgroup\$
    – jado
    Jun 22, 2016 at 19:49
8
\$\begingroup\$

Fission, 53

R"Hello, world!";
R;."..!..!!.!".\.
.;T..w..i".se.t/.

Try it online!

and twisted:

R;.e..o..w..l..".
R"T"lw!,i!os!dt\;
.;H..l.. "!re"!/.

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Japt, 67 61 57 bytes

Saved 6 bytes thanks to @JAtkin, 4 bytes thanks to @Ian

"Tzwzizsztzezzzzzzrzzzz!"rz;
"Hzezlzlzoz,z zwzorlzdz!"rz;

Twisted:

"Hzezlzlzoz,z zwzorlzdz!"rz;
"Tzwzizsztzezzzzzzrzzzz!"rz;

Test it online: Original, Twisted

How it works

"Tzwzizsztzezzzzzzrzzzz!"rz;  // Take this string and remove all "z"s.
"Hzezlzlzoz,z zwzorlzdz!"rz;  // Take this string and remove all "z"s.
                              // Implicit: output *last* expression
\$\endgroup\$
5
  • \$\begingroup\$ If this works like I think it does, you can remove the trailing space after Twister! by replacing the spaces with .. \$\endgroup\$
    – J Atkin
    Feb 2, 2016 at 21:29
  • \$\begingroup\$ @JAtkin It actually doesn't work like that, but I can save a bunch of bytes that way. Thanks! \$\endgroup\$ Feb 2, 2016 at 21:33
  • \$\begingroup\$ "T.w.i.s.t.e.r.!. . . . . "k".(newline)"H.e.l.l.o.,. .w.o.r.l.d.!"k". \$\endgroup\$
    – J Atkin
    Feb 2, 2016 at 21:36
  • \$\begingroup\$ Could be much easier to read with a different filler character... \$\endgroup\$
    – mbomb007
    Feb 3, 2016 at 18:35
  • 4
    \$\begingroup\$ @mbomb007 Since when is "easy to read" an achievement? :D \$\endgroup\$
    – yo'
    Feb 3, 2016 at 19:20
4
\$\begingroup\$

Python, 398 414 380 456 bytes*

Managed to update so that it is compliant with the rules, but I still hesitate to call this competitive. Since the commented lines are needed for it to run, I've included them in the byte count

This solution does not follow the rules, as it will print error messages in addition to the allowed output.

I just wanted to see if this could be done in python. It can, but it is not pretty.

print'Hello, world!'
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     
#rint'Twister!'     

When twisted becomes

print'Twister!'
#rint'Twister!'
#rint'Twister!'
#rint'Twister!'
#rint'Twister!'
#rint'Twister!'
#rint'Hwister!'
#rint'Teister!'
#rint'Twlster!'
#rint'Twilter!'
#rint'Twisoer!'
#rint'Twist,r!'
#rint'Twiste !'
#rint'Twisterw'
#rint'Twister!o
#rint'Twister!'r
#rint'Twister!' l
#rint'Twister!'  d
#rint'Twister!'   !
\$\endgroup\$
9
  • \$\begingroup\$ I think you could remove the space in each line, then remove the last line altogether. \$\endgroup\$ Feb 2, 2016 at 21:15
  • \$\begingroup\$ I didn't think to do that since it was against the rules of the initial twisting challenge, but thanks! \$\endgroup\$
    – wnnmaw
    Feb 2, 2016 at 21:18
  • \$\begingroup\$ You forgot to update the code ;) \$\endgroup\$ Feb 2, 2016 at 21:25
  • \$\begingroup\$ I figured it wouldn't really matter, but I did it for good measure \$\endgroup\$
    – wnnmaw
    Feb 2, 2016 at 21:29
  • 1
    \$\begingroup\$ I think you have misunderstood. The code is required to be a padded rectangle, but you can remove the space from rint ' on every line. \$\endgroup\$ Feb 2, 2016 at 21:29
2
\$\begingroup\$

C (gcc), 87 bytes

Untwisted

main(){puts(1?"Hello, world!":"Twister!");}
mai (){puts(0?"Hello, world!":"Twister!");}

Try it online!

Twisted

mai (){puts(1?"Hello, world!":"Twister!");}
main(){puts(0?"Hello, world!":"Twister!");}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Brainfuck, 467 367 285 bytes

Untwisted

 +[  [[-->-[>>+>-----<<]<--<---]>-.>>>+.>>..+++[.>]<<<<.+++.------.<<-.>>>>+.>>] [[--[<++++>--->+<]>-]<<<<.<<<-.<<+.>-.+.<----.>--.>>---.[-]]]
  [ -[[-->-[>>+>-----<<]<--<---]>-.>>>+.>>..+++[.>]<<<<.+++.------.<<-.>>>>+.>>]+[[--[<++++>--->+<]>-]<<<<.<<<-.<<+.>-.+.<----.>--.>>---.[-]]]

Try it online!

Twisted

  [  [[-->-[>>+>-----<<]<--<---]>-.>>>+.>>..+++[.>]<<<<.+++.------.<<-.>>>>+.>>] [[--[<++++>--->+<]>-]<<<<.<<<-.<<+.>-.+.<----.>--.>>---.[-]]]
 +[ -[[-->-[>>+>-----<<]<--<---]>-.>>>+.>>..+++[.>]<<<<.+++.------.<<-.>>>>+.>>]+[[--[<++++>--->+<]>-]<<<<.<<<-.<<+.>-.+.<----.>--.>>---.[-]]]

Try it online!

\$\endgroup\$
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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