21
2
This is Hole-3 from The Autumn Tournament of APL CodeGolf. I am the original author of the problem there, and thus allowed to re-post it here.
Given:
a number of turns (please state if no movements is 0, otherwise we'll assume it is called 1) and
a list of one or more starting positions (in any form, e.g. 0 or 1 indexed coordinates or 64 consecutive numbers/characters or A1–H8 – state which), on an 8-by-8 chessboard,
return (in any order) the list of unique positions (in the same format as the input) that knight(s) can be at after the given number of turns.
Each knight must move with every turn, but you do not have to worry about multiple knights occupying the same square.
A knight can only move to the positions marked with X relative to its current position, marked with ♞:
Examples (1-indexed coordinates)
1
move from [[1,1]]
: [[2,3],[3,2]]
2
moves from [[1,1]]
: [[1,1],[1,3],[1,5],[2,4],[3,1],[3,5],[4,2],[4,4],[5,1],[5,3]]
1
move from [[1,1],[5,7]]
: [[2,3],[3,2],[3,6],[3,8],[4,5],[6,5],[7,6],[7,8]]
2
moves from [[1,1],[5,7]]
: [[1,1],[1,3],[1,5],[1,7],[2,4],[2,6],[2,8],[3,1],[3,3],[3,5],[3,7],[4,2],[4,4],[4,6],[4,8],[5,1],[5,3],[5,5],[5,7],[6,4],[6,6],[6,8],[7,3],[7,7],[8,4],[8,6],[8,8]]
0
moves from [[3,4]]
: [[3,4]]
Can chess spaces be inputted and outputted by numbering 0-63 instead of [rank, file]? – Dave – 2017-10-18T12:24:48.347
@Dave Sure, why not? Just be consistent with I/O. – Adám – 2017-10-18T12:26:23.073
8I swear I read this HNQ as "Where the knight be in Ni moves" – Sidney – 2017-10-18T15:23:13.953
3Pun alert: the notation for knight is N. – Joshua – 2017-10-18T15:54:36.797
Can we use 1-based indexing on the number of steps? E.g.
[[1,1]], 2 -> [[2,3],[3,2]]
– Zgarb – 2017-10-18T17:15:01.363@Zgarb Sure, why not? – Adám – 2017-10-18T17:15:34.763
I like how you made
[1,2]
NOT be a required input value, that would save a few bytes from people's solutions. – Zacharý – 2017-10-20T01:13:55.243@Zacharý In Dyalog APL 16.0 it is trivial to handle, in many languages, not so much. – Adám – 2017-10-20T07:18:21.693
Sorry, I was referring at least to some of my early submissions for the original problem. – Zacharý – 2017-10-20T19:54:41.607