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 Nov 29 at 23:30
1  
@PaulGuyot For this challenge, please stick to full programs and STDOUT (as it's, loosely speaking, a quine variant). –  Martin Büttner Nov 29 at 23:32
    
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 Nov 30 at 6:11
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 Nov 30 at 6:38
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 Dec 1 at 13:53

11 Answers 11

up vote 59 down vote accepted

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
14  
It also has 4 upvotes, but I'm about to spoil that for you. Sorry! :-D –  squeamish ossifrage Nov 30 at 14:10
5  
Maybe we can get it to 44? +1 :) –  Doorknob 冰 Nov 30 at 17:38
    
sorry for being upvote #13 :P –  masterX244 Nov 30 at 21:19
2  
Keep the pace going till 4^4 –  Optimizer Dec 1 at 6:28
8  
Doesn't 4^4 == 0? ;) –  zennehoy Dec 1 at 17:22

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
7  
This is one of the coolest pieces of code I've seen in a long time. –  Kasran Nov 29 at 23:40
    
Straightforward and simple, but quite boring. Still +1. –  Nova Nov 30 at 0:57
5  
It would be even more cool if it displayed a 7 made of 7. :) –  A.L Nov 30 at 23:09
1  
@A.L Just need 8 more characters (7 spaces and a newline). –  Qix Dec 1 at 7:08

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
6  
Upvoting for the graphical representation of the source. –  Riking Dec 1 at 6:33

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
    
+1 to learn about the bracket trick! –  Anonsage Dec 4 at 21:25

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=""
; for(;++
i< 91;){;
var q=q+(
!(i% 10.0
)?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 Nov 30 at 7:37
    
You've got some unitialised global variables over there :P –  Tomáš Zato Dec 2 at 14:20

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
    
The exploded CJam and Pyth answers are by far my favourite thing on the site. Have an upvote, Reddit-style. –  Soham Chowdhury Dec 6 at 18:53

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 Nov 30 at 5:43
    
@sp3000 kind of haskell-esque. i use python way more than haskell, how did i not know this –  undergroundmonorail Nov 30 at 10:53

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

Befunge, 9x9

I have no idea why I did this. It took way too long. I have a massive headache now.

8>v_20gv 
v9<^v#<4 
1@,/<>^6 
v1,*$:<p 
->,!-^87 
:^*25<^g 
_88g,^^4 
9vp\v#6< 
        @

Output:

        @
        @
        @
        @
        @
        @
        @
        @
$$$$$$$$ 

Some explanation

The code uses g to read the @ characters from the grid "on the fly" (and also the final space, which is @ / 2), and p to modify the loop to write the last output line.

Every single character on the code is used at some point, either as code or as data (the 9 and the @ on the two last lines).

I basically had to do a lot of workarounds to make the code work. The instruction pointer does multiple intersections during execution, of which some are jumped over. (I couldn't use any instruction there for different directions as they would interfere. There is no NOP.) Elsewhere I either reused the same character or just undid it (see the $: in the middle).

I also did some creative work on the stack:

  • When the (inner) character writing loop terminates (all spaces done for this line), the stack has n,0. I then have to decrement n. The obvious solution would be $1-, but I managed to shorten it by using !-.
  • When the (outer) line writing loop terminates (all normal lines printed), the stack has a 0. I then organized code changer (20g46p7g46\p) to use that 0, instead of wasting 2 characters on $0.
share|improve this answer
    
Befunge always gets a +1 from me. Nice with the !-; that's something I'd do. Fun fact: In Funge-98, z is a NOP. –  Kasran Dec 4 at 20:09
    
@Kasran Yeah. z wasn't listed on the wiki and I used this for coding, which meant any non-command flipped the IP direction. Pretty much had to refactor 70% of the code due to one place that should be NOP. –  Pietu1998 Dec 4 at 20:47
    
Well, the advantage of doing that is that your code will run in Befunge-95 (which I don't believe has a non-space NOP) as well as 98; since it's a lot harder to use 95, I've always found those answers extra impressive. –  Kasran Dec 4 at 20:52

protected by Community Dec 1 at 13:18

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.