16
\$\begingroup\$

As a kind of part 2 to Hello, World! (Every other character), write a program such that all three of these programs print "Hello, World!": the entire program, the 1st, 3rd, 5th, etc. characters of your program, and the 2nd, 4th, 6th, etc.

If your program is:

abc
def

It should output "Hello, World!", but so should

acdf

And

b
e

No solutions with built-in "Hello, World!"s.

\$\endgroup\$
  • 6
    \$\begingroup\$ Why not just change the text from "Hello, World!" to something else? \$\endgroup\$ – Conor O'Brien Jul 2 '17 at 3:32
  • 1
    \$\begingroup\$ I think that avoids loopholes enough and "Hello, World!" is canonical. \$\endgroup\$ – Leo Tenenbaum Jul 2 '17 at 3:35
  • 24
    \$\begingroup\$ IMO it'd be nice to see a different string than "Hello, World!" once in a while (that's just my opinion) \$\endgroup\$ – Conor O'Brien Jul 2 '17 at 3:36
  • 1
    \$\begingroup\$ Are whitespaces and newlines counted as characters? \$\endgroup\$ – officialaimm Jul 2 '17 at 3:58
  • 6
    \$\begingroup\$ @ConorO'Brien With the pleasant side effect that very few languages will have built-ins for literally every other string. \$\endgroup\$ – Dennis Jul 2 '17 at 18:14

17 Answers 17

23
\$\begingroup\$

x86 machine code, 378 bytes

demo

PROG.COM Download and run it in MS-DOS emulator, DOSBox for example.

B3 B4 B3 02 90 B3 B3 B4 02 B3 B4 B3 02 90 B3 B2
B3 48 90 B3 B3 B2 48 B3 B2 B3 48 90 B3 CD B3 21
90 B3 B3 CD 21 B3 CD B3 21 90 B3 B2 B3 65 90 B3
B3 B2 65 B3 B2 B3 65 90 B3 CD B3 21 90 B3 B3 CD
21 B3 CD B3 21 90 B3 B2 B3 6C 90 B3 B3 B2 6C B3
B2 B3 6C 90 B3 CD B3 21 90 B3 B3 CD 21 B3 CD B3
21 90 B3 CD B3 21 90 B3 B3 CD 21 B3 CD B3 21 90
B3 B2 B3 6F 90 B3 B3 B2 6F B3 B2 B3 6F 90 B3 CD
B3 21 90 B3 B3 CD 21 B3 CD B3 21 90 B3 B2 B3 2C
90 B3 B3 B2 2C B3 B2 B3 2C 90 B3 CD B3 21 90 B3
B3 CD 21 B3 CD B3 21 90 B3 B2 B3 20 90 B3 B3 B2
20 B3 B2 B3 20 90 B3 CD B3 21 90 B3 B3 CD 21 B3
CD B3 21 90 B3 B2 B3 77 90 B3 B3 B2 77 B3 B2 B3
77 90 B3 CD B3 21 90 B3 B3 CD 21 B3 CD B3 21 90
B3 B2 B3 6F 90 B3 B3 B2 6F B3 B2 B3 6F 90 B3 CD
B3 21 90 B3 B3 CD 21 B3 CD B3 21 90 B3 B2 B3 72
90 B3 B3 B2 72 B3 B2 B3 72 90 B3 CD B3 21 90 B3
B3 CD 21 B3 CD B3 21 90 B3 B2 B3 6C 90 B3 B3 B2
6C B3 B2 B3 6C 90 B3 CD B3 21 90 B3 B3 CD 21 B3
CD B3 21 90 B3 B2 B3 64 90 B3 B3 B2 64 B3 B2 B3
64 90 B3 CD B3 21 90 B3 B3 CD 21 B3 CD B3 21 90
B3 B2 B3 21 90 B3 B3 B2 21 B3 B2 B3 21 90 B3 CD
B3 21 90 B3 B3 CD 21 B3 CD B3 21 90 B3 CD B3 20
90 B3 B3 CD 20 B3 CD B3 20 90

Also you can download LEFT.COM and RIGHT.COM

How it works and how to run

See answer to part one

Magic

If you want to execute 0xAB opcode command with one parameter 0xCD, you write

Magic

Generator

Run code, get hex. Convert in to binary

cat prog.hex | xxd -r -p > PROG.COM

function byte2hex(byte){
	var ret=byte.toString(16).toUpperCase();
	return ret.length==1 ? "0"+ret : ret;
}

function str2hex(str){
	var ret = [];
	for(var i=0;i<str.length;i++){
		ret.push(byte2hex(str.charCodeAt(i)));
	}
	return ret;
}

function genCode(hexArr){
	var ret = [["B4","02"]];
	for(var i=0;i<hexArr.length;i++){
		if(hexArr[i]!=hexArr[i-1]){
			ret.push(["B2",hexArr[i]]);
		}
		ret.push(["CD","21"]);
	}
	ret.push(["CD","20"]);

	return ret;
}

