Build a twisted "Hello world!"

31

4

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.

J Atkin

Posted 2016-02-02T17:50:59.333

Reputation: 4 846

3

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 – 2016-02-02T23:26:30.007

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 – 2016-02-03T13:49:43.557

@Dennis I suppose the better one to use in this case would be the jelly newline. – J Atkin – 2016-02-03T14:17:47.200

@JAtkin OK, thanks for clarifying. I've updated my answer accordingly. – Dennis – 2016-02-03T17:45:39.007

Answers

10

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.

Dennis

Posted 2016-02-02T17:50:59.333

Reputation: 196 637

2It looks like it's screaming "Holy Sh*t" – phase – 2016-06-22T19:49:15.053

42

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.

PurkkaKoodari

Posted 2016-02-02T17:50:59.333

Reputation: 16 699

This is so much smarter than my approach – wnnmaw – 2016-02-02T22:20:37.100

This is kinda ridiculous, 40 upvotes? – J Atkin – 2016-02-18T17:24:31.753

@JAtkin I've noticed that some Python solutions tend to get huge amounts of them. Not that I'm complaining :~) – PurkkaKoodari – 2016-02-18T19:18:00.313

Cooler way: print " Hello, world!" [2::] – Erik the Outgolfer – 2016-06-15T14:48:59.423

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Works, but I'll just keep it as is for now. – PurkkaKoodari – 2016-06-15T16:39:15.417

17

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.

Martin Ender

Posted 2016-02-02T17:50:59.333

Reputation: 184 808

Wow, very close now! :) I'm sure there's a way to be shorter... – FryAmTheEggman – 2016-02-02T18:48:27.050

Why do you guys like fission? – J Atkin – 2016-02-02T18:57:28.547

@JAtkin Why wouldn't we? :) – Martin Ender – 2016-02-02T18:59:43.127

@MartinBüttner I was thinking specifically why for this challenge? – J Atkin – 2016-02-02T19:02:24.003

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 Ender – 2016-02-02T19:03:33.307

15

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.

Martin Ender

Posted 2016-02-02T17:50:59.333

Reputation: 184 808

Amazing! I think this is about as small as it can get, the amount of reuse is impressive. – FryAmTheEggman – 2016-02-02T22:26:14.720

8

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!

FryAmTheEggman

Posted 2016-02-02T17:50:59.333

Reputation: 16 206

4

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!'   !

wnnmaw

Posted 2016-02-02T17:50:59.333

Reputation: 1 618

I think you could remove the space in each line, then remove the last line altogether. – ETHproductions – 2016-02-02T21:15:18.837

I didn't think to do that since it was against the rules of the initial twisting challenge, but thanks! – wnnmaw – 2016-02-02T21:18:16.010

You forgot to update the code ;) – ETHproductions – 2016-02-02T21:25:25.043

I figured it wouldn't really matter, but I did it for good measure – wnnmaw – 2016-02-02T21:29:04.727

1I 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 – 2016-02-02T21:29:49.217

You are absolutely right, silly me – wnnmaw – 2016-02-02T21:33:13.630

@wnnmaw The only thing to fix now is the excessively long bytecount.

– Erik the Outgolfer – 2016-06-15T14:38:16.273

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ, interesting my usual counter comes up with a different count... wonder why?

– wnnmaw – 2016-06-15T14:39:52.543

@wnnmaw That one is completely broken, so use mothereff.in

– Erik the Outgolfer – 2016-06-15T14:48:09.123

4

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

ETHproductions

Posted 2016-02-02T17:50:59.333

Reputation: 47 880

If this works like I think it does, you can remove the trailing space after Twister! by replacing the spaces with .. – J Atkin – 2016-02-02T21:29:44.317

@JAtkin It actually doesn't work like that, but I can save a bunch of bytes that way. Thanks! – ETHproductions – 2016-02-02T21:33:43.617

"T.w.i.s.t.e.r.!. . . . . "k".(newline)"H.e.l.l.o.,. .w.o.r.l.d.!"k". – J Atkin – 2016-02-02T21:36:32.850

Could be much easier to read with a different filler character... – mbomb007 – 2016-02-03T18:35:58.990

4@mbomb007 Since when is "easy to read" an achievement? :D – yo' – 2016-02-03T19:20:12.057

2

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!

gastropner

Posted 2016-02-02T17:50:59.333

Reputation: 3 264

1

Brainfuck, 467 367 285 bytes

Untwisted

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

Try it online!

Twisted

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

Try it online!

orthoplex

Posted 2016-02-02T17:50:59.333

Reputation: 339

1

You can save bytes by using the current shortest known "Hello, world!" program

– Jo King – 2019-04-08T10:53:56.270

@JoKing Thank you for the suggestion! Any ideas how to find a smaller "Twister!" program as well? – orthoplex – 2019-04-08T12:06:21.223

1

Using bf-crunch I can get a 56 byte "Twister!" program

– Jo King – 2019-04-08T12:35:41.333