7 7{3::0⋄(⍋,(⊢⊃⍨⍵⍳⍨⊃∘⍸¨∘=)¯1⌂kt⍺)⊇,⍳⍺}⊂
Try it online!
A version provided by @Adám.
Dyalog Extended has a shortcut to the dfns
library ⌂
, and a few convenience functions e.g. monadic =
which is equivalent to 0∘=
, and dyadic ⊇
for multiple indexing. Unfortunately an inner assignment is buggy, so we use a tacit inner function to reference the value of ¯1⌂kt⍺
twice. Otherwise we'd have a 38 byte solution:
7 7{3::0⋄(⍋,w⊃⍨⍵⍳⍨⊃∘⍸¨=w←¯1⌂kt⍺)⊇,⍳⍺}⊂
How the inner tacit function (⊢⊃⍨⍵⍳⍨⊃∘⍸¨∘=)
works
This tacit function has 5 terms: `⊢` `⊃⍨` `⍵` `⍳⍨` `⊃∘⍸¨∘=`
A tacit function is grouped by 3 terms from the right:
⊢ ⊃⍨ (⍵ ⍳⍨ ⊃∘⍸¨∘=)
A group of 3 terms is interpreted as follows:
(f g h)w → (f w) g (h w) ⍝ f, g, h are functions
(A g h)w → (A ) g (h w) ⍝ A is an array
Then we can interpret the above tacit function as
(⊢ ⊃⍨ (⍵ ⍳⍨ ⊃∘⍸¨∘=))w
→ (⊢ w) ⊃⍨ (⍵ ⍳⍨ (⊃∘⍸¨∘= w)) ⍝ Expand tacit function
→ w ⊃⍨ ⍵ ⍳⍨ ⊃∘⍸¨∘= w ⍝ ⊢ is identity function; simplify parens
→ w ⊃⍨ ⍵ ⍳⍨ ⊃∘⍸¨ = w ⍝ (f∘g)w simply expands to f g w
So the code `(⊢⊃⍨⍵⍳⍨⊃∘⍸¨∘=)w` is equivalent to `w⊃⍨⍵⍳⍨⊃∘⍸¨=w`.
⎕CY'dfns'
7 7{3::0⋄(,⍳⍺)[⍋,w⊃⍨⍵⍳⍨⊃∘⍸¨0=w←¯1kt⍺]}⊂
Try it online!
Golfed 4 bytes by extracting some terms out of the dfn using a tacit form.
⎕CY'dfns'
{3::0⋄(,⍳7 7)[⍋,w⊃⍨(⊃∘⍸¨0=w←¯1 kt 7 7)⍳⊂⍵]}
Try it online!
Because we've got a language that has a Knight's tour solver built in.
Since ¯1 kt 7 7
tries to find all solutions, it's infeasible to actually run the code as-is. For demonstration purposes, the TIO link uses 3x4 board instead.
How it works
The function dfns.kt
finds a (possibly open) Knight's tour, where the right argument is the board's dimensions, and the optional left argument is the number of solutions to find. Giving -1
on the left causes the function to search all possible solutions.
⎕CY'dfns' ⍝ Import the 'dfns' workspace
{3::0⋄(,⍳7 7)[⍋,w⊃⍨(⊃∘⍸¨0=w←¯1 kt 7 7)⍳⊂⍵]} ⍝ ⍵←starting coordinates
w←¯1 kt 7 7 ⍝ Find ALL solutions of 7x7 knight's tour
(⊃∘⍸¨0= ) ⍝ Extract the starting coordinates from each solution
⍳⊂⍵ ⍝ Find the index of the solution that starts at ⍵
,w⊃⍨ ⍝ Fetch the solution and flatten it
(,⍳7 7) ⍝ Take the flattened list of coordinates on 7x7 board
[⍋ ] ⍝ Sort the coordinates by flattened solution
3::0⋄ ⍝ If there's no solution starting with ⍵,
⍝ catch the Index Error and return 0 instead
Every black square on 7x7 board can be the starting location of a Knight's tour.
The following isn't directly related to the code above, but nevertheless here is a proof by example.
Starting at (0,0), Ending at (5,1)
0 11 22 45 2 13 24
21 46 1 12 23 44 3
10 27 34 39 36 25 14
47 20 37 26 33 4 43
28 9 40 35 38 15 32
19 48 7 30 17 42 5
8 29 18 41 6 31 16
Starting at (1,3), Ending at (6,4)
20 27 10 33 44 1 12
9 32 21 0 11 34 45
26 19 28 43 40 13 2
31 8 41 22 29 46 35
18 25 30 39 42 3 14
7 38 23 16 5 36 47
24 17 6 37 48 15 4
Starting at (2,2), Ending at (3,3)
30 19 40 3 28 17 38
41 2 29 18 39 4 27
20 31 0 13 10 37 16
1 42 11 48 15 26 5
32 21 14 9 12 47 36
43 8 23 34 45 6 25
22 33 44 7 24 35 46
By swapping heads and tails, reflection, and rotation, all black squares can be the starting point of some Knight's tour.
I found these three tours by splitting the board into four groups that can form a continuous loop:
O O O . O O O | . . . O . . . | . . . . . . . | . . . . . . .
O . O O O . O | . O . . . O . | . . . . . . . | . . . . . . .
O O . . . O O | . . . . . . . | . . O O O . . | . . . . . . .
. O . . . O . | O . . . . . O | . . O . O . . | . . . O . . .
O O . . . O O | . . . . . . . | . . O O O . . | . . . . . . .
O . O O O . O | . O . . . O . | . . . . . . . | . . . . . . .
O O O . O O O | . . . O . . . | . . . . . . . | . . . . . . .
and breaking and gluing the chains whenever needed. It's kind of a heuristic, so it would be usually shorter to hardcode the three boards or just run naive DFS.
There are no closed tours for m×m boards with m odd
so it's not possible to take a single looped solution and simply start on a different square. That makes this reasonably interesting and not a direct duplicate of the linked question, which leaves the start square to choice. According to en.wikipedia.org/wiki/Knight's_tour there are 16557521832 directed tours on a 7x7 board so I imagine at least one starts on every square. I assume we only have to find one valid solution for the given input, not all of them! \$\endgroup\$