50
\$\begingroup\$

yes, from coreutils, is 91 lines long. Many of them are comments, but that is still WAY too long.

Edit from September 2019: the source file grew over the past five years and is now 126 lines long.

Write a program that imitates yes:

  • outputting to stdout an infinite stream of "y\n"'s
  • there must be an option to stop it other than killing the process with SIGKILL: but SIGINT and SIGPIPE are fine
  • you are not allowed to use "y" or "\n" or their ASCII values (121, 0x79, 0171, 10, 0xA or 012)

Shortest answer wins.

Bonus:

  • subtract 10 from your code length, if you can receive a phrase in stdin and print it out instead of "y" (but still including the line-break).
\$\endgroup\$
  • 13
    \$\begingroup\$ "you are not allowed to use "y" or "\n"" -- should I read this as "You may not use y or \n inside of a string literal"? \$\endgroup\$ – apsillers Dec 17 '14 at 14:28
  • 12
    \$\begingroup\$ On a related note, GNU true.c is 80 lines long. \$\endgroup\$ – Paused until further notice. Dec 17 '14 at 22:32
  • 6
    \$\begingroup\$ @DennisWilliamson On a similarly related note, false.c is 2 lines long.... ;_; \$\endgroup\$ – LordAro Dec 18 '14 at 2:30
  • 6
    \$\begingroup\$ the coreutils yes takes an optional argument on the command line, not stdin. \$\endgroup\$ – Brian Minton Dec 18 '14 at 20:05
  • 7
    \$\begingroup\$ @MrLore: to pipe into other programs that might constantly ask for confirmation of the various things they are doing, so you dont have to sit there typing the ys yourself. \$\endgroup\$ – marcus erronius Dec 20 '14 at 16:57

65 Answers 65

1
\$\begingroup\$

Windows Batch: 24 bytes

A yes.bat file with this content will work on most Windows XP and later systems with default settings:

@echo %COMSPEC:~12,1%&%0

Else this slightly larger yes.bat file (34 bytes) will work in all cases:

@set "T=%~n0"
@echo %T:~0,1%
@%0

Can be made down to 13 bytes if the file is called y.bat instead of yes.bat, but I feel that's cheating:

@echo %~n0&%0
\$\endgroup\$
1
\$\begingroup\$

C89, 31 37 chars

main(){for(;;)printf("%c\n",'z'-1);}

Built on the idea of @LordAro, without filename requirements.

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

Fortran, 28

do1;1 print'(A1)',11**2;end

Compile with gfortran -ffree-form.

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

linux (Ba)sh, 13 to 26 (23 to 36 chars) (repeats 'y' or the input, like yes)

create an executable (+x) file, named as you like (ex: my_yes), containing one of those possible contents:

echo ${*:-y};exec $0 $*  #23 chars, score 13, but loses spaces, and will choke on many inputs

or

echo "${@:-y}";exec $0 "$@"  #27 chars, score 17, but will choke on some inputs

or

printf "%s\n" "${@:-y}";exec $0 "$@"  #36 chars, score 26, a tiny more robust, but "longer"

and then execute it with the arguments you want, it will repeat them ad-lib. (With no arguments, it repeats "y", as the yes command does too)

$ chmod +x my_yes
$ ./my_yes
y
y
y
[ctrl-C]

$ ./my_yes '  It looks ok!'
  It looks ok!
  It looks ok!
  It looks ok!
  It looks ok!
[ctrl-C]
\$\endgroup\$
  • \$\begingroup\$ some inputs that can make it choke on the first 2 : first argument starting with a "-", for example. (and many others) \$\endgroup\$ – Olivier Dulac Dec 18 '14 at 13:18
  • \$\begingroup\$ I use "exec" to ensure I don't bring the computer down very quickly with recursive bash (which 'yes' doesn't do, usually ^^) \$\endgroup\$ – Olivier Dulac Dec 18 '14 at 13:20
1
\$\begingroup\$

CFML, 247 bytes

