Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Consider a square of printable ASCII characters (code points 0x20 to 0x7E) for side length N, like the following (here, N = 6):

=\    
 g \  
7     
m+y "g
  L ~ 
e> PHq

We also require each row and each column to contain at least 1 space and 1 non-space character. (The above example satisfies this.)

We define the negative of such a square, to be a square of the same size, where each space is replaced with a non-space and vice versa. E.g., the following would be a valid negative of the above example:

  1234
a b cd  
 ZYXWV
   !  
{} [ ] 
  ?   

The choice of non-space characters is irrelevant (as long as they are from the printable ASCII range).

The Challenge

You're to write a program, with square source code with side length N > 1, which prints a negative of itself to STDOUT. Trailing spaces have to be printed. You may or may not print a single trailing newline.

The usual quine rules also apply, so you must not read your own source code, directly or indirectly. Likewise, you must not assume a REPL environment, which automatically prints the value of each entered expression.

The winner is the program with the lowest side length N. In the event of a tie, the submission with the fewest non-space characters in the source code wins. If there's still a tie, the earliest answer wins.

share|improve this question
    
Is a function's return value accepted or should it be printed to STDOUT? –  Paul Guyot 2 days ago
1  
@PaulGuyot For this challenge, please stick to full programs and STDOUT (as it's, loosely speaking, a quine variant). –  Martin Büttner 2 days ago
    
You should clarify that the board size must be N>0. I see that your example specifies N>1 (and did you mean N>=1?), but this is not in the rules proper. –  imallett yesterday
2  
@imallett implied by "We also require each row and each column to contain at least 1 space and 1 non-space character". Wait. No. It isn't. Good point. It does imply N==0||N>1, though. –  Jan Dvorak yesterday
1  
@jpcooper Yes, that's a square, but it doesn't satisfy "We also require each row and each column to contain at least 1 space and 1 non-space character." because the first m columns don't contain spaces, and the last n columns don't contain non-spaces. –  Martin Büttner 10 hours ago

10 Answers 10

Perl, 7×7 (42 non-spaces)

for$i( 
1..56 )
{$_= $i
%8? $i%
7? $":7
: $/;1;
 print}

Output:

      7
     7 
    7  
   7   
  7    
 7     
7      
share|improve this answer
4  
This is one of the coolest pieces of code I've seen in a long time. –  Kasran 2 days ago
    
Straightforward and simple, but quite boring. Still +1. –  Nova yesterday
3  
It would be even more cool if it displayed a 7 made of 7. :) –  A.L yesterday
1  
@A.L Just need 8 more characters (7 spaces and a newline). –  Qix 17 hours ago

CJam, 4X4 (12 10 non-spaces)

 6, 
SS +
* 4/
 N* 

Output:

0  1
  2 
 3  
4  5

Previous version with 12 non-spaces:

 4a4
* 4S
** 4
/N* 

And the output is

4   
 4  
  4 
   4

As pointed out by Martin, this version has

  • 4 Spaces,
  • 4 *,
  • 4 4,
  • 4 other characters, and
  • 4 4 as the output

;)

Try it online here

share|improve this answer
11  
It also has 4 upvotes, but I'm about to spoil that for you. Sorry! :-D –  squeamish ossifrage yesterday
5  
Maybe we can get it to 44? +1 :) –  Doorknob yesterday
    
sorry for being upvote #13 :P –  masterX244 yesterday
2  
Keep the pace going till 4^4 –  Optimizer 18 hours ago
1  
Doesn't 4^4 == 0? ;) –  zennehoy 7 hours ago

Marbelous - 16x16

....@0..@1....  
@3..0A08..@2  ..
/\--....>1  ..Hp
..@2..\\  =0@3..
ss..//  --\\\\..
@0/\  @1/\Hp..!!
:s  #the-cake-is
  2020#a-pie/lie

Test it here! Spaces as blanks, cylindrical board, and include libraries all must be checked.

Output

              07
            06  
          05    
        04      
      03        
    02          
  01            
01              

Explanation

There are two boards here: the main board (shown below), and the ss board, which takes no inputs and outputs two spaces (0x20) to STDOUT.

A blank cell is equivalent to a .., and anything after a # is a comment.

Picture of Board

Every tick, ss outputs two spaces to STDOUT.

The green path is a simple loop that outputs a newline (0x0A) at the end of every 7th tick.

The blue path will output the numbers (Hp prints a marble as two hex digits) present in the output, at the end of every 6th tick.

After we have printed 01 once, the loop ends, and moves down the red path, which duplicates this marble.

