Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Write a program that produces an output such that:

  1. At least three distinct characters appear.
  2. The number of occurrences of each character is a multiple of 3.

For example, A TEA AT TEE is a valid output since each of the 4 distinct characters, A, E, T and (space), occurs 3 times.

Of course, a challenge about the number 3 needs to have a third requirement. So:

  1. The program itself must also follow the first two requirements. (This means your program will be at least 9 bytes long.)

You must write a full program, not a function. Be sure to show your program's output in your answer.

Also, to keep things interesting, you are highly encouraged:

  • not to use comments to meet requirement 3 if you can help it
  • to produce output that isn't just a string repeated 3 times
  • to make the output different from the program itself (for languages that can automatically output the contents of its own program, you can contribute to this community wiki).

This is . Shortest code in bytes wins.

share|improve this question
4  
Do newlines count (as a distinct character) ? – zeppelin Jan 25 at 19:05
4  
Are programs that consist entirely of literals allowed? (There are a lot of languages where 123123123 will work, as currently written.) – ais523 Jan 25 at 19:09
1  
@zeppelin Yes, newlines count as a distinct character. – darrylyeo Jan 25 at 19:58
2  
What I mean to ask is, can a program output e.g. abcabcabc with a trailing newline? – ETHproductions Jan 25 at 20:18
1  
@ETHproductions Ah, I see. No, that is not allowed. Three trailing newlines would be acceptable, however. – darrylyeo Jan 25 at 20:19

43 Answers 43

Brain-Flak, Flakcats, Brain-Flueue, Brain-Flak Classic and Miniflak, 18 bytes

Proven optimal!

((([()][()][()])))

Try it online!

Explanation

Brain-Flak, Brain-Flueue and Miniflak

   ([()][()][()]) Push -3
  (              ) Copy
 (                ) Copy

This prints:

-3
-3
-3

(There is a trailing newline)

Brain-Flak Classic

Brain-Flak Classic is the original version of Brain-Flak and has some important differences from modern Brain-Flak. In BFC [...] prints its contents rather than negating it.

   [()] Print 1
       [()] Print 1
           [()] Print 1
  (            ) Push 3
 (              ) Push 3
(                ) Push 3

At the end of executing the contents of the stack (3 3 3) is printed.

This prints:

1
1
1
3
3
3

(There is a trailing newline)

Flakcats

Flakcats is quite different from the other 4 flaks and I am surprised that this works in Flakcats. The three operators here are nearly the same as the ones that Brain-Flak uses.

The main difference in this particular program between Flakcats is the (...) operator which in Flakcats is equivalent to ([{}]...) in Brain-Flak. This however does not make a difference to us because it picks up zeros and thus operates much in the same way that Brain-Flak does.

Here is that program compiled into Brian-Flak:

([{}]([{}]([{}][()][()][()])))

This prints:

-3
-3
-3

(There is a trailing newline)

Proof of Optimality in Brain-Flak and Miniflak

This is not a formal proof, but rather an informal proof that would have to be expanded to be made more rigorous

Because of the restrictions that Brain-Flak programs must be a balanced-string and the program length must be a multiple of 3 any valid submission must be a multiple of 6 in length. This means any solution smaller than 18 must be length 12.

Because of the outputs trailing newline the final height of the stack must be a multiple of three or we will break the restrictions on output.

Any valid submission of length 12 must have 2 types of braces (having less would break the restrictions on number of distinct characters and more would mean more than 12 characters). Since the program produces output it must have a push.

This leaves us to select our other set of braces. The options are:

<...>/<>

This fails because we need to generate "value" in order to create any number other than zero we must give up a () to create a one which makes it impossible to push more than two times.


[...]/[]

This fails for the same reason the last failed. The square braces are really bad at making value. The [] monad can create value but we need to push numbers first and we then don't have enough parens left over to push three times.


{...}/{}

This one is promising, we could create a loop and use one () to push multiple times, but alas it is not possible.

In order for the loop to end there must be a zero on the stack at some point and in order for us to have the correct output we must have something other than zero on the stack at the end of the program. Since we have neither [] nor <> the zero at the end of the loop must be a implicit zero from the bottom of the stack. This means the loop cannot add any new numbers to the stack making it useless.


