7
\$\begingroup\$

i.e each byte appears in a different ANSI shell colour.

Rules

  • Resetting the color of the shell after output is not required
  • Must be output in STDOUT or the primary colour text output of your language.
  • Must be done programmatically (iterating through each color and letter of the string induvidually) instead of just writing a fixed string to shell like the reference command.
  • Must output the same as the reference command: echo -e '\e[30mH\e[31me\e[32my\e[33m!\e[34mH\e[35me\e[36my\e[37m!\e[90mH\e[91me\e[92my\e[93m!\e[94mH\e[95me\e[96my\e[97m!' on a recent version of bash (I am using 4.3 installed via Homebrew on OS X) i.e like this: Reference output Obviously colours will vary based on terminal colour scheme.

Scoring

This is , so lowest bytes wins.

\$\endgroup\$
4
  • 9
    \$\begingroup\$ "Must be done programmatically (with iteration) instead of just writing a fixed string to shell like the reference command." Restrictions like this are highly problematic (several other answers on that post also apply). For a start it's not clear how much iteration is enough: can I have a loop with only two iterations that prints the first half and then the second half? Can I have a loop with ten iterations that prints the string on the first run and does nothing at all on the other nine? \$\endgroup\$ Jul 10, 2016 at 11:33
  • 11
    \$\begingroup\$ If you're worried that printing the fixed string is always going to beat printing the output in a loop, then that usually indicates a problem of the challenge. Either, loops are going to beat fixed-output answers, in which case the restriction is unnecessary, or the fixed-output answers will be shorter in which case the restriction seems like a very artifical patch to a fundamental problem of the challenge. \$\endgroup\$ Jul 10, 2016 at 11:35
  • 3
    \$\begingroup\$ Is the example order of colors binding, or may we use any order as long as all 16 are used? \$\endgroup\$
    – Adám
    Jul 10, 2016 at 18:01
  • \$\begingroup\$ Can I write "Heyyyyy..."? \$\endgroup\$ Aug 2, 2016 at 3:25

14 Answers 14

7
\$\begingroup\$

Bash, 62

