9
\$\begingroup\$

This question already has an answer here:

Given an input of a pair of nonnegative integers describing a leaper in chess, output a diagram of the squares to which the leaper can move.

From Wikipedia's description:

An (m,n)-leaper is a piece that moves by a fixed type of vector between its start square and its arrival square. One of the coordinates of the vector 'start square – arrival square' must have an absolute value equal to m and the other one an absolute value equal to n. [...] For instance, the knight is the (1,2)-leaper.

In normal-people-speak, an (m,n)-leaper can move m squares in any direction, turn 90 degrees, and move n more squares. For example, a (1,2)-leaper (also known as the knight) can move to these squares:

.E.E.
E...E
..S..
E...E
.E.E.

where S represents the start square, E represents the end square, and . represents an empty square. This must also be your output for the input 1 2 (and 2 1).

Specifically, the output must

  • be a square grid with side length max(m,n) * 2 + 1.

  • contain exactly one S, located in the center of the square.

  • contain Es at the positions that a (m,n)-leaper at S could move to.

  • contain dots (.) at all other positions.

You may assume that m ≥ 0, n ≥ 0, m + n > 0, and that m and n are integers.

Output may be a single string, an array of lines, or an array of arrays of characters.

Since this is , the shortest code in bytes will win.

Test cases (all test cases should work with input integers swapped as well):

4 1
...E.E...
.........
.........
E.......E
....S....
E.......E
.........
.........
...E.E...

2 2
E...E
.....
..S..
.....
E...E

3 0
...E...
.......
.......
E..S..E
.......
.......
...E...

1 1
E.E
.S.
E.E

0 1
.E.
ESE
.E.

12 13
.E.......................E.
E.........................E
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
.............S.............
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
E.........................E
.E.......................E.
\$\endgroup\$

marked as duplicate by LegionMammal978, cat, feersum, Martin Ender code-golf Mar 5 '16 at 23:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
\$\begingroup\$

Javascript, 153 151 bytes

x=>y=>(G=(g,h)=>~[b-x,b+x][X='indexOf'](g)&&~[b-y,b+y][X](h),r=[...Array((b=x>y?x:y)*2+1)]).map((_,i)=>r.map((_,j)=>G(i,j)|G(j,i)?'E':b-i|b-j?'.':'S'))

Ungolfed with explanation:

f=                                     // saves the function in f
    x=>y=>(                            // currying the leaping numbers
        G=(g,h)=>                      // helper function
            ~[b-x,b+x][X='indexOf'](g) // check if g/x is in a goal square by x
                &&                     // and
            ~[b-y,b+y][X](h),          // if h/y is in a goal square by y
        r=[...Array(                   // row or column
            (b=x>y?x:y)*2+1            // of Math.max(x,y)*2+1 squares
        )]                             // stored in r
    ).map((_,i)=>                      // map r with i being the row index
        r.map((_,j)=>                  // for each row map a column with j being its index
            G(i,j)|G(j,i)              // if this square is one possible goal
                ?'E'                   // then 'E'
            :b-i|b-j                   // if is not the middle square
                ?'.'                   // then '.'
            :'S'                       // else 'S'
        )                              // end/next column setup
    )                                  // end function

P.addEventListener('click',_=>(
    a=I.value.split`,`,
    O.innerHTML=f(+a[0])(+a[1]).map(x=>x.join``).join`\n`
));
<input id=I value="1,2"><button id=P>Play</button><pre id=O>

\$\endgroup\$
  • \$\begingroup\$ I had a 145 byte answer but the question was closed before I could post it... \$\endgroup\$ – Neil Mar 5 '16 at 23:37

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