One duplicate is printed (the second 01), and the other is sent down the black path, which terminates the board at the !! cell. Because of the location of the Hp used in this last print, the 01 appears before the same tick's two spaces, rather than after, the behavior of every other Hp call.

share|improve this answer
1  
Upvoting for the graphical representation of the source. –  Riking 18 hours ago

Python - 11x11

import re
[print(
    re.sub(
"0",   " ",
bin(x)[
2:].zfill(
11)))for x
in[19,15,
1920,116,
15,1,5,3,
3,3, 67]]

Output

      1  11
       1111
1111       
    111 1  
       1111
          1
        1 1
         11
         11
         11
    1    11

It's a pretty messy and boring solution, but I just thought I'd show that...

  1. It can be done in Python
  2. If you can compress information about your code shorter than your code, then you can pull off something like this :)

This solution takes advantage of the fact that, if you're within a pair of brackets in Python, then you can split your code over several lines and arbitrarily add spaces without getting an IndentationError. Another way of doing something like this is by ending the line with a backslash.

share|improve this answer

Python - 7x7 (37 non-spaces)

print( 
'%+7s' 
'\n'%1 
*6+'%' 
'-7s'% 
111111 
      )

Output

      1
      1
      1
      1
      1
      1
111111 

Uses Python's old % string formatting operator to do the job: +7 and -7 take care of right/left justification, and the last space to match the closing parenthesis for print in particular. In preparing the format string, we also have

  • automatic concatenation of string literals across lines, and
  • string repetition by multiplication (giving us multiple replacement fields for the price of one)
share|improve this answer

JavaScript (9x9)

 i=9;q=""
; while((
++ i)<91)
{;q =""+q
+(!( i%10
)?1:" ");
;i%9|| (q
+="\n") }
alert(q) 

Output

1        
 1       
  1      
   1     
    1    
     1   
      1  
       1 
        1

Notes

I made and golfed (to the best of my ability) code for a square with diagonal of any size n:

q="";for(i=***n***;++i<***n^2+n+1***;i%***n***||(q+="\n"))q+=i%***n+1***?"0":1

replacing the ***asdf*** numbers with constants depending on the side length n, for example for n=6:

q="";for(i=6;++i<43;i%6||(q+="\n"))q+=i%7?" ":1

But, even though that code is length 46, I couldn't get the constant space to line up with a space in the diagonal of the code until it was as big as a 9x9, with a wasted line (the 5th one)

Edit: Changed to add alert(). Before:

 i=9;q=""
; while((
++ i)<91)
{q= q+""+
""+( "")+
(!((i %10
))?1:" ")
;i%9||( q
+="\n")} 
share|improve this answer
    
Ahh, yea, oops, I see what you mean. I'm fixing it right now, sorry. –  Kuilin Li yesterday

Python 3, 8x8

There are 50 non-space characters and 14 spaces. The last line has one useless character, but everything else is necessary.

(*_,q,n 
)=p=''' 
       p
'''[2:] 
print(p 
*2+q*7, 
n+p*4+p 
[0:-1]) 

Output:

       p
       p
ppppppp 
       p
       p
       p
       p
       p
share|improve this answer
2  
Just tried playing around with (a,*b,c)="12345"... starred assignment is interesting :) –  Sp3000 yesterday
    
@sp3000 kind of haskell-esque. i use python way more than haskell, how did i not know this –  undergroundmonorail yesterday

CJam, 5X5

SSSS 
1N]4 
*B11 
    S

And the output is

    1
    1
    1
    1
1111 

I was this close to 4X4 solution. <sigh> See my other answer

Try it online here

share|improve this answer

Ruby, 8x8

 (0...8)
. map(){
|x |$><<
32. chr*
x+[x ]*'
'<<32 .\
chr*(7 -
x)+?\n} 

Output:

0       
 1      
  2     
   3    
    4   
     5  
      6 
       7
share|improve this answer

CJam, 5x5, 12 non-spaces

Not a winner, but I wanted to add a rather small and sparse submission, since most answers just print a diagonal.

 1 ]
D * S
 * 5
/ N *
 1 ; 

prints

1 1 1
 1 1 
1 1 1
 1 1 
1 1 1

Test it here.

The last two characters of the code, don't do anything, so it actually only has 10 bytes of real code. For a smaller grid, I could even reduce it by another two bytes to 8, but that doesn't fit on 3x3, and this code doesn't work for even grid sizes.

How it works:

1]           "Push [1].";
  D*         "Repeat 13 times.";
    S*       "Riffle with spaces.";
      5/     "Split into runs of five elements.";
        N*   "Join those with line breaks.";
          1; "Push and pop a 1. No-op.";
share|improve this answer

protected by Community 11 hours ago

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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