13
\$\begingroup\$

A leaper is a category of fairy chess piece which moves by "jumping." A normal knight is a (1,2)-leaper, meaning each move involves moving a distance of 1 square in an orthogonal direction and 2 squares in the perpendicular direction.

.o.o.
o...o
..N..
o...o
.o.o.

There are many different leapers. The (1,3)-leaper is called the Long Knight, or Camel. Its move pattern looks like this:

..o.o..
.......
o.....o
...L...
o.....o
.......
..o.o..

There's also the (2,2) Alfil...

o...o
.....
..A..
.....
o...o

...and even the (0,1) Wazir.

.o.
oWo
.o.

Challenge

Given a pair of numbers as input, output the corresponding movement diagram. You may write a program or function, with input provided via STDIN/command-line or as an argument, and output provide by STDOUT or the return value. This is code-golf.

Input

Input will be a comma-separated list of two integers and an uppercase letter. The two integers will be in the range 0-7 (inclusive) and in non-decreasing order. Trailing newline optional for input and output.

Here are the inputs for the above four examples:

1,2,N
1,3,L
2,2,A
0,1,W

Output

Output will be a square-shaped multiline string. The capital letter will be placed in the center to represent the current location of the piece. The movement locations will be represented by either 4 or 8 lowercase os. All other spaces in the square will be filled with periods.

If the movement pattern is 0,0, output just the capital letter.

As a hint, if the second integer (the larger one) has value N, then the square will always have side length 2N+1. The os will always be on the perimeter of the square.

Additional Testcases

1,1,F

o.o
.F.
o.o

5,7,Q

..o.........o..
...............
o.............o
...............
...............
...............
...............
.......Q.......
...............
...............
...............
...............
o.............o
...............
..o.........o..


0,0,J

J
\$\endgroup\$
5
  • \$\begingroup\$ Why is the camel testcase letter a 'L'? I know out doesn't matter but it might be helpful to change out to a 'C'. \$\endgroup\$
    – Riker
    Jan 9, 2016 at 18:50
  • 1
    \$\begingroup\$ @RikerW My choice wasn't completely arbitrary, that's its "standardized" letter. \$\endgroup\$
    – PhiNotPi
    Jan 9, 2016 at 18:53
  • 1
    \$\begingroup\$ Okay. That makes sense. \$\endgroup\$
    – Riker
    Jan 9, 2016 at 18:54
  • 1
    \$\begingroup\$ For a function, are 3 arguments ok or do you want a single string argument comma separated? \$\endgroup\$
    – edc65
    Mar 6, 2016 at 10:43
  • \$\begingroup\$ @PhiNotPi where do you get the “standard” code. L is more commonly Bishop in German - and two letter codes are used for the fairy pieces \$\endgroup\$
    – Laska
    Dec 21, 2022 at 8:31

4 Answers 4

2
\$\begingroup\$

Ruby,107

->a,b,n{(-b..b).map{|i|s='..'*b+?.
i%b==0&&(i==0?s[b]=n :s[b+a]=s[b-a]=?o)
i.abs==a&&s[0]=s[-1]=?o
puts s}}

Ungolfed in test program

f=->a,b,n{
  (-b..b).map{|i|                           #iterate from -i to i (lines of grit)
    s='..'*b+?.                             #make a string of 2n+1 .'s
    i%b==0&&(i==0?s[b]=n :s[b+a]=s[b-a]=?o) #if i%b=0 plot the centre character (if i=0) or the o's on the top and bottom rows
    i.abs==a&&s[0]=s[-1]=?o                 #if i.abs=a plot the o's in left and right columns
    puts s                                  #having substituted the .'s with o and centre as necessary, output the current line
  }
}

a=gets.to_i
b=gets.to_i
n=gets.chop
f[a,b,n]
\$\endgroup\$
1
\$\begingroup\$

Pyth, 40 bytes

JASv<2zFZK+rH_1SHFY,LZKp?qJSY\o?sY\.ez)k

I'm learning Pyth! Try it out.

Explanation

J             J =
 A                (G, H) =
  S                        sorted(
   v<2z                           eval(input[:-2]))

FZK+rH_1SH    for Z in K = [H, H-1, ..., 0] + [1, 2, ..., H]:
  FY,LZK          for Y in [(Z, k) for k in K]:
    p                 print the following value without newline:
     ?qJSY\o              if J = sorted(Y): 'o'
       ?sY\.              if sum(Y) != 0:   '.'
         ez               else:             input[-1]
  )               end for
k                 print newline
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 163 161 145 bytes

(x,y,c,m=a=>`\n`+a.slice(1).reverse().join``+a.join``,a=Array(y+1).fill`.`,q=a.map(_=>[...a]))=>m(q.map(m,q[x][y]=q[y][x]='o',q[0][0]=c)).slice(2)

Where \n is the literal new line character. Works by generating the bottom right quadrant and mirroring it along both axes.

Edit: Saved 2 bytes thanks to @edc65.

(I got here via a duplicate question which allowed an array result which would have been 19 bytes shorter, but didn't guarantee nondecreasing order, which wasted 8 bytes.)

\$\endgroup\$
2
  • \$\begingroup\$ You can save 3 bytes using a template string with no brackets for fill and a literal newline \$\endgroup\$
    – edc65
    Mar 6, 2016 at 10:53
  • \$\begingroup\$ @edc65 I'd already tried to adjust for the literal newline (I always write "Where \n is the literal new line character" when I do that) but thanks for the other tip. \$\endgroup\$
    – Neil
    Mar 6, 2016 at 11:10
0
\$\begingroup\$

JavaScript (ES6) 144 150

(a,b,c,g=Array(b-~b).fill`.`)=>(g=g.map(x=>[...g])).map(x=>x.join``,[b-a,b+a].map(t=>g[t][0]=g[0][t]=g[t][2*b]=g[2*b][t]='o'),g[b][b]=c).join`
`

Less golfed

(a,b,c)=> {
  var g=Array(b*2+1).fill('.');
  g=g.map(x=>[...g]);
  var q=(t)=>
    g[t][0] =
    g[0][t] =
    g[t][2*b] =
    g[2*b][t] = 'o';
  q(b+a);
  q(b-a);
  g[b][b] = c;
  return g.map(x=> x.join('')).join('\n')
}

Test

f=(a,b,c,g=Array(b-~b).fill`.`)=>(g=g.map(x=>[...g])).map(x=>x.join``,[b-a,b+a].map(t=>g[t][0]=g[0][t]=g[t][2*b]=g[2*b][t]='o'),g[b][b]=c).join`
`

console.log=x=>O.textContent+=x+'\n'

t=`1,2,N
1,3,L
2,2,A
0,1,W
1,1,F
5,7,Q`.split`\n`
.forEach(t=>([x,y,z]=t.split`,`, console.log(t+'\n'+f(+x,+y,z)+'\n')))
<pre id=O></pre>

\$\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.