h=y!He
for i in {3,9}{0..7};do printf \\e[${i}m${h:i%4:1};done

Must be run from an actual script file. If you want to try it from the command line, you'll need to escape the !, i.e. h=y\!He

Online.

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

C, 126 bytes

#define a"\x1B[%d;%dm"
#define b j,i++
i,j;main(){for(;i=30,j<2;j++)printf(a"H"a"e"a"y"a"!"a"H"a"e"a"y"a"!",b,b,b,b,b,b,b,b);}

output may vary depending on your compiler, linker, operating system, and processor

Ideone doesn't have colour output, so have a screenshot from my phone:

Looks like this on my phone

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

bash, 101 72 bytes

29 bytes saved with a trick from @DomHastings.

s='y!He'
for i in {30..37} {90..97}
do
printf "\e["$i"m"${s:$i%4:1}
done

If I hadn´t scrambled the string, this would have been 105 (($i+2)%4 instead of $i%4), just as my
previous approach, 105 bytes

function p
{
s='Hey!'
for i in {0..3}
do
let r=i+$1
printf "\e["$r"m"${s:$i:1}
done
}
p 30;p 34;p 90;p 94
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hooray for bash! Looks like you can use one brace expansion and negate the need for the if (... for i in {30..37} {90..97} ...) for 72! \$\endgroup\$ Aug 15, 2017 at 11:48
2
\$\begingroup\$

dc, 72 71 bytes

Fi4C 2:c6B 3:c81 0:c23 1:c28si20[d1CP61Pn74Pd4%;cP1+dli>P]dsPx68si60lPx

Of course the escape codes won't render right on TIO, but you can still try it online!

First we set our input to base 15, this is solely for the sake of saving a byte. Make an array, c, of the code points for 'y', '!', 'H', and 'e'. We'll exploit the fact that 30 mod 4 == 90 mod 4; for both of our sets of color values, value mod 4 starts at 2, so that's where we put our 'H' in the array. From there it's just a matter of setting up a loop that runs until we hit the value stored in i. We have our color code on the stack, we print the necessary escape codes, print the stack value, mod 4 and print that value from the array c, then increment the stack. We run through this once starting from 30 w/ i set at 38, then once more starting from 90 w/ i set at 98.

Shaved off one byte by doing everything in base 15; decimal version below for slightly more clarity:

72 2:c101 3:c121 0:c33 1:c38si30[d27P91Pn109Pd4%;cP1+dli>P]dsPx98si90lPx
\$\endgroup\$
1
\$\begingroup\$

J, 59 bytes

echo,(u:27 91),"1(16$'Hey!'),.~'m',.~,/30 90":@+/i.8
exit''

Save it as a script to run using J. It will print the output to stdout with the escaped colors.

\$\endgroup\$
1
  • \$\begingroup\$ Doesn't this do only one single output of the entire string? \$\endgroup\$
    – Adám
    Jul 10, 2016 at 18:14
1
\$\begingroup\$

Octave, 78 bytes

c=[b=[a="\x1b[30m"' a a a;'Hey!'] b];c(4,:)+=0:7;d=c;d(3,:)+=6;disp([c d](:)')

Usage:

If the code is in a file hey.m:

$ octave hey.m
Hey!Hey!Hey!Hey!
\$\endgroup\$
0
1
\$\begingroup\$

Pyke, 25 bytes

"Hey!"4*F\mo8.D6*3+"["s_

Try it here!

                           - o = 0
"Hey!"4*                   -   "Hey!Hey!Hey!Hey!"
        F                  -  for i in ^:
                           -   stack = [i]
         \m                -   stack.append("m")
           o               -    o += 1 
            8.D            -   stack.extend(divmod(^, 8))
               6*3+        -   stack[-1] = stack[-1]*6+3
                   "["     -   stack.append("[\x1b")
                        s  -   stack = sum(stack)
                         _ -  stack = reverse(stack)
                           - print "".join(^)
\$\endgroup\$
1
\$\begingroup\$

Python 3, 81 bytes

print(*['\33[%sm%s'%(i,'Hey!'[i%4-2])for i in range(30,98)if not 37<i<90],sep='')

A full program that prints to STDOUT.

How it works

for i in range(30,98)...       For all possible colour codes i in [30,97]...
...if not 37<i<90              If i is in the desired range [30,37] or [90,97]...
'\33[%sm%s'%(i,'Hey!'[i%4-2])  ...create a string of the form
                               '\033[{colour code}m{current string character}'...
[...]                          ...and store all strings in a list X
print(*...,sep='')             Print all strings in X with no separating space

Try it on CodingGround

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

SmileBASIC, 39 bytes

FOR I=0TO 15COLOR I?("Hey!"*4)[I];
NEXT
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog), 38 41 bytes

Now returns right result too!

∊(⍕¨∊29 89∘.+⍳8){'\e[',⍺,'m',⍵}¨16⍴'Hey!'

Try it online!

16⍴'Hey!' cyclically reshape the string to length 16

(){ apply the below anonymous function to each letter pairing it with the corresponding element for this list as left argument:

⍳8 1 through 8
29 89∘.+ addition table with these numbers vertically and those horizontally
ϵnlist (flatten)
⍕¨ format (stringify) each

'm',⍵ prepend an "m"
⍺, prepend the left argument
'\e[', prepend the string

ϵnlist (flatten)

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

4, 392 bytes

3.600486014960250603516045260553606546075560957610276119161318012111362072623336241062530021112402211255105115035005125205105115035015125215105115035025125225105115035035125235105115035045125205105115035055125215105115035065125225105115035075125235105115095005125205105115095015125215105115095025125225105115095035125235105115095045125205105115095055125215105115095065125225105115095075125234

Try it online!

Tip: use the -verbose flag.

\$\endgroup\$
2
  • \$\begingroup\$ Is it this language? esolangs.org/wiki/4 \$\endgroup\$ Aug 16, 2017 at 3:44
  • \$\begingroup\$ @JerryJeremiah yes \$\endgroup\$
    – Uriel
    Aug 16, 2017 at 8:38
1
\$\begingroup\$

Perl 5, 50 bytes

49 bytes code + 1 for -p. Note that \x1b is a literal escape character. Utilises empty input which TIO doesn't support, so a newline is provided and not used.

$_="Hey!"x4;s/./\x1b[@{[(30..37,90..97)[$-++]]}m$&/g

Try it online!

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

PowerShell, 41 Bytes.

0..15|%{Write-Host -n('Hey!'[$_%4])-f $_}

The day we get an alias for Write-Host is a good day.

enter image description here

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

JavaScript 134 bytes

<script>
  var a=0;
  var c=["H","e","y","!"];
  for(;a<3;){
    var b=-1;
    for(;++b<4;){
      document.write(<b style='color:rgb(0,0,"+(a*4+b)+"'>"+c[b]);
    }
  }
</script>

Golfed

<script>a=0;c=["H","e","y","!"];for(;a++<3;){b=-1;for(;++b<4;){document.write(<b style='color:rgb(0,0,"+(a*4+b)+"'>"+c[b]);}}</script>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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