Since none of the brace choices can create a program of length 12 none can exist.

Since Miniflak is a subset of Brain-Flak any shorter Miniflak program would also be a shorter Brain-Flak program and thus does not exist.

Proof of Optimality in Brain-Flueue

Brain-Flueue is a joke language based off of Brain-Flak. The two are so similar their interpreters are identical everywhere but two lines. The difference between the two is, as their names suggests, Brain-Flueue stores its data in queues while Brain-Flak stores its data in stacks.

To start we have the same restrictions on program size created by Brain-Flak, thus we are looking for a program of size 12. In addition we are going to need a (...) in order to create any output and another pair. the <> and [] pairs do not work in Brain-Flueue for the exact same reason they do not work in Brain-Flak.

Now we know that our program must consist of the characters ((())){{{}}}.

Via the same methods used in the previous proof we can demonstrate that there must be a loop in the final program.

Now here is where the proofs differ, because Brain-Flueue operates across queues rather than stacks the program can exit a loop with values on the queue.

In order to exit the loop we will need a zero in the queue (or an empty queue but if the queue is empty we get the same problem as Brain-Flak) this will mean that we will have to open our program with ({}) to create the zero. We will need a push inside of the loop to push the necessary number of items to the queue. We will also need to push a non zero number before the loop so that we can enter the loop at all; this will cost us at absolute minimum (()). We have now used more parens than we have.

Thus there is no Brain-Flueue program to do the task that is 12 bytes, and furthermore there our program is optimal.

Optimal solution in Flakcats and Brain-Flak Classic

The following solution is optimal in Flakcats and Brain-Flak Classic.

((([][][])))

Explanation

    [][][] -3
 (((      ))) push 3 times

Alternative 24 byte Brain-Flak solutions

(<((<((<(())>)())>)())>)

Try it online!

((<((<((<>)())>)())>)())

Try it online!

((((((()()()){}){}){})))

Try it online!

share|improve this answer
9  
+1 for proof(s) of optimality! – 1000000000 Jan 25 at 21:18
    
+1 for the proof as well. – Alex L. 2 days ago

C#, 114 111 118 102 bytes

If we don't care about using proper words: (102 bytes)

class CCcddiilMMmmnrrSSsttvvWWyy{static void Main(){{System.Console.Write(("A TEA AT TEE"));;;}}}///".

If we care about proper words: (120 bytes)

class erebeWyvern{static void Main(){int embedWildbanana;{System.Console.Write(("A TEA AT TEE"));;}}}///CC Myst mvcSMS".

My original submission - case insensitive: (113 bytes)

class EreBeWyvern{static void Main(){int embedwildbanana; {System.Console.Write(("A TEA AT TEE"));;}}}/// vyt".

I know the comment isn't really in the spirit of the CG, but it's all I could come up with in a limited amount of time, I'll see if I can improve it through the day. Surely I must get at least some bonus points for the nod to being adventurous.

Edit: Thank you to roberto06 for catching the missing letters!

share|improve this answer
1  
I'm a C++ guy, not C#, but can you not just wrap the Write call in {()} without affecting it? – Sparr Jan 25 at 20:01
1  
You might be able to put the parentheses around the argument, rather than the call as a whole. – ais523 Jan 25 at 20:11
4  
Brownie points for the wild banana. – darrylyeo Jan 25 at 21:07
6  
Nice! I like the variable names. +1 – Mistah Figgins Jan 26 at 1:53
1  
You should remove V from your comment, and add vyt, since Vis only present there while v is present twice (typo, I guess), y is also present twice, and t is present 5 times. See here. Nevertheless, awesome job ! – roberto06 2 days ago

JavaScript, 36 33 bytes

alert(((alert|alert||333111222)))

Alerts 333111222. This works because | converts both of its operands to 32-bit integers, and any value that doesn't look anything like an integer (e.g. the function alert) gets converted to 0. 0|0 is 0, so the || operator returns its right operand, or 333111222

A few more interesting versions:

(a="(trelalert)\\\"")+alert(a+=a+=a)

Outputs (trelalert)\"(trelalert)\"(trelalert)\".