function format(arr){
	var ret=[],tmp=[];
	for(var i=0;i<arr.length;i++){
		tmp.push(arr[i]);
		if(i%16==15 || i==arr.length-1){ret.push(tmp.join(" "));tmp=[];}
	}
	return ret.join("\n");
}

function magicCode(str,mode){
	var ret=[];
	var code=genCode(str2hex(str));

	for(var i=0;i<code.length;i++){
		var ab=code[i][0],cd=code[i][1],tmp;
		tmp=["B3",ab,"B3",cd,"90","B3","B3",ab,cd,"B3",ab,"B3",cd,"90"];
		for(var j=0;j<tmp.length;j++){
			if((mode&1 && j%2==0) || (mode&2 && j%2==1)){ret.push(tmp[j])}
		}
		
	}
	return ret;
}

const LEFT=1,RIGHT=2,ALL=3;

var str=prompt("string","Hello, world!");

var out = ["PROG.COM\n",format(magicCode(str,ALL)),"\n\nLEFT.COM\n",format(magicCode(str,LEFT)),"\n\nRIGHT.COM\n",format(magicCode(str,RIGHT))].join("\n");

document.write(out.replace(/\n/ig,"<br>"));

\$\endgroup\$
12
\$\begingroup\$

Python 3, 115 bytes

print=="partisn't";
"ran" >="partisn't" ;
print((""" "HHeelllloo,,  wwoorrlldd!! """[" ) #2">"1":: 3- 1] [ 1:-1 ]))

Every odd character

pit=print;"a">"ats'";pit(" Hello, world! "[  2>1: -1  :1])

Every even character

rn="ats'"
rn =print 
rn("""Hello, world!""")#""":3 ][1- )

Try it online!

This could probably get a lot shorter, but I'm happy I managed to get it to work in Python. Similar to vroomfondel's answer on the first part.

Boring 93 byte answer

""""""
print("Hello, world!")
""""""#
""""

pprriinntt((""HHeelllloo,,  wwoorrlldd!!""))
#"""

Every odd character

"""pit"el,wrd"
"""
""
print("Hello, world!")#"

Every even character

"""
rn(Hlo ol!)"""#""
print("Hello, world!")
""

Try it online!

\$\endgroup\$
9
\$\begingroup\$

><>, 45 bytes