This is more a "just for the heck of it" answer, as it's obviously hideously long (CFML earns no points for terseness); and I just realized it can't actually be killed without restarting the service, although it will indeed stop printing to the browser on an ESC character.

<html><body><cfflush><cfif isDefined("URL.i")><cfscript>a=URL.i;if(a is ""){a=chr(asc("x")+1);}while(1){writeOutput("#a#<br>");getPageContext().getOut().flush();}</cfscript><cfelse><form action="a.cfm"><input name="i"/></form></cfif></body></html>
\$\endgroup\$
1
\$\begingroup\$

C, 35 bytes

main(a,b){b='x'+1;for(;;)puts(&b);}

I don't know if this will compile correctly with all compilers on all platforms or not; if it doesn't work for you, let me know. I used several things here that I have never thought of using before. :)

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

Node.js, 53 chars (63 if you count node -e "")

update:

  • Reduced another 5 chars.
  • Silly extra char removed.
  • Reduced 2 chars, from 61 to 59.

The real yes does not uses stdin, but an argument, like:

yes n

Well, this answer works like that, it will print y\n over and over:

node -e "for(;;)console.log(process.argv[1]||34 .toString(36))"

And if you append an n after the line, it will print n\n instead:

node -e "for(;;)console.log(process.argv[1]||34 .toString(36))" n

No bonus, since I'm not using stdin.

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

QC 26 (26 - 10 = 16) bytes

(00600F014"0178)0101A01$14

The program requires an input on stdin because QC doesn't have a way to get arguments. Empty input just prints y.

Explanation

(00 read string into memory at address 00, address 00 is length, next bytes are the string itself
600F014 jump if values at 00 and F0, if not equal jump to address 14
"0178 Write 78(lowercase x) to address 01
)0101 increment value at address 01 by 01 which gives y
A01 print string and newline from address 01 until 00 is reached
$14 jump to address 14(print the next line)
\$\endgroup\$
  • \$\begingroup\$ Wait, It's decremented by 10, so it becomes 16 bytes, making this the ultimate answer! \$\endgroup\$ – Matthew Roh Feb 12 '17 at 6:58
1
\$\begingroup\$

Vitsy, 15 - 10 = 5 bytes

Because Vitsy waits for STDIN to be entered, I have made the bonus input come in as command line arguments.

zl)[bD*][:ZaOu]

Explanation:

z               Grab the entirety of the input stack and patch it on the current.
 l)[   ]        If the stack has no members...
    bD*         Push 11^2 to the stack (this is the ASCII value for 'y').
        [     ] Loop infinitely.
         :      Duplicate the current stack.
          Z     Push the entire contents of the stack to output, top first.
           aO   Push out a newline to the out.
             u  Flatten the top two stacks.
\$\endgroup\$
1
\$\begingroup\$

C(GCC) - 161 bytes

main(int argc,char**argv){if(argc>1){while(1){for(int i=0;i<argc-1;i++){printf("%s ",argv[i+1]);}puts("");}}else{char*c=malloc(1);c[0]='x'+1;while(1){puts(c);}}}

Uses malloc to allocate 1 byte and set its value 'x' + 1 If any input is provided as an argument(mulitple words work too) it will print it instead of y.

Ungolfed version:

