# Mat Printing Matrix

A manufacturing company wants to print a design on mats of varying dimensions, and they hired you to program a robot to make these mats. The design consists of alternating rings of any 2 symbols on a mat. Below are some sample looks:

Column 9 by Row 7

Symbol 1: @

Symbol 2: -

Input: 9 7 @ -

@@@@@@@@@
@-------@
@-@@@@@-@
@-@---@-@
@-@@@@@-@
@-------@
@@@@@@@@@


Column 13 by Row 5

Symbol 1: @

Symbol 2: -

Input: 13 5 @ -

@@@@@@@@@@@@@
@-----------@
@-@@@@@@@@@-@
@-----------@
@@@@@@@@@@@@@


Column 3 by Row 5

Symbol 1: $Symbol 2: + Input: 3 5$ +

$$+ + +$$\$


Column 1 by Row 1

Symbol 1: #

Symbol 2: )

#


Write a program that takes in the length, breadth, symbol 1 and symbol 2 and prints out the mat design on the screen.

*The row and column number is always odd

Shortest code wins!

• Similar. Jan 18 at 7:31
• No worries, it's a quite a different challenge. Jan 18 at 7:39
• Did you mean to tag this code-challenge? Jan 18 at 8:20
• @RiccardoBucco I'm not OP, but yes: see this pastebin. The first symbol will always be the outer ring, going inwards. Jan 18 at 15:06
• Is this once again a challenge taken from codingame?
– xnor
Jan 18 at 15:10

# Jelly, 9 bytes

Takes the length,breadth as the first argument and the symbols as a second argument.

«þ/U«ṚƊịY


Try it online!

«þ/        -- Minimum table of the left argument
Ɗ    -- On that table:
U«Ṛ     --   Minimum of each row reversed and the columns reversed
ị   -- Index into the symbols (modular indexing)
Y  -- Join by newlines


# Pari/GP, 86 bytes

f(a,b,c,d)=[print(concat(r~))|r<-matrix(a,b,i,j,[c,d][vecmin([i-1,j-1,a-i,b-j])%2+1])]


Try it online!

# R, 85 bytes

function(a,b,s,t,m=matrix(t,b,a),r=row(m),c=col(m)){m[!pmin(b-r,a-c,r-1,c-1)%%2]=s;m}


Try it online!

Idea of pmin inspired by @alephalpha's vecmin.

# J, 18 17 bytes

{~2|<./&(<.&i.-)/


Try it online!

Note: Explanation needs slight update but idea is the same as current solution

Consider 7 5:

• <./&i./ First uses &i. to generate both 0 1 2 3 4 5 6 and 0 1 2 3 4, and the creates a "mininum" function table:

0 0 0 0 0
0 1 1 1 1
0 1 2 2 2
0 1 2 3 3
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4

• - Note using i. on a negative number counts down, so i. _3 is 2 1 0.

• -<.&(...)] Is a fork which applies the verb from step to both 7 5 and _7 _5, and then takes the min. So min of:

 0 0 0 0 0       4 3 2 1 0
0 1 1 1 1       4 3 2 1 0
0 1 2 2 2  min  4 3 2 1 0
0 1 2 3 3       3 3 2 1 0
0 1 2 3 4       2 2 2 1 0
0 1 2 3 4       1 1 1 1 0
0 1 2 3 4       0 0 0 0 0


which gives:

0 0 0 0 0
0 1 1 1 0
0 1 2 1 0
0 1 2 1 0
0 1 2 1 0
0 1 1 1 0
0 0 0 0 0

• 2| Then we mod by 2:

0 0 0 0 0
0 1 1 1 0
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
0 1 1 1 0
0 0 0 0 0

