Fill in the grid randomly

9

1

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!

jacksonecac

Posted 2016-10-11T17:11:21.393

Reputation: 2 584

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 – 2016-10-11T17:49:47.513

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 – 2016-10-11T19:36:36.167

@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 – 2016-10-11T19:38:03.183

@TonHospel Good point. I will edit to ensure n < 10 – jacksonecac – 2016-10-12T11:10:51.650

What's the bonus? And "GUI and black squares" could use some clarification. – Rɪᴋᴇʀ – 2016-10-12T14:14:44.397

"Do not print out the random numbers as visually I should be able to see which index was selected." What does that mean? – Rɪᴋᴇʀ – 2016-10-12T14:15:44.220

@EasterlyIrk In my example I print out each iteration of the matrix being filled. I displayed the numbers that were randomly chosen for reference. They should not be outputted in your solution. i.i "00 is randomly chosen:" should not be displayed. – jacksonecac – 2016-10-12T14:24:25.760

That could use some clarification then. – Rɪᴋᴇʀ – 2016-10-12T15:09:40.753

1This 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 – 2016-10-12T16:10:54.990

@carusocomputing nothing, should have been omitted. – jacksonecac – 2016-10-12T18:04:54.380

@DLosc lets stick with xy so that its more clear what each location is. – jacksonecac – 2016-10-12T18:59:18.150

Your very first input 2 example seems incorrect, since it contains 10 twice and no 01. – Kevin Cruijssen – 2016-10-13T08:09:40.863

What do you mean by 'refresh' in "Refresh or print after each fill in order to show the progression of the filling iterations." – corvus_192 – 2016-10-13T20:03:40.163

@corvus_192 However you want to display the different iterations of the filling in the of grid. So if you fill in 1, 1. You would display the grid with 1,1 filled. Then if the next is 2, 3 then you would fill 2, 3 and display the new matrix with 1, 1 and 2, 3 filled. – jacksonecac – 2016-10-14T12:06:35.273

Answers

5

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

Emigna

Posted 2016-10-11T17:11:21.393

Reputation: 50 798

It looks awesome but as I test it, it seems like it doesn't fill every square? – jacksonecac – 2016-10-12T18:24:33.210

@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 – 2016-10-12T18:26:04.007

"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 – 2016-10-12T18:28:40.487

@jacksonecac Thanks for the clarification. I've updated the answer accordingly :) – Emigna – 2016-10-12T21:52:17.867

Perfect! Nice job man! – jacksonecac – 2016-10-13T11:08:35.283

3

Pip, 41 40 38 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)

DLosc

Posted 2016-10-11T17:11:21.393

Reputation: 21 213

Nice Job! That works perfectly – jacksonecac – 2016-10-12T18:54:46.777

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 – 2016-10-12T19:16:16.813

1@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 – 2016-10-12T19:20:28.740

##, got it. Nice use of assumptions. Thanks for the explanation haha. – Magic Octopus Urn – 2016-10-12T19:23:03.500

1

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 

Magic Octopus Urn

Posted 2016-10-11T17:11:21.393

Reputation: 19 422

the matrix should be NxN so a perfect square. – jacksonecac – 2016-10-12T18:47:40.837

@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 – 2016-10-12T18:49:03.753

If you want delimiters between the iterations, specify it in the brief. – Magic Octopus Urn – 2016-10-12T18:49:56.653

Here, try it with the newline added inbetween iterations: https://groovyconsole.appspot.com/edit/5171951567896576

– Magic Octopus Urn – 2016-10-12T18:51:40.803

I apologize I jumped to conclusions. Let me parse this out :D – jacksonecac – 2016-10-12T18:52:51.020

Nice job! thats awesome man – jacksonecac – 2016-10-12T19:00:21.350

Judging from the output, this answer sometimes fills in a spot that's already been filled in, contrary to OP's intentions: "check the random numbers... to make sure that spot is not already filled." – DLosc – 2016-10-12T19:55:17.733

1

R, 84 81 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"

rturnbull

Posted 2016-10-11T17:11:21.393

Reputation: 3 689

Nice Job! Go for it. Save those bytes! – jacksonecac – 2016-10-13T11:09:25.060

You can save a few bytes using direct substitution instead of replace: for(i in sample(N^2)){m[i]="XX";print(m)} – Billywob – 2016-10-13T13:57:27.740

@Billywob Thanks, I've edited the code to incorporate your suggestion. Great catch! – rturnbull – 2016-10-13T15:26:32.950

0

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

Robert Benson

Posted 2016-10-11T17:11:21.393

Reputation: 1 339

0

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
}

Jörg Hülsermann

Posted 2016-10-11T17:11:21.393

Reputation: 13 026

0

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()

Karl Napf

Posted 2016-10-11T17:11:21.393

Reputation: 4 131