A solution using .repeat would be the same length, thanks to the shared aert:

alert("(trpp.all)\\\"".repeat(3.33))

which outputs (trpp.all)\"(trpp.all)\"(trpp.all)\".

Taking advantage of the extra backslashes to get rid of l and p almost works:

a\x6cert("(trax.cc)\"".re\x70eat(6.00677))

This one outputs (trax.cc)"(trax.cc)"(trax.cc)"(trax.cc)"(trax.cc)"(trax.cc)".

share|improve this answer
    
Nice abuse of decimals! – darrylyeo Jan 25 at 20:46
2  
@darrylyeo Heh, thanks. I could've easily stuck those in the string, but that wouldn't be as fun ;-) – ETHproductions Jan 25 at 20:46
    
Thought I'd let you know you were topped by an ES6 answer. – darrylyeo yesterday

Polyglot of purely literal answers, 9 bytes

333111222

This is a community wiki post for collecting answers that are just a literal that the language in question prints out automatically. Because it's a community wiki, feel free to edit it to add more languages where it works.

This program works in:

  • PHP
  • HTML (arguably not a language)
  • Jelly (and M)
  • 7 (more interesting, because the program's interpreted as both data and code; the first 3 prints the data, the rest of the program is useless stack manpulation)
  • CJam
  • Japt
  • Carrot
  • R (the R display also outputs [1] as metadata)
  • RProgN
  • Actually (though it actually prints 2\n2\n2\n1\n1\n1\n3\n3\n3\n)
  • ///
  • Noodel
  • TI-Basic
  • SimpleTemplate

Ignoring the final newline, this is valid in quite a few more languages:

Most links go to Try It Online!

share|improve this answer
3  
This prints 2\n2\n2\n1\n1\n1\n3\n3\n3\n in Actually, which is perfectly valid. Should that be added to the post? – ETHproductions Jan 25 at 20:10
    
@ETHproductions I actually think Actually should be added to the post since it uses the same code ;) – Kritixi Lithos Jan 25 at 20:12
    
As the person who made Actually, I agree that it belongs in this post. The newlines don't really make a difference. – Mego Jan 26 at 3:21
    
This also works on the language I've written: SimpleTemplate. It will compile it into an over-kill echo '333111222'; (in PHP) but it works. – Ismael Miguel 2 days ago
    
@IsmaelMiguel: Feel free to edit it into the answer yourself. It's a community wiki, that's how they work. – ais523 2 days ago

Jelly, 9 bytes

**‘‘‘888*

A full program which prints 700227072, which is 888 cubed.

TryItOnline!

How?

**‘‘‘888* - Main link: no arguments
          - implicit L=R=0
*         - power       A = L ^ R = 1
  ‘       - increment   B = L + 1 = 1
 *        - power       C = A ^ B = 1
   ‘      - increment   D = C + 1 = 2
    ‘     - increment   E = D + 1 = 3
     888  - literal     F = 888
        * - power           F ^ E = 700227072
share|improve this answer
    
888^3 is 700227072? That's very clever, perhaps other languages can use this trick. – ETHproductions 2 days ago

CJam, 9 bytes

10,10,10,

Outputs 012345678901234567890123456789

Try it online!

Explanation

10,       The range from 0 to 9
   10,    The range from 0 to 9
      10, The range from 0 to 9
share|improve this answer
    
doesn't 3,3,3, work for 7 bytes? – chim 2 days ago
    
Ah! yeah, the third requirement :) – chim 2 days ago

Perl 6, 15 bytes

.say;.say;.say;

Prints six distinct characters, three times each:

(Any)
(Any)
(Any)

Try it online!

How it works

  • A bare method call operates on the current topic, $_.
  • $_ starts out as the type object of type Any, which say prints as (Any).
share|improve this answer
1  
Now I've got a Yeah Yeah Yeahs song stuck in my head. – Stuart P. Bentley 2 days ago

Microscript II, 9 bytes

{{{~~~}}}

Explanation: Creates a code block, but doesn't invoke it. When execution ends, the contents of the main register (IE this code block) are implicitly printed.

share|improve this answer
    
