Fill in the grid randomly

Given positive integer n < 10, create a 2 dimensional matrix where each location is filled with its x and y index (starting from the top left).

For example:

Input: 2

00 10
10 11


Input: 3

00 10 20
01 11 21
02 12 22


Once the grid is created, randomly fill each index. This can be with an 'x' or any other way to denote a spot has been filled.

You determine which location to fill by randomly generating indices to fill the matrix. You can only fill n^2 times so you cannot fill as many times as you want until the matrix is completely filled. At the end the matrix must be filled so you must do some work to make sure that you check the random numbers that you use to fill to make sure that spot is not already filled.

Refresh or print after each fill in order to show the progression of the filling iterations.

Example for filling:

Input: 2

00 10
01 11


00 is randomly chosen:

XX 10
01 11


01 is randomly chosen:

XX 10
XX 11


00 is randomly chosen, but since it's already been chosen a re-roll chooses 10:

XX XX
XX 11


11 is randomly chosen:

XX XX
XX XX


Do not print out the random numbers as visually I should be able to see which index was selected. By this I mean do not print "11 is randomly chosen:". It is here for exploratory sake.

Since this is code-golf The shortest code wins.

Have fun and happy golfing!

• I don't understand what is so complicated about the instructions which are very clear. "create a 2 dimensional matrix where each location is filled with it's xy index (starting from the top left)" (Not a printable string). "Refresh or print after each fill in order to show the progression of the filling iterations." must show the progression. Why be overly specific when it just narrows how creative users can be with their solutions? – jacksonecac Oct 11 '16 at 17:49
• Is n>= 10 possible ? (you have to start to know about the maximum length to properly fill in leading 0's then). The filling for that case is one index at a time, not 1 digit at a time, right ? – Ton Hospel Oct 11 '16 at 19:36
• @TimmyD I agree that this should have spent more time in the Sandbox simply because that is what the sandbox is for but for me the instructions are pretty clear about what is required. Not a bad challenge IMHO. – ElPedro Oct 11 '16 at 19:38
• @TonHospel Good point. I will edit to ensure n < 10 – jacksonecac Oct 12 '16 at 11:10
• This looks much better. I would still take out the references to "The shortest code wins with a bonus if some GUI was used instead of ASCII". It's still undefined. – Morgan Thrapp Oct 12 '16 at 16:10

05AB1E, 29 bytes

<ÝDâJU[X¹ä»,XÐÙg#Jþ2ô.R„  :)U


Try it online!

Space chosen as the char for the removed numbers (as it looks nice), but it could be replaced with any char without affecting byte-count.

Explanation

                                # implicit input n
<ÝDâ                            # cartesian product of [0..n-1] and [0..n-1]
JU                          # join pairs and store in X
[     XÐÙg#               # loop until there's only spaces left in X
X¹ä                      # split X into n pieces
»,                    # join rows by space and columns by newlines and print
Jþ             # join X to string and remove all non-digits
2ô.R         # split in pieces of 2 and pick a pair at random
„  :)    # replace this pair with 2 spaces
U   # and store in X

• It looks awesome but as I test it, it seems like it doesn't fill every square? – jacksonecac Oct 12 '16 at 18:24
• @jacksonecac: As I understood it, I should randomly fill n^2 times, with the possibility of not all squares getting filled if the same index is chosen at random more than one time. If that's wrong I'll have to redo this later (have to run now) – Emigna Oct 12 '16 at 18:26
• "You determine which location to fill by randomly generating indices to fill the matrix. You can only fill n^2 times so you cannot fill as many times as you want until the matrix is completely filled." So it must be filled. I will clarify more in the description. – jacksonecac Oct 12 '16 at 18:28
• @jacksonecac Thanks for the clarification. I've updated the answer accordingly :) – Emigna Oct 12 '16 at 21:52
• Perfect! Nice job man! – jacksonecac Oct 13 '16 at 11:08

Pip, 414038 36 bytes

35 bytes of code, +1 for the -S flag.

Pm:J_MM ZCGa{ST:mmR:asX2}M$ALmSK{r}  Takes input from cmdline argument. Replaces with space (any other character is possible for +1 byte). Outputs successive iterations separated by a single newline (which is legal but can make it a bit hard to read). Try it online! All kinds of dirty tricks in this one. Shorter version has fewer dirty tricks. :^( Explanation: Pm:J_MM ZCGa{ST:mmR:asX2}M$ALmSK{r}
-S flag means nested lists are delimited first
by newlines then by spaces when stringified/printed
a                         1st cmdline arg
CG                          Coordinate Grid, a list of lists of coord pairs
Z                            Zip (transposes so it's x,y instead of row,col)
J_                                Function that takes a list and joins all items
MM                              MapMap: map this function to each sublist
This joins a coord pair [1;0] into a string "10"
Pm:                                 Assign the result to m and print it

$ALm Fold m on Append List: appends all sublists of m together, making a single list of coord pairs SK Sort with the following function as key: {r} Return a random number We now have a randomly-ordered list of all the coord pairs from m { }M Map this function to that list: ST:m Convert m to string in-place mR: Replace (in-place)... a the argument (a coord pair)... sX2 ... with two spaces The map operation returns a list of strings, one for each step of the process, which are autoprinted (separated by newlines)  • Nice Job! That works perfectly – jacksonecac Oct 12 '16 at 18:54 • Actually, for n>=10 the randomization isn't working correctly, but it still hits the brief. For numbers larger than 10 it only removes where index_i==index_j. Any idea behind the reason why that would be? – Magic Octopus Urn Oct 12 '16 at 19:16 • @carusocomputing Not entirely sure, but it's probably something to do with how the indices are chosen in the (mi@##Pmi@0) part. I put in several byte-reducing hacks that depend on the indices being single digits. – DLosc Oct 12 '16 at 19:20 • ##, got it. Nice use of assumptions. Thanks for the explanation haha. – Magic Octopus Urn Oct 12 '16 at 19:23 Groovy (202 Bytes) {a->b=new String[a][a];while(b.flatten().flatten().contains(null)){b[(int)(Math.random()*a)][(int)(Math.random()*a)]="XX";b.eachWithIndex{e,i->e.eachWithIndex{f,j->print f?"XX ":"${i}${j} "}println()}}}  That specific output format really messed up my byte count, but meh. Try it out: https://groovyconsole.appspot.com/edit/5171951567896576 (+9 bytes for a prettier print) Ungolfed: y={a-> b=new String[a][a]; while(b.flatten().flatten().contains(null)) { b[(int)(Math.random()*a)][(int)(Math.random()*a)]="XX"; b.eachWithIndex{ e,i-> e.eachWithIndex{ f,j-> print f ? "XX ": "${i}${j} " } println() } } } y(4)​  Output Example: 00 01 02 XX 10 11 12 13 20 21 22 23 30 31 32 33 00 01 02 XX XX 11 12 13 20 21 22 23 30 31 32 33 XX 01 02 XX XX 11 12 13 20 21 22 23 30 31 32 33 XX 01 XX XX XX 11 12 13 20 21 22 23 30 31 32 33 XX 01 XX XX XX 11 12 XX 20 21 22 23 30 31 32 33 XX 01 XX XX XX 11 12 XX XX 21 22 23 30 31 32 33 XX 01 XX XX XX 11 XX XX XX 21 22 23 30 31 32 33 XX 01 XX XX XX XX XX XX XX 21 22 23 30 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 30 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 30 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 23 XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 XX XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 XX XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 XX XX 31 32 33 XX XX XX XX XX XX XX XX XX 21 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX 31 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX 32 XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX 22 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX  • the matrix should be NxN so a perfect square. – jacksonecac Oct 12 '16 at 18:47 • @jacksonecac It is, it's a 0-indexed 4x4 square. The square itself is just newline separated, as well as each iteration is newline separated, so the output kinda runs together. – AdmBorkBork Oct 12 '16 at 18:49 • If you want delimiters between the iterations, specify it in the brief. – Magic Octopus Urn Oct 12 '16 at 18:49 • Here, try it with the newline added inbetween iterations: groovyconsole.appspot.com/edit/5171951567896576 – Magic Octopus Urn Oct 12 '16 at 18:51 • I apologize I jumped to conclusions. Let me parse this out :D – jacksonecac Oct 12 '16 at 18:52 R, 8481 74 bytes Now uses one-indexing rather than zero-indexing. Got rid of 7 bytes thanks to @Billywob. N=scan() m=outer(1:N,1:N,paste0) for(i in sample(N^2)){m[i]="XX";print(m)}  Example output for N=3  [,1] [,2] [,3] [1,] "11" "12" "XX" [2,] "21" "22" "23" [3,] "31" "32" "33" [,1] [,2] [,3] [1,] "11" "12" "XX" [2,] "21" "22" "23" [3,] "31" "XX" "33" [,1] [,2] [,3] [1,] "11" "12" "XX" [2,] "XX" "22" "23" [3,] "31" "XX" "33" [,1] [,2] [,3] [1,] "11" "XX" "XX" [2,] "XX" "22" "23" [3,] "31" "XX" "33" [,1] [,2] [,3] [1,] "XX" "XX" "XX" [2,] "XX" "22" "23" [3,] "31" "XX" "33" [,1] [,2] [,3] [1,] "XX" "XX" "XX" [2,] "XX" "22" "23" [3,] "XX" "XX" "33" [,1] [,2] [,3] [1,] "XX" "XX" "XX" [2,] "XX" "XX" "23" [3,] "XX" "XX" "33" [,1] [,2] [,3] [1,] "XX" "XX" "XX" [2,] "XX" "XX" "XX" [3,] "XX" "XX" "33" [,1] [,2] [,3] [1,] "XX" "XX" "XX" [2,] "XX" "XX" "XX" [3,] "XX" "XX" "XX"  • Nice Job! Go for it. Save those bytes! – jacksonecac Oct 13 '16 at 11:09 • You can save a few bytes using direct substitution instead of replace: for(i in sample(N^2)){m[i]="XX";print(m)} – Billywob Oct 13 '16 at 13:57 • @Billywob Thanks, I've edited the code to incorporate your suggestion. Great catch! – rturnbull Oct 13 '16 at 15:26 AWK, 229 bytes func p(a){for(k=1;k<=m;k++){if(k==a)gsub("[0-9]","X",M[k]) printf"%s",M[k]}}{n=$1;m=n*n
k=1
for(i=0;i<n;i++)for(j=0;j<n;j++){s=k%n==0?k==m?"\n\n":"\n":" "
M[k++]=i j s}p()
for(;z<m;z++){do{y=int(rand()*m+1)}while(M[y]~"X")p(y)}}


I added a few bytes to give the output a space between each matrix.

Note: to make it more 'random' between runs, a call to srand() could be added for 7 additional bytes.

Usage and output after storing above code in FILE:

    awk -f FILE <<< 2

00 01
10 11

XX 01
10 11

XX XX
10 11

XX XX
10 XX

XX XX
XX XX


PHP, 172 Bytes

for(;$x<$s=($a=$argv[1])*$a;)$r[]=$x%$a.($x++/$a^0);echo($c=chunk_split)(join(" ",$r),$a*3);for(;$q<$s;){if($r[$z=rand(0,$s-1)]<X)++$q&$r[$z]=XX;echo$c(join(" ",$r),$a*3);}


Breakdown

for(;$x<$s=($a=$argv[1])*$a;)$r[]=$x%$a.($x++/$a^0); #make the array
echo($c=chunk_split)(join(" ",$r),$a*3); # Output array for(;$q<$s;) { if($r[$z=rand(0,$s-1)]<X)++$q&$r[$z]=XX; #fill position if it is not XX and raise increment echo$c(join(" ",$r),$a*3); #Output array
}


Python 2, 190 bytes

from random import *
R=range(input())
G=[(x,y)for x in R for y in R]
def f():print"\n".join(" ".join(["XX","%d%d"%(x,y)][(x,y) in G]for x in R)for y in R)
f()
while G:G.remove(choice(G));f()