13
\$\begingroup\$

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!

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

13 Answers 13

7
\$\begingroup\$

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

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!

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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:

    -----
    -@@@-
    -@-@-
    -@-@-
    -@-@-
    -@@@-
    -----
    
\$\endgroup\$
3
\$\begingroup\$

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

Descending range

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

The minimum matrix

  • 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

\$\endgroup\$
2
\$\begingroup\$

Haskell, 64 bytes

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

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 20 bytes

NθE⊘⊕N⭆⊘⊕θ§ζ⌊⟦ιλ⟧‖O⌈

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

Nθ

Input the width.

E⊘⊕N⭆⊘⊕θ§ζ⌊⟦ιλ⟧

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.

‖O⌈

Reflect with overlap to complete the map.

Alternative approach, also 20 bytes:

NθE⮌…⁰N⭆⮌…⁰θ§ζ⌊⟦ικλμ

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

Nθ

Input the width.

E⮌…⁰N⭆⮌…⁰θ§ζ⌊⟦ικλμ

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.

\$\endgroup\$
2
\$\begingroup\$

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

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 36 28 bytes

NθNηW∧›θ⁰›η⁰«Bθη§ζⅈ↘≧⁻²θ≧⁻²η

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

Try it online (verbose) or try it online (pure).

Explanation:

Get the first two inputs as integers:

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

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

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

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()));
 Bθη§ζⅈ

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);
 ≧⁻²θ≧⁻²η
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Neil
    Feb 2, 2022 at 13:55
  • \$\begingroup\$ @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. \$\endgroup\$ Feb 2, 2022 at 14:28
1
\$\begingroup\$

Vyxal, 18 bytes

›½ɾΠvg:G$ṅ/vf⌊v∞∞İ

Try it Online!

How?

›½ɾΠvg:G$ṅ/vf⌊v∞∞İ
›                  # Increment both of the (implicit) first input
 ½                 # Halve both
  ɾ                # Generate a [1, n] range for both
   Π               # Cartesian product of the two
    vg             # Minimum of each
      :G           # Duplicate and get the maximum
        $          # Swap
         ṅ         # Join by nothing
          /        # Split this into that many equal parts
           vf      # Convert each back to a list
             ⌊     # Convert each to an integer
              v∞   # Palindromise each
                ∞  # Palindromise the list
                 İ # Index these into the (implicit) second input
\$\endgroup\$

Your Answer

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

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