# Transform characters of your choice into "Hello, world!"

Write a program or function that takes a character as input and outputs a character. Also, choose a list of 13 distinct ASCII printable characters (32-126). When a character from this list is passed into the program or function, it should output the character at the corresponding position in Hello, world!. This way, running your code on each element of the list in turn would produce Hello, world!.

Example:

If your chosen list is Troublemaking, you should produce a program or function that maps:

T -> H
r -> e
o -> l
u -> l
b -> o
l -> ,
e ->
m -> w
a -> o
k -> r
i -> l
n -> d
g -> !


Rules:

• Your score is length of the program/function.
• You can't store any data between program/function runs.
• Include list of the characters you chose into the answer.
• I think this is an interesting puzzle, where it might not be obvious at first to solvers that competitive answers probably shouldn't write "Hello, world!" at all in the their code.
– xnor
Jul 31 at 22:01
• I made some edits that I hope make the challenge easier to understand. open-ended-function is used for challenges where the solver has the flexibility to choose what the inputs or the outputs of the code are, and it's important to choose well to make your code short. I think this applies here.
– xnor
Jul 31 at 22:10
• Assuming that the input character is valid, should the output character always be in the range 32-126 as well? Jul 31 at 22:21
• @Arnauld No, it doesn't need to be in this range. But if you make two versions where one of them satisfies such property and other doesn't, then feel free to share both.
– Jiří
Jul 31 at 22:28
• @WalterMitty The program can always produce just single character, so it can't output the whole string.
– Jiří
Aug 2 at 14:36

# Python 3, 18 bytes

A function that inputs and outputs an ASCII code.

lambda c:c-954%c%3


Try it online!

The chosen 13-byte string is: Helmo,"yprnd!, and was found using brute force.

Alternative Solutions

lambda c:c^656%c%7    # 18b ('Mahim/!sntjd"')
lambda c:c^308268%c   # 19b ('Z{@HU 2I}BY5')
lambda c:c|2242944%c  # 20b ('HeLdi, AoblD!')
lambda c:c^c*2%54%21  # 20b ('Ngchb"+|i{lt,')
lambda c:c-c*c%60%11  # 20b ('Mjmpr-#|tvqe$')  • 2 questions: did you write this in C for speed? also, how did you decide on the form x-x0%x%x1? are there are other forms you also tried? Aug 1 at 4:04 • @Jonah you can see the other forms dingle tried in the Alternative Solutions section – c-- Aug 1 at 4:08 • Doh! In general, how are you finding the candidates? Just random trial and error or is it informed by any theory? Aug 1 at 4:09 • @Jonah Yeah, speed is the main reason I usually use C for brute force tasks like this (and perhaps a bit of personal favouritism). To your second question, it's mostly trial and error. I tried many possible arrangements, but interestingly many were of the form c OP (something involving c). Going down that path eventually led to the final formula x-x0%x%x1, and at that point there wasn't much left to try. Then there's the acquired knowledge of knowing what works and what doesn't (a modulo chain with possibly other operators mixed in works well, for example). Aug 1 at 5:14 • @Jonah: If you had a fixed set of input characters, you could use gperf to look for a "perfect hash function", a sequence of math ops that map those inputs to those outputs. gnu.org/software/gperf/manual/gperf.html#Motivation (This is something that's useful for compiler internals, apparently. And in that case it's often not a 1:1 mapping.) Aug 2 at 22:57 # Perl 5, 8 bytes −1 byte by Sisyphus. y;yav;ol  Input: Heavy, world! Try it online! • y;yav;ol works for -1 Aug 1 at 2:58 • Explanation: y is character substitution (transliteration) like tr, or sed's y operator, from one of the set yav to the set ol. y turns into o, a or v turn into l. Those are the repeated characters in Hello, World!, thus the ones that need to get remapped so 13 unique input characters can produce it. Nice idea to use this instead of some kind of "hash" function. Aug 3 at 1:41 • I find it amusing that using ; as the regex delimiter allows you to remove the otherwise necessary trailing ;. I'm guessing it's because the -p option implicitly adds a semicolon at the end of the program. Aug 3 at 2:03 # JavaScript (ES6), 16 bytes Expects and returns an ASCII code. Produces Hello, world! when given (a-IK*s]d5P/+. c=>28+c*c%389%96  Try it online! # JavaScript (ES6), 19 bytes This version was built such that the output character is guaranteed to be in the range 32-126 as well. The OP has since clarified that this is not required. Expects and returns an ASCII code. Produces Hello, world! when given ,X=CI:YWx.~7-. c=>32+c*c*71%193%91  Try it online! ### Full mapping  | 0 1 2 3 4 5 6 7 8 9 A B C D E F ---+-------------------------------- 20 | M < S B Y = I - 9 m y G H ! r @ 30 | 6 T J h S f Q d D L , 4 d l 6 3 40 | c U z l + x 7 y - o # Z i : > u 50 | n ? 8 Y R s a w e ^ i L W / / 60 | b b / / W L i ^ e w a s R Y 8 70 | ? n u > : i Z # o - y 7 x + l  • Very nice answer, can you please also include description how this formula was created or include link for tool or guide which was used? – Jiří Jul 31 at 22:31 • you could also do 23+c*c%202%97 with "4=n6,8.AT\I' (found with brute-force) Jul 31 at 23:11 • There's also 30+c*c%593%90 with < &g89ksi^x+% and 31+c*c%591%95 with 276F=>uAThLaW. @Jiří I used this code since you were interested. Jul 31 at 23:28 • @Jonah You need to write a function from character to character, not an entire string. Aug 1 at 0:24 • @Jonah And you must do so with 13 distinct characters, which is what makes the challenge interesting. Aug 1 at 0:27 # Python, 31 bytes lambda c:c.translate('ll !o'*8) Attempt This Online! Input characters to use are Hel#o, w"r$d!

