29
\$\begingroup\$

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.
\$\endgroup\$
11
  • 6
    \$\begingroup\$ 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. \$\endgroup\$
    – xnor
    Jul 31, 2022 at 22:01
  • 5
    \$\begingroup\$ 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. \$\endgroup\$
    – xnor
    Jul 31, 2022 at 22:10
  • 2
    \$\begingroup\$ Assuming that the input character is valid, should the output character always be in the range 32-126 as well? \$\endgroup\$
    – Arnauld
    Jul 31, 2022 at 22:21
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – Jiří
    Jul 31, 2022 at 22:28
  • 3
    \$\begingroup\$ @WalterMitty The program can always produce just single character, so it can't output the whole string. \$\endgroup\$
    – Jiří
    Aug 2, 2022 at 14:36

19 Answers 19

15
\$\begingroup\$

Perl 5, 8 bytes

−1 byte by Sisyphus.

y;yav;ol

Input: Heavy, world! Try it online!

\$\endgroup\$
3
  • 7
    \$\begingroup\$ y;yav;ol works for -1 \$\endgroup\$
    – Sisyphus
    Aug 1, 2022 at 2:58
  • \$\begingroup\$ 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. \$\endgroup\$ Aug 3, 2022 at 1:41
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ Aug 3, 2022 at 2:03
13
\$\begingroup\$

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}B`Y5')
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$')
\$\endgroup\$
6
  • \$\begingroup\$ 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? \$\endgroup\$
    – Jonah
    Aug 1, 2022 at 4:04
  • 1
    \$\begingroup\$ @Jonah you can see the other forms dingle tried in the Alternative Solutions section \$\endgroup\$
    – c--
    Aug 1, 2022 at 4:08
  • 2
    \$\begingroup\$ @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). \$\endgroup\$ Aug 1, 2022 at 5:14
  • 2
    \$\begingroup\$ @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.) \$\endgroup\$ Aug 2, 2022 at 22:57
  • 1
    \$\begingroup\$ (correction; gperf is for mapping strings (e.g. language keywords) to different integers in a 0..n-1 range, so normally is 1:1. But this problem isn't, you need 3 different characters to map to 'l', for example.) \$\endgroup\$ Aug 3, 2022 at 1:36
8
\$\begingroup\$

JavaScript (Node.js), 14 bytes

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

Try it online!

Input text is:

Hel0o, w1r2d!

lol

\$\endgroup\$
1
  • 2
    \$\begingroup\$ lol 𝅷𝅷𝅷𝅷𝅷𝅷 \$\endgroup\$ Aug 3, 2022 at 18:39
7
\$\begingroup\$

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
\$\endgroup\$
8
  • \$\begingroup\$ Very nice answer, can you please also include description how this formula was created or include link for tool or guide which was used? \$\endgroup\$
    – Jiří
    Jul 31, 2022 at 22:31
  • \$\begingroup\$ you could also do 23+c*c%202%97 with "4=n6,8.AT\I' (found with brute-force) \$\endgroup\$
    – naffetS
    Jul 31, 2022 at 23:11
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – naffetS
    Jul 31, 2022 at 23:28
  • 1
    \$\begingroup\$ @Jonah You need to write a function from character to character, not an entire string. \$\endgroup\$ Aug 1, 2022 at 0:24
  • 2
    \$\begingroup\$ @Jonah And you must do so with 13 distinct characters, which is what makes the challenge interesting. \$\endgroup\$
    – Arnauld
    Aug 1, 2022 at 0:27
7
\$\begingroup\$

Python, 31 bytes

lambda c:c.translate('ll !o'*8)

Attempt This Online!

Input characters to use are Hel#o, w"r$d!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That's a really clever use of translate; I didn't know that was possible! \$\endgroup\$ Jul 31, 2022 at 23:16
  • 2
    \$\begingroup\$ @Adam just recycling an old idea... \$\endgroup\$
    – loopy walt
    Jul 31, 2022 at 23:23
7
\$\begingroup\$

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.

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

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!"),
                        #  jump to the end, to leave the character unchanged.
    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.
\$\endgroup\$
6
\$\begingroup\$

05AB1E, 7 5 bytes

∞'Ÿ™‡

Uses H2345, world! as 13 characters.

Try it online or verify each ASCII character separately.

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".

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

Retina 0.8.2, 7 bytes

T`d`o-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.)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ So that I remember why I included the -, the o is normally magic in Retina transliteration, but it's not magic when used in a range. Preceding it with a \ would also have made it nonmagic, but I guess - is the more... magic way to do it. \$\endgroup\$
    – Neil
    Jan 17 at 0:39
4
\$\begingroup\$

Vyxal, 6 bytes

kakH¡Ŀ

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

     Ŀ # Transliterate
ka     # Lowercase alphabet
  kH¡  # To corresponding chars of "Hello, world!"
\$\endgroup\$
1
  • \$\begingroup\$ @aketon Sentence case, the "w" has to be lowercase \$\endgroup\$
    – emanresu A
    Aug 2, 2022 at 23:49
4
\$\begingroup\$

Bash, 9 bytes

tr 012 ol

Takes Hel1o, w0r2d!

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ −1 byte: tr 213 ol \$\endgroup\$ Aug 1, 2022 at 3:04
4
\$\begingroup\$

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)

enter image description here

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

C (clang), 23 bytes

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

Try it online!

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

C++ (clang), 22 bytes

[](int&c){c^=656%c%7;}

Port of the above.

Try it online!

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

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].

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ Jul 31, 2022 at 23:21
3
\$\begingroup\$

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

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

K (ngn/k), 26 21 bytes

"Hello world!"`i$-97+

