19
\$\begingroup\$

Your program should compute the number of moves it takes a chess knight to reach each square of the chess board. The input will be two space-separated integers representing coordinates of the starting square (horizontal then vertical coordinate, 0-7 inclusive each). Your program should output a number grid containing the minimum number of moves a chess knight needs to make to reach each square.

examples

input

0 0

output

03232345
34123434
21432345
32323434
23234345
34343454
43434545
54545456

input

3 1

output

21232123
32303232
21232123
34121432
23232323
32323234
43434343
34343434

Shortest code wins.

\$\endgroup\$

5 Answers 5

5
+500
\$\begingroup\$

APL (Dyalog Classic), 55 bytes

{⍵⌊d⍴1+⌊/¨⌷∘⍵¨¨a/⍨¨Ö↓2=×/↑|∘.-⍨a←,⍳d}⍣≡0@(⊂⌽⎕)⊢9⍴⍨d←8 8
{⍵⌊d⍴1+⌊/¨⌷∘⍵¨¨a/⍨¨Ö↓2=×/↑|∘.-⍨a←,⍳d}⍣≡0@(⊂⌽⎕)⊢9⍴⍨d←8 8
                                       0@(⊂⌽⎕)⊢9⍴⍨d←8 8 ⍝ create an 8x8 grid of 9s with a 0 at the start position
                           ∘.-⍨a←,⍳d                    ⍝ all differences between coordinates
                     2=×/↑|                             ⍝ is 2 equal to the product of the absolute values?
               a/⍨¨Ö↓                                   ⍝ filter where true (finds knight moves)
          ⌷∘⍵¨¨                                         ⍝ index into the board
     1+⌊/¨                                              ⍝ 1 plus the minimum (finds minimum distance)
   d⍴                                                   ⍝ 8x8 reshape
 ⍵⌊                                                     ⍝ the minimum of the new and old moves
{                                   }⍣≡                 ⍝ apply until converged (minimum distances found)

Try it online!

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

Ruby 1.9, 146 151 characters

g=(?9*8+".
")*8
r=->x,a=0{x<0||a<g[x].to_i&&(g[x]=a.to_s;[21,19,12,8].map{|i|r[x+i,a+1];r[x-i,a+1]})}
r[eval gets.split*?++"*10"]
puts g.tr(?.,"")
\$\endgroup\$
4
\$\begingroup\$

Haskell, 255 236 231 229 bytes

import Data.List
k x y=unlines[[toEnum$findIndices(elem(i,j))(scanl(\s _->filter(\(z,w)->z`elem`n&&w`elem`n)$(\(a,b)->[(a+c,b+d)|(c,d)<-zip[1,1,-1,-1,-2,-2,2,2][2,-2,2,-2,1,-1,1,-1]])=<<s)[(x,y)]n)!!0+48|j<-n]|i<-n]where n=[0..7]

D:

This is my first attempt at golfing. Also somewhat new to Haskell.

Test suite:

import System.Environment

main :: IO ()
main = do
    args <- getArgs
    let readArgs = map read args
    let out = k (readArgs !! 0) (readArgs !! 1)
    putStr out
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to the site! Good first golf. If you want some additional ideas for Haskell golfing, check out codegolf.stackexchange.com/questions/19255/… \$\endgroup\$
    – isaacg
    Commented May 13, 2017 at 6:25
  • \$\begingroup\$ map(\(c,d)->...)$zip ... can be shortened to zipWith(\c d->...).... \$\endgroup\$
    – Laikoni
    Commented May 13, 2017 at 11:50
2
\$\begingroup\$

Windows PowerShell, 178 183 188

filter f($n){if($d[($p=$_)]-gt$n){$d[$p]=$n
12,8,21,19|%{$p+$_
$p-$_}|f($n+1)}}$d=,0*20+(0..7|%{,9*8+0,0})+,0*20
$x,$y=-split$input
20+"$y$x"|f 0
2..9|%{-join$d[(10*$_).."$_`7"]}

Passes both test cases.

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

JavaScript, 426 408 bytes

for(a=[],i=0;i<8;i++){a[i]=[];for(j=0;j<8;j++)a[i][j]=99}m=[[2,1],[2,-1],
[-2,1],[-2,-1],[1,2],[-1,2],[1,-2],[-1,-2]];function s(f,g,e,b){b&&(a[f][g]=0);
for(var b=[],c=0;c<m.length;c++){var d=[f+m[c][0],g+m[c][1]];a[d[0]]&&
a[d[0]][d[1]]&&a[d[0]][d[1]]>e&&(a[d[0]][d[1]]=e,b.push(d))}for(c=0;c<b.length;c++)
s(b[c][0],b[c][1],e+1)}function _(f,g){s(g,f,1,1);for(e="",b=0;b<8;b++)e+=
a[b].join("")+"\n";return e}

JavaScript is not the most concise language out there... But my coding style is a little verbose too.

Usage: _(0, 0) etc.

\$\endgroup\$
3
  • \$\begingroup\$ Take out all those vars and save yourself tons of space... \$\endgroup\$
    – Ry-
    Commented May 26, 2011 at 22:36
  • \$\begingroup\$ @minitech: It's a recursive function so that messes everything up... Some vars could be removed though, thanks. \$\endgroup\$
    – pimvdb
    Commented May 27, 2011 at 14:38
  • \$\begingroup\$ Convert some of your functions to arrow functions (ES6) to save bytes \$\endgroup\$
    – user100690
    Commented Mar 17, 2021 at 11:15

Your Answer

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

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