! """!!ddllrrooWW  oolllleeHH"!!"" >~o <> o <

Or just even characters:

 "!dlroW olleH"!">o<  

Or just odd characters:

!""!dlroW olleH!" ~ >o<

Try them online: original, evens, odds.

The original version pushes "!!ddllrrooWW oolllleeHH" to the stack, then the fish bounces between >~o <, which deletes a letter, prints two, deletes two, prints two, deletes two, etc. The two half-programs are fairly standard string printing programs. The tricky part was combining " and ! to toggle in and out of string mode in all three programs correctly.

\$\endgroup\$
7
\$\begingroup\$

Befunge-98, 43 bytes

120 x""!!ddllrrooWW  ,,oolllleeHH""cckk,,@@

Try it online!

Only the odd ones:

10x"!dlroW ,olleH"ck,@

Try it online!

Only the even ones:

2 "!dlroW ,olleH"ck,@

Try it online!

Explanation

The full program:

120               Push 1, push 2, push 0.
x                 Pop 0 (y) and 2 (x) and set the instruction pointer's movement
                  delta to (x,y). That is, run the remainder of the code
                  by skipping every other cell.
"!dlroW ,olleH"   Push the code points of the output.
ck,               Print 13 characters from the top of the stack.
@                 Terminate the program.

In the odd program, the 2 is gone, so that 10x really does nothing at all (it sets the delta to (1,0) which is the default anyway). The remainder of the program is then the same.

In the even program, we just push the 2 at the beginning which we can ignore entirely. The remainder of the program is the same as before.

\$\endgroup\$
6
\$\begingroup\$

Gammaplex, 46 bytes

//

EERRrrXXXX""HHeelllloo,,  WWoorrlldd!!""XX

See here.

Interpreter. It may need some changes to work in modern compilers.

\$\endgroup\$
5
\$\begingroup\$

Mathematica, 65 bytes

PPrriinntt[[""HHeelllloo,,  WWoorrlldd!!""]]Print@"Hello, World!"

It throws some warnings, prints Hello, World!, and returns Null PPrriinntt[["" HHeelllloo, Null, "" WWoorrlldd!!]]. When run as a program (not in the REPL), the return value will not be printed.

After removing the even characters:

Print["Hello, World!"]Pit"el,Wrd"

It prints Hello, World!, and returns "el,Wrd" Null Pit.

After removing the odd characters:

Print["Hello, World!"]rn@Hlo ol!

It prints Hello, World!, and returns Null ol! rn[Hlo].

\$\endgroup\$
5
\$\begingroup\$

x86 machine code, 73 bytes

Inspired by Евгений Новиков's solution, I thought it should be doable with less tricks, i.e., just jumping around to otherwise "disjoint" codes for all three variants. I'm still trying with a smart variant that uses lodsb; lodsb as central point (so only one string constant is needed for all variants)

EB 14 00 00 8A 8A 17 16 01 01 B4 B4 09 09 CD CD
21 21 CD CD 20 20 8A 1f 01 B4 09 CD 21 CD 20 48
65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 00 48 48 65
65 6c 6c 6c 6c 6f 6f 2c 2c 20 20 57 57 6f 6f 72
72 6c 6c 64 64 21 21 00 00

If I recall correctly from my childhood days, COM's tiny model starts with DS=CS=SS and the code is loaded beginning from CS:0100h. I do not assume it is guaranteed that the code is loaded into a zeroed memory block (if it were guaranteed, I could drop two bytes).

Disassembly of the long code should be

  JMP *+14h
  ; 20 irrelevant bytes
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
message:
  DB "Hello, World!\0"
  DB "HHeelllloo,,  WWoorrlldd!!\0\0"

Disassembly of odd code

  JMP *+00h
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
  ; some irrelevant bytes
message:
  DB "Hello, World!\0"

Disassembly of even code:

  ADC AL,00h
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
  ; some irrelevant bytes
message:
  DB "Hello, World!\0"
\$\endgroup\$
  • \$\begingroup\$ Good job! In DOSBox program is not working. I don't know why com file with this code freeze system at start. \$\endgroup\$ – Евгений Новиков Jul 2 '17 at 12:28
  • \$\begingroup\$ Test your code next time. The 8A at 0116 should be BA instead, and strings are terminated by $, not NULL. \$\endgroup\$ – NieDzejkob Dec 22 '18 at 19:54
4
\$\begingroup\$

Retina, 48 bytes

G |`

HHeelllloo,,  WWoorrlldd!!
$_&

(.)\1t?
$1

Try it online!

Odd positions:

G|
Hello, World!
_
()1?$

Try it online!

Even positions:

 `
Hello, World!$&
.\t
1

Try it online!

Explanation

The full program:

G |`

This does nothing at all. The | is not an existing configuration option. The G makes this a grep stage, but there is really nothing to be grepped and the regex is empty any, so this doesn't do anything. The purpose of this stage is to have two linefeeds in front of the main "Hello, World!" line so that one of them always survives the reduction. The reason for making this a grep stag is that we need to offset the parity of the lines, and grep stages only require a single line.


HHeelllloo,,  WWoorrlldd!!

This turns the (empty) working string into the required output with each character doubled.

$_&

This does nothing. The regex tries to match a _ and a & after the end of the string which is of course impossible. We'll need those characters in the reduced version though, again to deal with vanishing linefeeds.

(.)\1t?
$1

Finally, we remove the duplicate characters by replacing (.)\1 with $1. The t? is never used but will again be necessary in the reduced versions.

The odd program:

G|
Hello, World!

The G can't match the empty input, but that's why we have the | to allow an alternative empty match. This turns the empty working string into the desired output.

_
()1?$

This replaces underscores with ()1?$, but there are no underscores in the string, so this does nothing.

The even program:

 `
Hello, World!$&

The ` just denotes an empty configuration string, so we again use the empty regex to replace the working string with the output. This time we also insert $& but that's the match itself, which is empty, of course, so it doesn't do anything.

.\t
1

This would replace any character followed by a tab with a 1, but we don't have any tabs, so this is also a no-op.

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

Lua, 106 bytes

----[[[[
print("Hello, World!")
--[[
---- ] ]
---- ] ]
pprriinntt((""HHeelllloo,,  WWoorrlldd!!""))
----]]

Try it online!

Alternate 1:

--[[
rn(Hlo ol!)-[
--]]--  
print("Hello, World!")--]

Try it online!

Alternate #2:

--[[pit"el,Wrd"
-[--  
--]]print("Hello, World!")
--]

Try it online!

Alternator script: Try it online!

\$\endgroup\$
3
\$\begingroup\$

CJam, 44 bytes

{{HHeelllloo,,  WWoorrlldd!!}}ss{{}}ss-X)%L-

Try it online!

\$\endgroup\$
2
\$\begingroup\$

PHP, 70 bytes

"";echo'Hello, World!'.""//pprriinntt''HHeelllloo,,  WWoorrlldd!!''
;;

Odd characters:

";coHlo ol!."/print'Hello, World!';

Even characters:

"eh'el,Wrd'"/print'Hello, World!'
;
\$\endgroup\$
2
\$\begingroup\$

Haskell, 92 bytes

{---- }

mmaaiinn==ppuuttSSttrr""HHeelllloo,,  WWoorrlldd!!""----}main=putStr"Hello, World!"

Try it online! It looks like the comment abuse is too much for the syntax highlighting to handle. {- ... -} is an inline or multi-line comment, whereas -- starts a line comment.

Odd characters:

{--}
main=putStr"Hello, World!"--mi=uSrHlo ol!

Try it online!

Even characters:

-- 
main=putStr"Hello, World!"--}anptt"el,Wrd"

Try it online!

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

LOGO, 71 bytes

;;\\
pr[Hello, World!]er "|

pprr[[HHeelllloo,,  WWoorrlldd!!]]
;;\\
|

(the program have a trailing newline)

Two removed versions:

;\p[el,Wrd]r"
pr[Hello, World!]
;\|

and

;\
rHlo ol!e |
pr[Hello, World!];\

(the program have a trailing newline)

For an explanation what pr and er do, see this post. In this case, er is feed with a word determine procedure name.

The \ is escape character in Logo, which will escape the newline after the end of the comment, therefore make the second line (rHlo ol!e |) of second removed program a comment.

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

Javascript,68 bytes

//**
alert`Hello, World`//**//aalleerrtt``HHeelllloo,,  WWoorrlldd``

Odd Characters:

/*aetHlo ol`/*/alert`Hello, World`

Even Characters:

/*
lr`el,Wrd/*/alert`Hello, World`

Modified version of my answer on the other one.

\$\endgroup\$
  • 1
    \$\begingroup\$ Why do you need the space at the start? \$\endgroup\$ – CalculatorFeline Jul 2 '17 at 16:08
  • \$\begingroup\$ This doesn't seem to be syntactically valid when you take the 1st, 3rd, etc. characters. Also, it's missing a l in HHeelllloo. \$\endgroup\$ – Arnauld Jul 3 '17 at 0:16
  • \$\begingroup\$ @CalculatorFeline,@Arnauld fixed \$\endgroup\$ – SuperStormer Jul 4 '17 at 22:42
1
\$\begingroup\$

Perl 5, 61 bytes

60 bytes code + 1 for -p.

$ _ =$ _ =q";HHeelllloo,,  WWoorrlldd!!";;;q##;s/;|.\K.//g##

Try it online!

Every Odd Byte

  $_="Hello, World!";q#s;.K/g#

Try it online!

Every Even Byte

$_=  q;Hello, World!;;#;/|\./#

Try it online!

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

BotEngine, 180 178 bytes

Based on my answer to this question.

 vv 
 v < 
 eHH 
 eee 
 ell 
 ell 
 eoo 
 e,, 
 e   
 eWW 
 eoo 
 err 
 ell 
 edd 
 e!! 

>>P          e     e     e     e     e     e     e     e     e     e     e     e     e P

Odd characters (note the multiple trailing spaces on the last line):

 v
v<
eH
ee
el
el
eo
e,
e 
eW
eo
er
el
ed
e!
>P                                          

Even characters:

v     H  e  l  l  o  ,     W  o  r  l  d  ! 
>     e  e  e  e  e  e  e  e  e  e  e  e  eP  
\$\endgroup\$
0
\$\begingroup\$

Runic Enchantments, 52 bytes

 L @""H!edlllroo,W  W,oorlllde!H"" ~@"!dlroW ,olleH"

Try it online!

Runic is not usually very good at dealing with radiation as having flow control characters removed at random makes tracing execution a huge pain, but predictable radiation like every other character? Easy, we just encode two programs that are reversed of each other and interleaved, then tack on a third copy for the base execution and control which one is executed with a single character. In program 2, the third copy is garbage that's never seen, and in program 3 it retains the quotes, allowing it to be popped without printing it.

Program 1 only executes this part:

 L                                  @"!dlroW ,olleH"

Program 2 only executes this part:

    " H e l l o ,   W o r l d ! "   @               

Like this:

  "Hello, World!" @!lo olH

Try it online!

Program 3 only executes this part:

 L @ " ! d l r o W   , o l l e H " ~ " d r W , l e "

Like this:

L@"!dlroW ,olleH"~"drW,le"

Try it online!

The "drW,le" portion is executed, but the ~ immediately pops it off the stack, preserving the desired output.

Naively it would appear that a conversion of the ><> answer would result in a shorter program, weighing in at 45 bytes:

! ```!!ddllrrooWW  oolllleeHH`!!`` R~$ LR $ L

However, Runic has one limitation that ><> does not have: a maximum stack size of 10 + IP's mana (which is initially 10). And !!ddllrrooWW oolllleeHH contains 24 characters, causing the IP to bleed mana until it expires just before executing the R command, resulting in no output for the base program.

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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