Skip to main content
added 178 characters in body
Source Link
M--
  • 33.6k
  • 12
  • 74
  • 115

R, 175 169169 155 bytes

\(v){s=sort(v);g=matrixg=matrix(,6,6);C=T
for(is in 36:1sort(v,T)){
T>1&&g[T-1,C]==s[i]|C>1&&g[TC]==s|C>1&&g[T,C-1]==s[i]&&return1]==s&&return()
g[T,C]=s[i];T=T+1;C=CC]=s;T=T+1;C=C-1
if(T>6|C<1){C=T+C;T=1;C>6&&{T=T+C-6;C=6}}}
g}

Attempt This Online!Attempt This Online!

\(v) {                      # v = vector of 36 numbers to arrange in 6x6 grid
  sg = sortmatrix(v, 6, 6)        # create 6x6 grid initialized #with sortNA vvalues
 in ascendingC order= (reversedT loop handles descending)
  g = matrix(, 6, 6)      # create 6x6 grid initialized with NA# C = column index (shorterstarts thanas 0TRUE = 1)
  C = T                   # C = column index, # T = row index (both start asimplicitly TRUE = 1, will be reassigned)
  
  for(is in 36:1sort(v, T)) {    # iterate through v #sorted iteratein backwardsdescending throughorder
 indices                           # sort(effectivelyv, descendingT) ordermeans sort(v, decreasing=TRUE)
                            # s takes each sorted value directly
    
    T > 1 &&                #   if not in first row AND
    g[T-1, C] == s[i]s          #   value directly above equals current value (not strictly descending)
    |                       #   OR
    C > 1 &&                #   if not in first column AND  
    g[T, C-1] == s[i]s          #   value directly to left equals current value (not strictly descending)
    && return()             #   then return NULL (impossibleno solution - not strictly descending)
    
    g[T, C] = s[i]s             #   place current value in grid at position (T, C)
    T = T + 1               #   move down one row (diagonal movementfilling pattern)
    C = C - 1               #   move left one column (diagonal movementfilling pattern)
    
    if(T > 6 | C < 1) {     #   if we've gone offmoved theoutside grid boundaries:
      C = T + C           #  #   calculate next diagonal'scalculate starting column for next diagonal
      T = 1                 #     reset to first row
      C > 6 &&              #     if columncalculated iscolumn beyondexceeds grid width:
      {T = T + C - 6        #       adjustshift row down by overflow amount
       C = 6}               #       set column to lastrightmost columnposition (6)
    }
  }
  g                         # return the completed grid (if possible)
}

R, 175 169 bytes

\(v){s=sort(v);g=matrix(,6,6);C=T
for(i in 36:1){
T>1&&g[T-1,C]==s[i]|C>1&&g[T,C-1]==s[i]&&return()
g[T,C]=s[i];T=T+1;C=C-1
if(T>6|C<1){C=T+C;T=1;C>6&&{T=T+C-6;C=6}}}
g}

Attempt This Online!

\(v) {                    # v = vector of 36 numbers to arrange
  s = sort(v)             # sort v in ascending order (reversed loop handles descending)
  g = matrix(, 6, 6)      # create 6x6 grid initialized with NA (shorter than 0)
  C = T                   # C = column index, T = row index (both start as TRUE = 1)
  
  for(i in 36:1) {        # iterate backwards through indices (effectively descending order)
    T > 1 &&              #   if not in first row AND
    g[T-1, C] == s[i]     #   value above equals current value (not strictly descending)
    |                     #   OR
    C > 1 &&              #   if not in first column AND  
    g[T, C-1] == s[i]     #   value to left equals current value (not strictly descending)
    && return()           #   then return NULL (impossible)
    
    g[T, C] = s[i]        #   place current value in grid at position (T, C)
    T = T + 1             #   move down one row (diagonal movement)
    C = C - 1             #   move left one column (diagonal movement)
    
    if(T > 6 | C < 1) {   #   if we've gone off the grid boundaries:
      C = T + C           #     calculate next diagonal's starting column
      T = 1               #     reset to first row
      C > 6 &&            #     if column is beyond grid:
      {T = T + C - 6      #       adjust row by overflow amount
       C = 6}             #       set column to last column
    }
  }
  g                       # return the completed grid
}

R, 175 169 155 bytes

\(v){g=matrix(,6,6);C=T
for(s in sort(v,T)){
T>1&&g[T-1,C]==s|C>1&&g[T,C-1]==s&&return()
g[T,C]=s;T=T+1;C=C-1
if(T>6|C<1){C=T+C;T=1;C>6&&{T=T+C-6;C=6}}}
g}

Attempt This Online!

\(v) {                      # v = vector of 36 numbers to arrange in 6x6 grid
  g = matrix(, 6, 6)        # create 6x6 grid initialized with NA values
  C = T                     # C = column index (starts as TRUE = 1)
                            # T = row index (implicitly TRUE = 1, will be reassigned)
  
  for(s in sort(v, T)) {    # iterate through v sorted in descending order
                            # sort(v, T) means sort(v, decreasing=TRUE)
                            # s takes each sorted value directly
    
    T > 1 &&                #   if not in first row AND
    g[T-1, C] == s          #   value directly above equals current value
    |                       #   OR
    C > 1 &&                #   if not in first column AND
    g[T, C-1] == s          #   value directly to left equals current value
    && return()             #   then return NULL (no solution - not strictly descending)
    
    g[T, C] = s             #   place current value at position (T, C)
    T = T + 1               #   move down one row (diagonal filling pattern)
    C = C - 1               #   move left one column (diagonal filling pattern)
    
    if(T > 6 | C < 1) {     #   if we've moved outside grid boundaries:
      C = T + C             #     calculate starting column for next diagonal
      T = 1                 #     reset to first row
      C > 6 &&              #     if calculated column exceeds grid width:
      {T = T + C - 6        #       shift row down by overflow amount
       C = 6}               #       set column to rightmost position (6)
    }
  }
  g                         # return the completed grid (if possible)
}
added 1264 characters in body; deleted 3 characters in body
Source Link
M--
  • 33.6k
  • 12
  • 74
  • 115