Have you posted something similar in the Quine challenge? – wizzwizz4 Jan 25 at 19:08
    
@wizzwizz4 {} would technically be a quine, but I don't think it meets our definition of a "proper quine". The program "q"q (which I did submit to the quine challenge) does, however. – SuperJedi224 Jan 25 at 19:09
    
@wizzwizz4: That wouldn't be a proper quine, because each character represents itself. – ais523 Jan 25 at 19:09

PKod, 9 bytes

sonsonson

Outputs: 111222333


Explanation:

Background: PKod has only one variable that you mess with, with the code
This variable starts with default value of 0

s  -  Add next char to the variable, and jump over it. 
n  -  Print numeric value of variable

o has ascii char code "111" in decimal. Thus s adds 111 to the variable, then prints the number. First "son" makes it 111 and prints 111. Next makes it 222 and prints 222, lastly makes it 333 and prints 333

share|improve this answer

Python 2, 36 30 bytes

Since a trailing newline isn't allowed, this is probably as short as it can get:

print"\\\""*3;print;print;3**3

Try it online

Outputs \" three times, followed by three newlines.


The below programs don't count the trailing newline, so they're not valid.

27 bytes:

print"""printprint"""*3*3*3

Prints 54 of each character in print.

Try it online


Same length, shorter output:

print"""printprint*3*3"""*3

Outputs printprint*3*3printprint*3*3printprint*3*3


24 bytes:

print~9;print~9;print~9;
share|improve this answer
    
You could do print"""printprint*3*3"""*3 for a much shorter output ;-) – ETHproductions Jan 25 at 20:05
    
Could you not do print 123;print 123;print 123; for the naive solution? – ETHproductions Jan 25 at 20:33
    
OP has clarified that the single trailing newline is not allowed (see comments). – FlipTack Jan 25 at 20:36
1  
@ETHproductions Sure. It depends how naive we're being. – mbomb007 Jan 25 at 20:43
2  
Even more naive: print~9;print~9;print~9; – Sp3000 Jan 25 at 22:35

Ruby, 12 bytes

p$@;p$@;p$@;

outputs

nil
nil
nil

Try it online!

To fulfill the second "encouraged" criterion, I need 15 characters:

p 1;p 3;p 1133;

produces

1
3
1133

Try it online too!

share|improve this answer

PHP, 33 bytes

<?=($s="<?;<?=;'".'"').($s).($s);

Opted for something more interesting than the 9-byte program with no PHP tag.

Outputs <?;<?=;'"<?;<?=;'"<?;<?=;'"

Try it online!

share|improve this answer

Batch, 36 21 bytes

@echo
@echo
@echo

Outputs

ECHO is on.
ECHO is on.
ECHO is on.

Edit: Saved 15 bytes thanks to @P.Ktinos.

share|improve this answer
1  
Wouldn't 3 alone echo's work? echo\necho\necho - replace \n with a new line - it looks valid to me image.prntscr.com/image/ef96173169eb41168815203387d4975b.png , makes it 14 bytes. – P. Ktinos Jan 25 at 20:56
    
Outgolfed: codegolf.stackexchange.com/a/108180/63033 – P. Ktinos Jan 25 at 22:37

V, 9 bytes

12i12i12i

Try it online!

Outputs 12i 24 times:

12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i12i

Vim, 12 bytes

12i12i12i<ESC><ESC><ESC>

Outputs the same as the V answer

share|improve this answer

C, 111 bytes

