Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.
share|improve this question
2  
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? – Dennis Feb 2 at 23:26
    
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.. – DasBeasto Feb 3 at 13:49
    
@Dennis I suppose the better one to use in this case would be the jelly newline. – J Atkin Feb 3 at 14:17
    
@JAtkin OK, thanks for clarifying. I've updated my answer accordingly. – Dennis Feb 3 at 17:45
up vote 7 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
This is so much smarter than my approach – wnnmaw Feb 2 at 22:20
    
This is kinda ridiculous, 40 upvotes? – J Atkin Feb 18 at 17:24
    
@JAtkin I've noticed that some Python solutions tend to get huge amounts of them. Not that I'm complaining :~) – Pietu1998 Feb 18 at 19:18

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.
share|improve this answer
    
Wow, very close now! :) I'm sure there's a way to be shorter... – FryAmTheEggman Feb 2 at 18:48
    
Why do you guys like fission? – J Atkin Feb 2 at 18:57
    
@JAtkin Why wouldn't we? :) – Martin Büttner Feb 2 at 18:59
    
@MartinBüttner I was thinking specifically why for this challenge? – J Atkin Feb 2 at 19:02
1  
@JAtkin 2D languages seemed suitable, and Fission seemed particularly simple because you can choose one or more arbitrary entry points into the program. – Martin Büttner Feb 2 at 19:03

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.

share|improve this answer
    
Amazing! I think this is about as small as it can get, the amount of reuse is impressive. – FryAmTheEggman Feb 2 at 22:26

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!

share|improve this answer

Python, 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!'   !
share|improve this answer
    
I think you could remove the space in each line, then remove the last line altogether. – ETHproductions Feb 2 at 21:15
    
I didn't think to do that since it was against the rules of the initial twisting challenge, but thanks! – wnnmaw Feb 2 at 21:18
    
You forgot to update the code ;) – ETHproductions Feb 2 at 21:25
    
I figured it wouldn't really matter, but I did it for good measure – wnnmaw Feb 2 at 21:29
1  
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. – ETHproductions Feb 2 at 21:29

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
share|improve this answer
    
If this works like I think it does, you can remove the trailing space after Twister! by replacing the spaces with .. – J Atkin Feb 2 at 21:29
    
@JAtkin It actually doesn't work like that, but I can save a bunch of bytes that way. Thanks! – ETHproductions Feb 2 at 21:33
    
"T.w.i.s.t.e.r.!. . . . . "k".(newline)"H.e.l.l.o.,. .w.o.r.l.d.!"k". – J Atkin Feb 2 at 21:36
    
Could be much easier to read with a different filler character... – mbomb007 Feb 3 at 18:35
2  
@mbomb007 Since when is "easy to read" an achievement? :D – yo' Feb 3 at 19:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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