This is very similar to Nanigashi's approach. Going diagonally and filling the matrix.Expanded version with comments:

Note: It's way past my bedtime now, but I will try to add an expanded version tomorrow.

\(v) {                    # v = vector of 36 numbers to arrange
  s = sort(v)             # sort v in ascending order (reversed loop handles descending)
  g = matrix(, 6, 6)      # create 6x6 grid initialized with NA (shorter than 0)
  C = T                   # C = column index, T = row index (both start as TRUE = 1)
  
  for(i in 36:1) {        # iterate backwards through indices (effectively descending order)
    T > 1 &&              #   if not in first row AND
    g[T-1, C] == s[i]     #   value above equals current value (not strictly descending)
    |                     #   OR
    C > 1 &&              #   if not in first column AND  
    g[T, C-1] == s[i]     #   value to left equals current value (not strictly descending)
    && return()           #   then return NULL (impossible)
    
    g[T, C] = s[i]        #   place current value in grid at position (T, C)
    T = T + 1             #   move down one row (diagonal movement)
    C = C - 1             #   move left one column (diagonal movement)
    
    if(T > 6 | C < 1) {   #   if we've gone off the grid boundaries:
      C = T + C           #     calculate next diagonal's starting column
      T = 1               #     reset to first row
      C > 6 &&            #     if column is beyond grid:
      {T = T + C - 6      #       adjust row by overflow amount
       C = 6}             #       set column to last column
    }
  }
  g                       # return the completed grid
}

This is very similar to Nanigashi's approach. Going diagonally and filling the matrix.

Note: It's way past my bedtime now, but I will try to add an expanded version tomorrow.

Expanded version with comments:

\(v) {                    # v = vector of 36 numbers to arrange
  s = sort(v)             # sort v in ascending order (reversed loop handles descending)
  g = matrix(, 6, 6)      # create 6x6 grid initialized with NA (shorter than 0)
  C = T                   # C = column index, T = row index (both start as TRUE = 1)
  
  for(i in 36:1) {        # iterate backwards through indices (effectively descending order)
    T > 1 &&              #   if not in first row AND
    g[T-1, C] == s[i]     #   value above equals current value (not strictly descending)
    |                     #   OR
    C > 1 &&              #   if not in first column AND  
    g[T, C-1] == s[i]     #   value to left equals current value (not strictly descending)
    && return()           #   then return NULL (impossible)
    
    g[T, C] = s[i]        #   place current value in grid at position (T, C)
    T = T + 1             #   move down one row (diagonal movement)
    C = C - 1             #   move left one column (diagonal movement)
    
    if(T > 6 | C < 1) {   #   if we've gone off the grid boundaries:
      C = T + C           #     calculate next diagonal's starting column
      T = 1               #     reset to first row
      C > 6 &&            #     if column is beyond grid:
      {T = T + C - 6      #       adjust row by overflow amount
       C = 6}             #       set column to last column
    }
  }
  g                       # return the completed grid
}
edited body; deleted 3 characters in body
Source Link
M--
  • 33.6k
  • 12
  • 74
  • 115

Here's a code golf solution:

R, 175175 169 bytes

\(v){s=rev(sorts=sort(v));g=matrix(0,6,6);C=T
for(i in 1:36:1){
T>1&&g[T-1,C]==s[i]|C>1&&g[T,C-1]==s[i]&&return()
g[T,C]=s[i];T=T+1;C=C-1
if(T>6|C<1){C=T+C;T=1;C>6&&{T=T+C-6;C=6}}}
g}

Attempt This Online!Attempt This Online!

This is very similar to Nanigashi's approach. Going diagonally and filling the matrix.

Note: It's way past my bedtime now, but I will try to add an expanded version tomorrow.

Here's a code golf solution:

R, 175 bytes

\(v){s=rev(sort(v));g=matrix(0,6,6);C=T
for(i in 1:36){
T>1&&g[T-1,C]==s[i]|C>1&&g[T,C-1]==s[i]&&return()
g[T,C]=s[i];T=T+1;C=C-1
if(T>6|C<1){C=T+C;T=1;C>6&&{T=T+C-6;C=6}}}
g}

Attempt This Online!

This is very similar to Nanigashi's approach. Going diagonally and filling the matrix.

Note: It's way past my bedtime now, but I will try to add an expanded version tomorrow.

Here's a code golf solution:

R, 175 169 bytes

\(v){s=sort(v);g=matrix(,6,6);C=T
for(i in 36:1){
T>1&&g[T-1,C]==s[i]|C>1&&g[T,C-1]==s[i]&&return()
g[T,C]=s[i];T=T+1;C=C-1
if(T>6|C<1){C=T+C;T=1;C>6&&{T=T+C-6;C=6}}}
g}

Attempt This Online!

This is very similar to Nanigashi's approach. Going diagonally and filling the matrix.

Note: It's way past my bedtime now, but I will try to add an expanded version tomorrow.

Source Link
M--
  • 33.6k
  • 12
  • 74
  • 115
Loading