(Note how the byte count is also the three same numbers. Wow. You can't do more meta than that.)

#include<stdio.h>
#define b "<acdhlmoprsu>."
#define t "en"
main(){{{printf(("<acdhlmoprsu>." b b t t t));;;}}}

Prints:

<acdhlmoprsu>.<acdhlmoprsu>.<acdhlmoprsu>.enenen
share|improve this answer

C, 66 Bytes

main(i){{for(i=0;i<3;i++){printf("""poop+fart=<3<3at0m=m0n""");}}}

Output

poop+fart=<3<3at0m=m0npoop+fart=<3<3at0m=m0npoop+fart=<3<3at0m=m0n    

Old Version 72 Bytes

main(i){for(i=0;i<3;i++){printf("poop+fart=<3<3 at {\"0m=m0\"}" "\n");}}
share|improve this answer
1  
Not only your code is much smaller than what I did, but it also contains real bits of poop and farts. Bravo. – dim 2 days ago

stacked, 24 bytes

''''   'putput'3*3*3*put

Try it online! Outputs 54 each of p u and t.

share|improve this answer

SMBF, 18 15 bytes

<+...+...+...<<

Try it online

Output:

===>>>???
share|improve this answer
    
I think you can move two < to the end to drop the w. – Martin Ender Jan 25 at 20:24

Ouroboros, 12 bytes

n11n222n1(((

Outputs 011222 three times. Try it here.

How?

In Ouroboros, each line of code represents a snake swallowing its tail. Execution proceeds from the head (start) of the line to the tail (end), and then loops back to the head. The ( instruction swallows characters from the tail. If the instruction pointer is swallowed, execution halts.

The n command outputs a number. Popping the empty stack gives zero, so the first n prints 0. The next prints 11, and the third prints 222.

Now we come to 1(: push 1 and swallow that many characters. The final paren is swallowed. The next-to-last paren does nothing1, and execution loops back to the beginning.

The second time through, the next-to-last paren is swallowed and we loop again. The third time, the third-to-last paren is swallowed. This swallows the IP, and the program ends.

1 The stack is empty, so it swallows 0 characters.

share|improve this answer
1  
"...each line of code represents a snake swallowing its tail" (⊙.☉)7 – Luigi 2 days ago
    
@Luigi en.wikipedia.org/wiki/Ouroboros (but yeah, that is a bit trippy) – DLosc 2 days ago

Retina, 18 12 bytes

Byte count assumes ISO 8859-1 encoding.


aaa¶¶ccc

¶

prints


a
a
a




c
c
c


That's 12 linefeeds.

Try it online!

share|improve this answer

PHP, 24 bytes

<?=111?><?=222?><?=333?>

Outputs

111222333

Simply three echos echoing out numbers.

share|improve this answer
1  
What does this output? How does it work? – DJMcMayhem Jan 25 at 22:59

Befunge 93, 9 bytes

...,,,@@@

TIO

Prints 0 0 0 (Trailing space, followed by 3 null bytes)

Because Befunge's stack is padded with 0s, we can print both the ASCII character with that value, and the integer itself. Because Befunge automatically prints a space after an integer, we are left with 3 distinct characters.

. prints 0 (trailing space), , prints a null byte, and @ ends the program

share|improve this answer

dc, 9 bytes

zfzfdzddf

Try it online!

Output:

0
1
0
3
3
3
1
1
0

(The program has no newlines; the output has 9 newlines.)


The new answer above is better than my old answer of the same length, because the new answer meets OP's suggestion that the output ideally shouldn't be the same string repeated 3 times. Here's the old answer, which I'll keep here so that @mbomb007's comment continues to make sense!

_A_A_Annn

Try the old answer online!

Output is

-10-10-10

(no newlines in either the program or the output)

share|improve this answer
5  
♫ Barbara _A_A_Annn – mbomb007 Jan 25 at 21:03
    
@mbomb007 LOL!! – Mitchell Spector Jan 25 at 21:47

Bash + coreutils, 15 9 bytes

id;id;id;

Try it online!

Sample Output:

uid=1000 gid=1000 groups=1000 context=system_u:unconfined_r:sandbox_t:s0-s0:c19,c100,c173,c211
uid=1000 gid=1000 groups=1000 context=system_u:unconfined_r:sandbox_t:s0-s0:c19,c100,c173,c211
uid=1000 gid=1000 groups=1000 context=system_u:unconfined_r:sandbox_t:s0-s0:c19,c100,c173,c211

(If you try this out, it will print your uid, gid, etc., 3 times.)


If you want to avoid repeating the same string 3 times (and also have the same output for everybody, unlike my first answer), the best I've found for bash + Unix utilities is 15 bytes long:

dc<<<cczdzfzfdf

Try this second version online!

Output:

2
0
0
3
2
0
0
3
3
2
0
0

(No newlines in the program, 12 newlines in the output.)

share|improve this answer

05AB1E, 9 bytes (I guess you could say this was a piece of PI)

-0 bytes thanks to Emigna/ETHProductions, made the solution more correct.

žqžqžq???

Alternate versions:

ž[g-Q]ž[g-Q]ž[g-Q]???

[g-Q] - Can put any letter a-Q here, as long as they all match (see below).

Try it online!

Explained:

PI,PI,PI,SORT,JOIN,SORT,JOIN,SORT,JOIN.

Result:

...111111222333333333444555555555666777888999999999

The reason it is only 9 bytes is because you don't need the sorts, I just put them in to help illustrate.

Result w/o { in the code:

3.1415926535897933.1415926535897933.141592653589793


Alternative Renditions:

The following commands can be used in place of PI:

ž 23  > žg       push current year
        žh       push [0-9]
        ži       push [a-zA-Z]
        žj       push [a-zA-Z0-9_]
        žk       push [z-aZ-A]
        žl       push [z-aZ-A9-0_]
        žm       push [9-0]
        žn       push [A-Za-z]
        žo       push [Z-Az-a]
        žp       push [Z-A]
        žq       push pi
        žr       push e
        žu       push ()<>[]{}
        žy       push 128
        žz       push 256
        žA       push 512
        žB       push 1024
        žC       push 2048
        žD       push 4096
        žE       push 8192
        žF       push 16384
        žG       push 32768
        žH       push 65536
        žI       push 2147483648
        žJ       push 4294967296
        žK       push [a-zA-Z0-9]
        žL       push [z-aZ-A9-0]
        žM       push aeiou
        žN       push bcdfghjklmnpqrstvwxyz
        žO       push aeiouy
        žP       push bcdfghjklmnpqrstvwxz
        žQ       push printable ASCII character set (32-128)
share|improve this answer
2  
Get out for the pun; for the trick though get one :) – geisterfurz007 2 days ago
    
Nice one! I don't think žv, žw, or žx are valid though as they each have only 2 distinct digits. – ETHproductions 2 days ago
    
Hmm, what's the point of the backwards character classes? – ETHproductions 2 days ago
    
@ETHproductions ahhh, forgot about at least 3, was just PI at first when I wrote it. What do you mean backwards character classes? – carusocomputing 2 days ago
1  
Note that this is only valid if you don't count the newline that's implicitly printed. That could easily be fixed by replacing JJJ with ??? though. – Emigna yesterday

Forth, 21 bytes

 1 1 1 . cr . cr . cr

Program contains 3 of each of 1.cr, and 9 spaces. Output contains 3 ones, 3 spaces, and 3 newlines.

Try it online

Output:

Each 1 has a space after it.

1 
1 
1 

share|improve this answer

Pip, 9 bytes

PzPzPzuuu

Outputs the lowercase alphabet three times, each time followed by a newline. Try it online!

Explanation

The naive attempt

P12P12P12

actually prints 12 four times. That's because Pip autoprints the result of the last expression in the program. P12 prints 12, but it is also an expression that evaluates to 12. So an extra 12 is printed.

We can avoid this by making the last expression in the program evaluate to nil. When nil is printed, it produces no output--not even a newline. The variable u is preinitialized to nil. Repeating it three times is fine, just adds a couple no-ops.

share|improve this answer

Retina, 27 bytes


aaabbb
S\`
S\`
S\`
\`\`\`

Try it online

The output is 150 bytes, containing 12 a, 12 b, and 126 newlines.

share|improve this answer

Del|m|t, 15 bytes

TIO

8 9 8 9 8 9 555

Prints 0 0 0 (with a trailing space) (there are null bytes after every space, including the trailing one

Because the stack is padded with 0s, we can just print the ASCII value and character, because printing the value also prints a space. 555 ends the program.

share|improve this answer

V, 9 bytes

¬ac¬ac¬ac

Try it online!

This outputs:

ababcabcc

This is pretty straightforward. Each ¬ is the "range operator", that takes two character arguments and inserts each character in that range. So ¬ac inserts abc. Repeating this three times causes some weird issues with the layout of the output (which is why they're not in order) but thankfully this doesn't matter for this particular answer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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