main (int argc, char**argv) {
    if (argc > 1) {
        while (1) {
            for (int i=0; i < argc - 1; i++) {
                printf("%s ",argv[i+1]);
            }
            puts("");
        }
    } else {
        char* c = malloc(1);
        c[0] = 'x' + 1;
        while(1) {
            puts(c);
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

Ohm, 17 - 10 = 7 bytes

IDö?.x1+;Ω
D,∞

Explanation:

IDö?.x1+;Ω  ■Main link
I           ■Input
 Dö?    ;   ■If(!input){
    .x1+    ■  Push 'x' and increment
            ■}
         Ω  ■Call link below

D,∞         ■Helper link
  ∞         ■while (true){
D,          ■  duplicate and print
            ■}
\$\endgroup\$
1
\$\begingroup\$

Keg, 6 bytes

On particular note, this uses an old version of Keg.

{\
y,,

TIO version does not work.

\$\endgroup\$
  • \$\begingroup\$ "Unfortunately Keg currently does not support outputting newlines". Then Keg can't answer challenges that require outputting newlines. \$\endgroup\$ – pppery Sep 20 at 18:55
  • \$\begingroup\$ Then this answer shouldn't have been posted until the codepage was extended, because it doesn't meet the specification and is therefore subject to deletion per codegolf.meta.stackexchange.com/a/7990/46076. \$\endgroup\$ – pppery Sep 20 at 23:30
0
\$\begingroup\$

Plan 9 rc: 20-10=10 (but not quite there)

f=`{cat};while()echo $f

The problem with this almost-solution is that it must have something on stdin. I haven't yet been able to find a way to get it to print y if stdin isn't there.

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

Java, 157 154 bytes

Despite the common misconception that you have to use System.out to print anything to the console in Java, this program outputs to the console using a FileOutputStream to the file described by FileDescriptor.out. Accoding to the specification for FileDescriptor.out, it is "A handle to the standard output stream. Usually, this file descriptor is not used directly, but rather via the output stream known as System.out".

class A{public static void main(String[]a)throws Exception{for(;;)new java.io.FileOutputStream(java.io.FileDescriptor.out).write(new byte[]{120+1,9+1});}}
\$\endgroup\$
0
\$\begingroup\$

Pascal: 40 characters

begin while 1=1do Writeln(Succ('x'))end.

Outputs “y”, stops on SIGINT.

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

JAGL V1.0 20 - 10 = 10 With Bonus, 14 Without

Tdn{78h[}Sf{p9[pDd}u

Coulda probably done better, but here is the explaination:

Td                               Get a line of text input, and duplicate on stack
  n                              Negate its truth value (for the if)
   {78h[}                        Push a block which will push the y character, if called
         Sf                      Swap top two values on stack and if no input, push y
           {p9[pDd}              Push block which prints the input (or y) and a newline
                   u             Do until loop. Output always evaluates to true

Can be killed by SIGINT

Without bonus:

'x  '{1+}/{pd}u

explaination:

'x  '                            Pushes the string '\tx'
      {1+}                       Adds 1 to each character in the string, making it 'y\n'
          {pd}u                  Infinitely prints and duplicates 
\$\endgroup\$
0
\$\begingroup\$

Lazy K, 167 bytes

`k````ssi``s`k``ss`k``siik``s`k`s``si`k```s``s`kski``s``s`ksk``s``s`ksk```s``s`kski``s``s`ksk``s``s`kski``s`kk``s`k`s``si`k``s``s`ksk```s``s`kski``s``s`ksk``s``s`kskik

It is probably too long...

```ssi``s`k``ss`k``siik                                              fixed-point combinator
``s``s`ksk```s``s`kski``s``s`ksk``s``s`kski                          \n
```s``s`kski``s``s`ksk``s``s`ksk```s``s`kski``s``s`ksk``s``s`kski    y
\$\endgroup\$
0
\$\begingroup\$

C 42

main(){while(1){putchar(11*11);puts("");}}
\$\endgroup\$
0
\$\begingroup\$

This works in my Befunge interpreter:

"z"1-.25*.

10 chars. "z" means push the ascii value of the characted "z", 1 means push the number one, - means subtract, . means print the ascii value of it and * means multiply. For more info: http://en.wikipedia.org/wiki/Befunge

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

Microscript, 23-10=13

Not technically a valid competing entry, as the language is too new. However, I thought this would still be a cool thing to try.

Besides the initial input, takes an extra input after each iteration and halts when and if any of the additional inputs is non-empty (the specs for the question said to provide a way to make it halt).

If['w2s]1{CaI#!v#$olx}h

On another note, this would be shorter with a command to clear the selected stack, as it would allow me to cut v#$ol down to a single character. So that's the command I'm adding next.

--EDIT AFTER THIS POINT--

Alright, the new command has been added, so now the program would look like this, scoring 19-10=9:

If['w2s]1{CaI#!Zx}h

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

SmileBASIC, 19 bytes - 10 = 9

INPUT C$@L?C$GOTO@L
\$\endgroup\$
0
\$\begingroup\$

PHP, 34 bytes - 10 = 24

for(;;)echo$argc>1?$argv[1]:y,"
";

Run with php -nr '<code>' or php -nr '<code>' '<string>'.

Or 56 bytes for an exact port:

for(;;)echo$argc>1?join(" ",array_slice($argv,1)):y,"
";

(-2 bytes with extended ASCII; -1 for the not-so-complete version)

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

Jelly, 7 - 10 = -3 bytes

ØyṪ³ȯṄß

Try it online!

Explanation

Øy             # The string "AEIOUYaeiouy"
  Ṫ            # Take the last element, namely "y"
    ³          # The cmdline argument (empty list if none supplied)
     ȯ         # Either the cmdline if supplied or "y"
      Ṅ        # Print with a newline
       ß       # Recurse

Thankfully Jelly programs can always be C-c-ed.

Alternative Sans Y; 10 - 10 = 0 bytes

“¢³ƒ»Ṫ³ȯṄß

If the 'y' in the original answer is unacceptable, here's one without. This works on the same principle, the string “¢³ƒ» is the compressed version of "Jelly"

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

R, 51 bytes

while(T){cat(paste0(letters[25],'\\',letters[14]))}

Literally outputs "an infinite stream of "y\n"'s", rather than "y[newline]".

In RStudio the output is truncated but it is otherwise infinite.

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

Python 3, 40 bytes - 10 = 30

i=input()
while 1:print(i or chr(11**2))

Stops with SIGINT (Ctrl+C)

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

R, 27-10 = 17 bytes

x=scan(,'');repeat print(x)

Repeats stdin over and over and over. No y or \n in the code.

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

OIL, 23 bytes noncompeting

120
8

17

1

4

11
6
6

Increments 120, transforms it to a "y", and repeatedly print it and a newline.

Can be stopped with SIGINT / Ctrl+C.

Variant that receives something from stdin instead:

OIL, 13-10 = 3 bytes

5

4

11
6
2
\$\endgroup\$
0
\$\begingroup\$

AHK, 38 bytes - 10 = 28

c=%1%
Loop,3
FileAppend,% c Chr(9+1),*
  • I may have misunderstood, but I think the 9+1 is allowed. If not, I'll delete this.
  • If Loop is used without brackets {}, it is assumed that only the next line is part of the loop.
  • If Loop is used without a count, it will repeat until it encounters a Break.
  • Per the help file, if an asterisk is passed as the filename into FileAppend, it outputs to stdout.
  • %1% is the first passed argument.
  • If I could use the input variable %1% directly, that would have been nice. It gets funny with the Chr function following it, though, and would have ended up being more bites.
\$\endgroup\$
0
\$\begingroup\$

TI-BASIC, 8 bytes

While not(getKey:Disp Str0:End
\$\endgroup\$
0
\$\begingroup\$

Chip, 15+3 = 18 bytes

+3 for flag -w, which ignores stdin and generates an endless stream of null bytes for input instead.

dbae
*}++g
 `zf

Try it online!
Note about the TIO: instead of using the -w flag, it uses input. The behavior is the same, except that input is finite. One byte of output per byte of input.

Explanation

*}+
 `z

This is a 1-clock. On the first byte, the + is active, and on the second byte, the other three elements are active. The two states alternate endlessly.

d ae
* ++g
   f

This produces the output corresponding to 'y'. These letters each map to a bit: _gfed__a -> 0b01111001 -> 0x79 -> 'y'.

db
*}
 `z

This produces the output corresponding to '\n'. Similar to above, but we have these bits: ____d_b_ -> 0b00001010 -> 0x0a -> '\n'.

\$\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.