• {~ And map to chars:

-----
-@@@-
-@-@-
-@-@-
-@-@-
-@@@-
-----


# ayr-0, 17 14 bytes

Since the 05Ab1e answer takes the input [row, column], I can get -3 bytes. Images and try it link outdated, just replace the code with this snippet.

~2||:v:\/@v:~


Try it!

# Explained

-0 makes ~ and |: (descending range) 0-indexed

The program A (~2||:v:\/@v:~) B is equivalent to A ~ 2 | (v:\/ |: B) v: v:\/ ~ B

### How to get there:

Expansion of the train (lowercase = fn, uppercase = var):

• B (f A g h i j) C
• B (f (A g h i j)) C
• B (f (A g (h i j))) C
• B f (A g (h i j)) C
• B f (A g ((h i j) C))
• B f (A g ((h C) i (j C)))

Substituting in the symbols:
B ~ (2 | ((|: C) v:\/@v: (~ C)))

Focusing on the fork (h i j):
v:\/@v: could be written as f \ / @ g
which is g @ (f \ /)
A g @ f B is (f A) g f B
so (|: C) v:\/@v: (~ C) is (v:\/ |: C) v: (v:\/ ~ C)

Therefore
A (~2||:v:\/@v:~) B is equivalent to A ~ 2 | (v:\/ |: B) v: v:\/ ~ B

### Which leads to the explanation:

A is the string, B is the dims

• |: ... ~ Take the descending and ascending 0-ranges of each element of B

• v:\/ Convert these each separately to a minimum matrix of shape B

• v: Take the minimum of these two matrices on an element-wise basis

• 2| Mod 2 each element

• ~ Use the matrix as indices into string A

# Python3, 240 bytes

b,a,c,d=input().split()
g=range
a,b=int(a),int(b)
r=[[d]*b for i in g(a)]
x=y=0
while 1:
for i in g(x,a-x):r[i][y]=r[i][b-1-y]=c
for j in g(y,b-y):r[x][j]=r[a-1-x][j]=c
x+=2;y+=2
if x>=a/2or y>=b/2:break
print('\n'.join(map(''.join,r)))


Try it online!

(x!y)q=[[cycle q!!minimum[k,1+x-k,j,1+y-j]|j<-[1..y]]|k<-[1..x]]


Try it online!

Also works with even sized mats and more than two character patterns.

# Charcoal, 20 bytes

ＮθＥ⊘⊕Ｎ⭆⊘⊕θ§ζ⌊⟦ιλ⟧‖Ｏ⌈


Try it online! Link is to verbose version of code. Takes the two symbols as a single argument. Explanation:

Ｎθ


Input the width.

Ｅ⊘⊕Ｎ⭆⊘⊕θ§ζ⌊⟦ιλ⟧


Input the height and loop over the top left quarter of the mat, outputting the symbols cyclically indexed by the minimum of the row and column.

‖Ｏ⌈


Reflect with overlap to complete the map.

Alternative approach, also 20 bytes:

ＮθＥ⮌…⁰Ｎ⭆⮌…⁰θ§ζ⌊⟦ικλμ


Try it online! Link is to verbose version of code. Takes the two symbols as a single argument. Explanation:

Ｎθ


Input the width.

Ｅ⮌…⁰Ｎ⭆⮌…⁰θ§ζ⌊⟦ικλμ


Input the height and loop over the mat, reversing the ranges of the row and column, outputting the symbols cyclically indexed by the minimum of the row and column and reversed row and column.

# 05AB1E, 13 bytes

>;Lâ€ßZä€ûûè


First input is a pair of $$\[row,column]\$$, second input is a pair of $$\[symbol2,symbol1]\$$. Output is a matrix of characters.

Try it online (footer J» is to pretty-print; feel free to remove it to see the actual output-matrix).

Explanation:

>; # Increase both values in the first (implicit) input-pair by 1, then halve
#  e.g. [5,13] → [6,14] → [3,7]
L  # Map both to a list in the range [1,n]
#  → [[1,2,3],[1,2,3,4,5,6,7]]
  # Pop and push both separated to the stack
#  → [1,2,3] and [1,2,3,4,5,6,7]
â  # Get the cartesian product of these two lists
#  → [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[2,1],[2,2],[2,3],[2,4],[2,5],
#     [2,6],[2,7],[3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7]]
€ß # Get the minimum of each inner pair
#  → [1,1,1,1,1,1,1,1,2,2,2,2,2,2,1,2,3,3,3,3,3]
Z  # Push the flattened maximum (without popping the list)
#  → [1,1,1,1,1,1,1,1,2,2,2,2,2,2,1,2,3,3,3,3,3] and 3
ä  # Split the list into that many equal-sized parts so we have a matrix
#  → [[1,1,1,1,1,1,1],[1,2,2,2,2,2,2],[1,2,3,3,3,3,3]]
€û # Palindromize each row
#  → [[1,1,1,1,1,1,1,1,1,1,1,1,1],
#     [1,2,2,2,2,2,2,2,2,2,2,2,1],
#     [1,2,3,3,3,3,3,3,3,3,3,2,1]]
û  # Palindromize the entire matrix
#  → [[1,1,1,1,1,1,1,1,1,1,1,1,1],
#     [1,2,2,2,2,2,2,2,2,2,2,2,1],
#     [1,2,3,3,3,3,3,3,3,3,3,2,1],
#     [1,2,2,2,2,2,2,2,2,2,2,2,1],
#     [1,1,1,1,1,1,1,1,1,1,1,1,1]]
è  # 0-based modulair index each into the (implicit) second input-string
#  e.g. "-@" →
#    [["@","@","@","@","@","@","@","@","@","@","@","@","@"],
#     ["@","-","-","-","-","-","-","-","-","-","-","-","@"],
#     ["@","-","@","@","@","@","@","@","@","@","@","-","@"],
#     ["@","-","-","-","-","-","-","-","-","-","-","-","@"],
#     ["@","@","@","@","@","@","@","@","@","@","@","@","@"]]
# (after which the matrix is output implicitly)


# APL+WIN, 22 bytes

Prompts for column then row as integers and the symbols as string. Index origin = 0

⎕[2|n⌊⌽⍉⌽⍉n←(⍳⎕)∘.⌊⍳⎕]


Try it online!Thanks to Dyalog Classic

# Python 3, 174 bytes

w,h,x,y=input().split()
n='\n'
f=lambda x,y,w,h:x*w+n+x+f(y,x,w-2,h-2).replace(n,x+n+x)+x+n+x*w if w*h-w-h+1else[n.join(x*(h-w+1)),x*(w-h+1)][w>h]
print(f(x,y,int(w),int(h)))


Try it online!

# Charcoal, 36 28 bytes

ＮθＮηＷ∧›θ⁰›η⁰«Ｂθη§ζⅈ↘≧⁻²θ≧⁻²η


-10 bytes thanks to @Neil and +2 bytes for a bug-fix.

Explanation:

Get the first two inputs as integers:

InputNumber(q);InputNumber(h);
ＮθＮη


Continue looping while both q and h are still larger than 0:

While(And(Greater(q,0),Greater(h,0)){ ... }
Ｗ∧›θ⁰›η⁰« ...


Print a box with dimensions q by h, with the X'th character of the third input-string z as border, where X is the current x-position of the cursor:

 Box(q,h,AtIndex(z,X()));
Ｂθη§ζⅈ


Move the cursor once towards the bottom-right:

 Move(:DownRight);
↘


Decrement both q and h by 2:

 MapAssignRight(Minus,2,q);MapAssignRight(Minus,2,h);
≧⁻²θ≧⁻²η

• Doesn't work for odd square matrices unfortunately. You can however use either of X() or Y() instead of a to keep track of which character to print, and you can use MapAssignRight(Minus,2,<variable>);` to save a byte over an assignment, which hopefully will make up for any bytes you need for your bugfix.
– Neil
Feb 2 at 13:55
• @Neil Thanks for the -10! And good call on the bug of odd square grids due to my multiply.. Fixed at the cost of 2 bytes. Feb 2 at 14:28