11
\$\begingroup\$

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!

\$\endgroup\$
14
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Oct 11, 2016 at 17:49
  • 1
    \$\begingroup\$ 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 ? \$\endgroup\$
    – Ton Hospel
    Commented Oct 11, 2016 at 19:36
  • \$\begingroup\$ @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. \$\endgroup\$
    – ElPedro
    Commented Oct 11, 2016 at 19:38
  • \$\begingroup\$ @TonHospel Good point. I will edit to ensure n < 10 \$\endgroup\$ Commented Oct 12, 2016 at 11:10
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 12, 2016 at 16:10

9 Answers 9

5
\$\begingroup\$

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
\$\endgroup\$
5
  • \$\begingroup\$ It looks awesome but as I test it, it seems like it doesn't fill every square? \$\endgroup\$ Commented Oct 12, 2016 at 18:24
  • \$\begingroup\$ @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) \$\endgroup\$
    – Emigna
    Commented Oct 12, 2016 at 18:26
  • \$\begingroup\$ "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. \$\endgroup\$ Commented Oct 12, 2016 at 18:28
  • \$\begingroup\$ @jacksonecac Thanks for the clarification. I've updated the answer accordingly :) \$\endgroup\$
    – Emigna
    Commented Oct 12, 2016 at 21:52
  • \$\begingroup\$ Perfect! Nice job man! \$\endgroup\$ Commented Oct 13, 2016 at 11:08
3
\$\begingroup\$

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)
\$\endgroup\$
4
  • \$\begingroup\$ Nice Job! That works perfectly \$\endgroup\$ Commented Oct 12, 2016 at 18:54
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Oct 12, 2016 at 19:16
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – DLosc
    Commented Oct 12, 2016 at 19:20
  • \$\begingroup\$ ##, got it. Nice use of assumptions. Thanks for the explanation haha. \$\endgroup\$ Commented Oct 12, 2016 at 19:23
2
\$\begingroup\$

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 
\$\endgroup\$
7
  • \$\begingroup\$ the matrix should be NxN so a perfect square. \$\endgroup\$ Commented Oct 12, 2016 at 18:47
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Oct 12, 2016 at 18:49
  • \$\begingroup\$ If you want delimiters between the iterations, specify it in the brief. \$\endgroup\$ Commented Oct 12, 2016 at 18:49
  • \$\begingroup\$ Here, try it with the newline added inbetween iterations: groovyconsole.appspot.com/edit/5171951567896576 \$\endgroup\$ Commented Oct 12, 2016 at 18:51
  • \$\begingroup\$ I apologize I jumped to conclusions. Let me parse this out :D \$\endgroup\$ Commented Oct 12, 2016 at 18:52
1
\$\begingroup\$

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"
\$\endgroup\$
3
  • \$\begingroup\$ Nice Job! Go for it. Save those bytes! \$\endgroup\$ Commented Oct 13, 2016 at 11:09
  • \$\begingroup\$ You can save a few bytes using direct substitution instead of replace: for(i in sample(N^2)){m[i]="XX";print(m)} \$\endgroup\$
    – Billywob
    Commented Oct 13, 2016 at 13:57
  • \$\begingroup\$ @Billywob Thanks, I've edited the code to incorporate your suggestion. Great catch! \$\endgroup\$
    – rturnbull
    Commented Oct 13, 2016 at 15:26
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
}
\$\endgroup\$
0
\$\begingroup\$

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()
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog Unicode), 30 bytes

↑{'×'@⍵⊢x}¨,\⊂¨y[?⍨≢y←,x←⍳2/⎕]

Try it online!

Uses ⎕IO←0 (0-indexing)

Uses the × symbol for replacement, in a grid of co-ordinate pairs.

Explanation

↑{'×'@⍵⊢x}¨,\⊂¨y[?⍨≢y←,x←⍳2/⎕]
                            ⎕  get the input n
                         ⍳2/   create an n×n coordinate grid
                       x←      store in x
                    y←,        convert to a list, and store in y
                 ?⍨≢           get indexes for shuffling the list
               y[            ] and get a random permutation of y
             ⊂¨                wrap each list
           ,\                  and get it's array prefixes
 {       }¨                    for each prefix:
  '×'@⍵⊢x                      place an '×' at those coordinates in the grid x
↑                              align all the grids into a matrix
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 18 (or 17 or 19?) bytes

L<ãíJD.rĆvDIô,yðº:

Try it online.

The program above outputs each matrix on a separated line. If these matrices have to be pretty-printed, a » can be added before the ,: try it online. (Using the exact output format as the challenge description is 22 bytes: try it online.)

If we're allowed to output with 1-based indexing instead of 0-based, the < can be dropped for -1 byte: try it online.

Explanation:

L                   # Push a list in the range [1, (implicit) input]
 <                  # Optional: decrease each by 1 to change the range to [0, input)
  ã                 # Create all possible pairs of this list
   í                # Reverse each pair
    J               # Join each pair together to a single 2-digit string
     D              # Duplicate this list
      .r            # Pop and shuffle this copy
        Ć           # Enclose, appending it's own head
                    # (this is so the final iteration will print the empty matrix)
         v          # Loop `y` over each index in the shuffled list:
          D         #  Triplicate the unshuffled matrix
           Iô       #  Pop one copy, and split it into parts equal to the input
             »      #  Optional: join each inner list by spaces, and then each string by newlines
              ,     #  Pop and print this matrix with trailing newline
               y    #  Push the current random index
                ðº  #  Push a space, and mirror it to two spaces: "  "
                  : #  Replace the index in the list with these double spaces
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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