• That's a really clever use of translate; I didn't know that was possible!
Jul 31 at 23:16
• @Adam just recycling an old idea... Jul 31 at 23:23

# JavaScript (Node.js), 14 bytes

c=>'lol'[c]||c


Try it online!

Input text is:

Hel0o, w1r2d!


lol

• lol 𝅷𝅷𝅷𝅷𝅷𝅷 Aug 3 at 18:39

# x86 32-bit machine code, 7 bytes

A8 28 75 02 0C 6C C3


Try it online!

Using the regparm(1) calling convention, this takes an ASCII code in AL and returns one in AL.

Chooses He@DG, world!.

In assembly:

f:  test al, 0b00101000 # Set flags based on bits 3 and 5 of the character.
jnz e               # If they are not both 0 (true for all of "Hello, world!"),
or al, 0b01101100   # Otherwise, set bits 2, 3, 5, and 6 to 1.
# (These are the bits that are 1 in both 'l' and 'o'.)
e:  ret                 # Return.


# Jelly, 6 bytes

“½ṬṚ»y


Try it online!

Takes Heacn, wmrld!. Searched with this script.

     y    Transliterate the input by
“½ṬṚ»     "monoclinal".


Unless I brute-forced something wrong, a 2-byte compressed string does not seem to be possible.

# 05AB1E, 7 5 bytes

∞'Ÿ™‡


Uses H2345, world! as 13 characters.

Explanation:

∞      # Push an infinite positive list: [1,2,3,...]
'Ÿ™  '# Push dictionary string "hello"
‡  # Transliterate the characters of the (implicit) input-string
# (after which the result is output implicitly)


See this 05AB1E tip of mine (section How to use the dictionary?) to understand why 'Ÿ™ is "hello".

# Vyxal, 6 bytes

kakH¡Ŀ


Try it Online! Takes a, b, ... m

     Ŀ # Transliterate
ka     # Lowercase alphabet
kH¡  # To corresponding chars of "Hello, world!"

• @aketon Sentence case, the "w" has to be lowercase Aug 2 at 23:49

# Retina 0.8.2, 7 bytes

Tdo-l


Try it online! Explanation: Inspired by @loopywalt's Python answer, works by translating 0 to o and both 3 and 4 to l, so given the 13 distinct characters Hel3o, w0r4d! as input the output is Hello, world! as desired. (The full transliteration actually maps 0123456789 to onmlllllll.)

# Bash, 9 bytes

tr 012 ol


Takes Hel1o, w0r2d!

Try it online!

• −1 byte: tr 213 ol Aug 1 at 3:04

# Python, 32 bytes

lambda c:"Helo, wrd!"[ord(c)%10]

Attempt This Online!

Takes in characters [F,G,H,R,I,J,K,L,S,M,\,N,O].

## Python, 35 bytes

lambda c:"Hello, world!"[ord(c)-97]

Attempt This Online!

Takes in [a,b,c,d,e,f,g,h,i,j,k,l,m].

• Of course, @Arnauld's JS answer also works in Python in 23 bytes: lambda c:28+c*c%389%96, but takes 38 bytes if it takes and returns a character: lambda c:chr(28+ord(c)*ord(c)%389%96)
Jul 31 at 23:21

# C (clang), 23 bytes

f(c){return 656%c%7^c;}


Try it online!

Takes Mahim/!sntjd". Brute forced solution found using this program.

# Nibbles, 6.5 6 bytes (12 nibbles)

=r$d:"lo"  "Hel1o, w2r4d!" becomes "Hello, world!". $d          # keep only digit characters
r             # and get the value
# (so non-digits become zero)
=               # and use this to index into
"lo"    # the string "lo"
:        # with the input character appended
# (wrapping around for index >3)


# JavaScript (V8), 12 bytes

c=>c-954%c%3


Try it online!

Using the string Helmo,"yprnd!, ported from dingledooper's answer

Expects input as a charcode and outputs as the charcode

# K (ngn/k), 26 bytes

{"Hello world!"@(i$x-97)}  Try it online! A port of Adam's Python answer. Takes in [a..l]. • "Hello world!"i$-97+ Aug 9 at 12:42

# Charcoal, 9 bytes

⭆Ｓ§⁺ιloΣι


Try it online! Link is to verbose version of code. Takes as input the 13 distinct characters He142, world!. Explanation: Works by mapping 147 to l and 258 to o.

 Ｓ          Input string
⭆           Map over characters and join
ι       Current character
⁺        Concatenated with
lo     Literal string lo
§         Cyclically indexed by
ι   Current character
Σ    Digit value (0 if not a digit)


## Knight, 13 bytes

O-=cP%%954c 3


## Knight, 16 bytes

O+28%%^P2 389 96


Takes Ifmmp-!xpsme"`