| Bytes | Lang | Time | Link |
|---|---|---|---|
| 019 | Dyalog APL | 250731T165416Z | Aaron |
| 088 | AWK | 250730T181119Z | xrs |
| 097 | Swift 6 | 250502T172909Z | macOSist |
| 069 | Tcl | 180414T200655Z | sergiol |
| 008 | Vyxal 3 s | 240131T181315Z | pacman25 |
| 775 | Vyxal Hj | 240204T203308Z | pacman25 |
| 012 | Uiua | 240131T201553Z | Joao-3 |
| 042 | Ruby | 221013T020834Z | Jordan |
| 017 | Jelly | 161227T113026Z | Erik the |
| 015 | 05AB1E | 161227T142932Z | Erik the |
| 009 | 05AB1E | 201009T090155Z | Kevin Cr |
| 032 | Perl 5 | 180512T070324Z | Dom Hast |
| 015 | Japt | 180424T195207Z | Oliver |
| 048 | Perl 5 | 180425T024101Z | Xcali |
| 072 | JavaScript Node.js | 180425T012125Z | l4m2 |
| 114 | Python 2 | 180424T230158Z | Husnain |
| 056 | Python 2 | 171009T050609Z | Koishore |
| 074 | Python 3 | 171008T193802Z | CursorCo |
| 027 | J | 171008T184344Z | FrownyFr |
| 011 | Pyth | 150803T144835Z | Beta Dec |
| 103 | Python 3 | 161111T123513Z | NeRoboto |
| 055 | Perl | 161111T203102Z | Denis Ib |
| 052 | Perl | 161112T093418Z | Denis Ib |
| 098 | Prolog SWI | 161111T140958Z | Emigna |
| 055 | PHP | 161111T132923Z | Titus |
| 049 | Python 2 | 160805T080829Z | xnor |
| 080 | Python 3 | 160805T071329Z | Destruct |
| 077 | JavaScript | 150608T184957Z | edc65 |
| 068 | Ruby | 150804T200422Z | daniero |
| 065 | POWERSHELL | 150731T111538Z | blabb |
| 092 | Swift 2 | 150731T105154Z | GoatInTh |
| 097 | C | 150731T115646Z | Cole Cam |
| 111 | C# | 150731T090152Z | Stephan |
| 054 | Haskell | 150611T212653Z | nimi |
| 086 | Ruby | 150718T212744Z | Piccolo |
| 092 | Ruby | 150718T202412Z | clapp |
| 033 | Matlab | 150610T103848Z | Oebele |
| 081 | Python 2 | 150610T071012Z | dieter |
| 012 | CJam | 150609T212738Z | Martin E |
| 025 | Mathematica | 150609T215426Z | Martin E |
| 095 | Python 2 | 150609T214022Z | Kade |
| 011 | K | 150609T212401Z | JohnE |
Dyalog APL, 19 chars
↑{3 3⍴(9⍴2)⊤⍵}¨⍳512
Same as the J's and K's, just figured I'd throw this in for completeness.
⍳512 Numbers 1 through 512 (2^9)
{ }¨ For each of those
(9⍴2) Get 9 2's
⊤⍵ To encode the input as binary
3 3⍴ Reshape that as a 3x3 square
↑ And mix so it looks pretty
AWK, 88 bytes
END{for(;i<512;i++){for(j=0;j<9;){printf and(i,2^(8-j))?1:0;printf(j+++1)%3?X:RS}print}}
Swift 6, 97 bytes
(512..<1024).map{[_](String($0,radix:2))}.map{print($0[1..<4]+"\n"+$0[4..<7]+"\n"+$0[7...]+"\n")}
Try it on SwiftFiddle! (SwiftFiddle doesn't seem to display completely empty lines in the output — replace the last "\n" with "\n " to fix this. Running the program locally doesn't have this problem.)
Vyxal Hj, 62 bitsv2, 7.75 bytes
U2(3ÞẊṅ¶+
U2(3ÞẊṅ¶+
# #H flag sets stack to 100
U # uniquify the digits, getting a list of [1,0]. This didn't work with the builtin for 10 when I tried it
2( # iterate twice
3ÞẊ # cartesian power of 3
ṅ¶+ # joined on nothing with a newline appended
# j flag joins top of stack on newlines
# this leads to the double newline separator
💎
Created with the help of Luminespire.
Bitstring:
01100100111011000111001100111101010001110000111111010101000100
Port of Kevin's 05ab1e answer, with different separators.
Uiua, 12 bytes
≡(↯3_3)⋯⇡512
Outputs an array of 512 3x3 bit matrices. Makes clever use of the bits ⋯ primitive, which converts all numbers in an array to lists of bits.
With &gifs, you can make a very tiny GIF cycling every grid.
Jelly, 17 bytes
512+ḶBḊ€s€3Y€j“¶¶
Uses 01. Because of a bug in ⁾, I had to use “¶¶ instead of ⁾¶¶, because otherwise, instead of two newlines, two pilcrows would have showed up in the output. As you can see, though, that didn't cost me any bytes at all.
K beats this, so this must be further golfed down.
05AB1E, 9 bytes
T3ã¶«3ãJ»
Uses the digits [1,0]. The T could be replaced with ₂, ₃, or ₆ to use the digits [2,6], [9,5], or [3,6] respectively.
Outputting as an unformatted list could have been 5 bytes: T3ã3ã.
Explanation:
T # Push 10
3ã # Take the cartesian power of 3 with its digits:
# ["111","110","101",...,"000"]
¶« # Append a newline to each triplet
# ["111\n","110\n","101\n",...,"000\n"]
3ã # Take the cartesian power of 3 with this list of strings:
# [["111\n","111\n","111\n"],["111\n","111\n","110\n"],...,["000\n","000\n","000\n"]]
J # Join each inner list together
# ["111\n111\n111\n","111\n111\n110\n",...,"000\n000\n000\n"]
» # And join that list by newlines
# "111\n111\n111\n\n111\n111\n110\n\n...\n000\n000\n000\n"
# (after which it is output implicitly as result)
JavaScript (Node.js), 72 bytes
h=console.log;f=u=>h(u/4&1,u/2&1,u&1)|u/8;for(i=0;i<512;h``)f(f(f(i++)))
JavaScript (Node.js), 64 bytes
for(i=0;i<512;f(f(f(i++))))f=u=>console.log(u/4&1,u/2&1,u&1)|u/8
JavaScript (Node.js), 75 bytes
for(i=511;++i<1024;f(f(f(i))))f=u=>console.log(u.toString(2).slice(-3))|u/8
Python 2, 114 bytes
from itertools import *
for i in product(".X",repeat=9):
i="".join(i);print i[0:3]+"\n"+i[3:6]+"\n"+i[6:]+"\n"
Python 2, 56 bytes
from itertools import*
print set(combinations('X.'*9,9))
Returns the 512 configurations as a set object in python. Refer to ungolfed version to make output more readable.
Ungolfed Version to make output more readable:
Python 2, 121 bytes
from itertools import*
for i in set(combinations('X.'*9,9)):
for j in range(3):print''.join(list(i))[j*3:(j*3)+3]
print
Python 3, 74 bytes
i=512;b="\n"
while i:i-=1;a=format(i,'09b');print(a[:3]+b+a[3:6]+b+a[6:],b)
Just a little shorter than Destructible Lemon's answer
Python 3, 123 121 109 103 bytes
Here is my old one:
import itertools
[print(a+b+c,d+e+f,g+h+i,'\n',sep='\n') for a,b,c,d,e,f,g,h,i in itertools.product(['X','.'],repeat=9)]
And here is my new one:
import itertools as i
[print(a[:3],a[3:6],a[6:],'\n',sep='\n') for a in i.product(['X','.'],repeat=9)]
This prints extra characters but the OP said ASCII art is allowed which implies multiple characters are fine.
Perl, 56 55 bytes
print$_/9&1<<$_%9?1:0,$/x(!(++$_%3)+!($_%9))for+0..4607
Output:
000
000
000
100
000
000
...
Perl, 52 bytes
printf+("%03b
"x3).$/,$_&7,$_/8&7,$_>>6&7 for 0..511
or 54 bytes:
print map$_.$/,sprintf('%09b',$_)=~/.../g,''for 0..511
or Perl 5.14+, 48 bytes:
say sprintf('%09b',$_)=~s/.../$&\n/gr for 0..511
Prolog (SWI), 98 bytes
Output is a list of 3x3 matrices containing the elements 0 and 1.
r([]).
r([H|T]):-between(0,1,H),r(T).
p(Y):-Z=[[_,_,_],[_,_,_],[_,_,_]],findall(Z,maplist(r,Z),Y).
I feel like the matrix generation could be shorter.
It should be possible to fit the between in a forall or something similar, but I can't figure out how.
Golfing tips appreciated.
PHP, 55 bytes
for(;$c<512;)echo chunk_split(sprintf("%09b ",$c++),3);
uses 0 and 1. Run with -r.
Python 2, 49 bytes
i=2048;exec"print bin(i/4)[i%4*3+3:][:3];i+=1;"*i
Split the binary expansion of i. The length-10 binary values 512 through 1023 are used, cutting off the initial 1 (and prefix 0b). These are split into chunks of 3 as windows [3:6], [6:9], [9:12], and [12:15], with the last one blank to make a blank line. Iterating over the four slices is collapsed with the outer loop of counting through 512 numbers with the divmod trick.
Python 3, 80 bytes
for i in range(512):print("\n".join(format(i,'09b')[j:j+3]for j in(0,3,6)),"\n")
I managed to outgolf someone :)
JavaScript, 77 80
Revised after the revision of the OP. Now we have a question, so here is an answer.
Run the snippet in any browser to test.
// Test: redefine console to have output inside the snippet
console = { log: function(x) { O.textContent+=x+'\n\n';} }
// Solution: 77 chars (note, distinct outputs to console are automatically separed)
for(i=511;++i<1024;)console.log(i.toString(2).slice(1).match(/.../g).join`
`)
<pre id=O></pre>
Old post: graphic display in a browser, with javascript and canvas. ~300 byte of code (can be made shorter).
Run the snippet below.
d=8, // Min block size
C.width=d*64,C.height=d*128,
T=C.getContext('2d')
for(i=0;i<512;i++)
{
bx=4*(i/32|0)
by=4*(i%32)
for(b=1,j=0;j<9;j++,b+=b)
{
if(b&i)
x=j/3|0, y=j%3, T.fillRect((bx+x)*d,(by+y)*d,d,d);
}
T.strokeRect(bx*d,by*d,d*3,d*3);
}
<canvas id=C></canvas>
Ruby, 68 bytes
Prints the exact same output as the example given in the question
puts (0..511).map{|i|("%09b"%i).tr("01",".X").gsub(/.../){$&+$/}}*$/
POWERSHELL - 65
0..511|%{[convert]::ToString($_,2).padleft(9,'0')-split"(.{3})"}
result
000
000
000
000
000
001
000
000
010
000
000
011
confirmation
(0..511|%{[convert]::ToString($_,2).padleft(9,'0')-split"(.{3})"} | measure -Line).lines/3
512
edit inspired by the mathematica answer's display of results-617
Add-Type -AssemblyName System.Drawing
$a=new-object System.Drawing.Bitmap 992,496
$g=[Drawing.Graphics]::FromImage($a)
$b=@{};$y=@{};$i=$c=$d=$z=$k=$l=$m=0;
0..511|%{$y[$d++]=[convert]::ToString($_,2).padleft(9,'0')}
while($m-lt480){while($l-lt496){for($z=($m+0);$z-lt($m+32);$z++){
$y[$z].tochararray()|%{if($_-eq"0"){$b[$i++]=[Drawing.Brushes]::Black}
else{$b[$i++]=[Drawing.Brushes]::White}
}
for($j=($l+0);$j-lt($l+30);$j+=10){
($k+0),($k+10),($k+20)|%{$g.FillRectangle($b[$c++],$_,$j,10,10)}
}$k+=31
}$k=0;$l+=31;$m+=32
}
}$a.save("$HOME/3X3_Grid.png")
Swift 2, 92 bytes
Int to binary string in Swift takes up too many chars so I just use two nested loops...
var s="";for x in 0..<512{(0..<9).map{s+=($0%3==0 ?"\n":"")+"\(x>>$0&1)"};s+="\n-"};print(s)
C - 97 bytes
i;main(j){for(;i++<512;)for(j=0;j++<13;)putchar(j%4&&j<13?i%(1<<j-j/4)>(1<<j-j/4-1)-1?88:46:10);}
Basically prints the example output from the original question.
C# - 111
for(int i=0;i<512;i++)Console.WriteLine(Regex.Replace(Convert.ToString(i,2).PadLeft(9,'0'),"(.{3})","$1\r\n"));
Converts every int to its binary representation and splits every 3 chars.
Haskell, 57 54 bytes
r x=sequence[x,x,x]
u=unlines
f=putStr$u$map u$r$r".X"
f gives the same output as in the challenge description, i.e. it starts with
...
...
...
...
...
..X
...
...
.X.
Edit: @Mauris found 3 bytes to save. Thanks!
Ruby, 86 bytes
0.upto(511).map{|i|i.to_s(2).rjust(9,'0')}.each{|j|p j[0..2];p j[3..5];p j[6..8];puts}
Mine prints with quotes because p is shorter than puts, but it does still fit the rules.
Ruby, 92 bytes
0.upto(511){|i|("%09d"%i.to_s(2)).scan(/.{3}/).map{|j|j.scan(/./)}.map{|j|puts j.join};puts}
Counts in 0s and 1s, and each block is separated by an empty line (\n\n)
Matlab, 33
reshape(dec2bin(0:511,9)',3,3,[])
Was kind of fiddly to get the dimensions correct, but I am very happy with the result!
Python 2, 81
import re
for i in range(512):print re.sub('(.{3})','\\1\n',bin(i)[2:].zfill(9))
CJam, 12 bytes
2,9m*3f/N*N*
Uses 0 and 1 as the distinct characters.
Explanation
2, e# Push [0 1].
9m* e# Generate all 9-tuples of 0s and 1s.
3f/ e# Split each 9-tuple into 3 subarrays of length 3.
N* e# Join all those grids with newlines.
N* e# Put newlines between all the length-3 arrays.
An alternative (still 12 byte) solution is
2,3m*3m*N*N*
Mathematica, 25 bytes
Image/@{0,1}~Tuples~{3,3}
Gives an array with all the grids as images, which is also directly displayed on screen:

(Cropped so as not to blow up the post unnecessarily.)
Python 2, 95 Bytes
Distinct characters are 0 and 1, each block is separated by \n\n.
n='\n';print(n+n).join(y[:3]+n+y[3:6]+n+y[-3:]for y in[bin(x)[2:].zfill(9)for x in range(512)])
K, 11 bytes
(3 3#)'!9#2
Output example:
((0 0 0
0 0 0
0 0 0)
(0 0 0
0 0 0
0 0 1)
(0 0 0
0 0 0
0 1 0)
(0 0 0
0 0 0
0 1 1)
…
This is K's native prettyprinted representation of a list of matrices, which I think is sufficient for the problem spec. Each matrix is delimited by an enclosing set of parentheses.
And a quick sanity check to demonstrate that 512 matrices are constructed:
#(3 3#)'!9#2
512
Very straightforward. Most of the work is in the !. First we generate a 9-long vector of 2s using "take" (9#2). Then, we make use of the "odometer" monadic form of !- a few examples illustrate its behavior:
!2 2
(0 0
0 1
1 0
1 1)
!2 3
(0 0
0 1
0 2
1 0
1 1
1 2)
!2 2 2
(0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1)
Then simply do a 3x3 reshape ((3 3#)) of each (') of the 9-length 0/1 vectors.