Down 5 bytes thanks to Razetime

A port of Adam's Python answer. Takes in [a..l].

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ "Hello world!"`i$-97+ \$\endgroup\$
    – Razetime
    Aug 9, 2022 at 12:42
0
\$\begingroup\$

Charcoal, 9 bytes

⭆S§⁺ι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.

 S          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)
\$\endgroup\$
0
\$\begingroup\$

Knight, 13 bytes

O-=cP%%954c 3

Port of @dingledooper's answer.

Knight, 16 bytes

O+28%%^P2 389 96

Port of @Arnauld's answer.

\$\endgroup\$
0
\$\begingroup\$

Headascii, 45 bytes

-----[]]]]]--[]]]][U-]()R-:-()R--:--()R-:R;P!

Takes a character and prints it, except for m, n, and p, which map to l, l, and o specifcially. Full chosen string therefore is Helmo, wprnd!

Try It In Headass!, which is the same except that it takes character codes as input and returns character codes. Or, run it in Headascii here by pasting this in:

erun("-----[]]]]]--[]]]][U-]()R-:-()R--:--()R-:R;P!","x")

and replacing x with the character input of your choice.

Or verify the whole thing at once like this lol:

> hellowarp="-----[]]]]]--[]]]][U-]()R-:-()R--:--()R-:R;P!"
'-----[]]]]]--[]]]][U-]()R-:-()R--:--()R-:R;P!'
> verify=x=>erun(hellowarp,x)
[Function: verify]
> "Helmo, wprnd!".split("").forEach(verify)
H
e
l
l
o
,
 
w
o
r
l
d
!

Explanation

-----[]]]]]--[]]]][U-]()R-:-()R--:--()R-:R;P!  full program
-----[]]]]]--[]]]][                            r2 = -108
                   U                           i = input (charcode)
                   U-]()                       if(i - r2 - 1 == 0){       // m
                        R-:               ;P!    print i - 1              // m - 1 == l
                          :-()                 }else if(i - r2 - 2 == 0){ // n
                              R--:        ;P!    print i - 2              // n - 2 == l
                                 :--()         }else if(i - r2 - 4 == 0){ // p
                                      R-: ;P!    print i - 1              // p - 1 == o
                                        :      }else{
                                         R;P!    print i
                                               }
\$\endgroup\$
1
  • \$\begingroup\$ its possible a modulo approach could sneak right under this but i couldnt get it to work so far \$\endgroup\$ Sep 17 at 1:31

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.