For the non-zero digits on a standard numpad
789
456
123
consider placing a chess knight at any digit and moving it around with any number of normal L-shaped jumps, tracing out a positive decimal integer. What positive integers can be expressed in such a way?
One of them is 38
, since the knight could start on the 3
and move left and up to the 8
. 381
and 383
are also possible.
3
itself is possible if no jumps are taken (which is allowed). 5
is as well, but no other digits can be reached from the 5
, so it is the only number where the digit 5
appears.
Write a program or function that takes in a positive decimal integer (you may take it as a string if desired) and prints or returns a truthy value if the number can be expressed by a knight on a numpad in the way described, but otherwise outputs a falsy value.
The shortest code in bytes wins. Tiebreaker is earlier answer
Examples
Truthy:
1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 18, 38, 61, 81, 294, 349, 381, 383, 729, 767, 38183, 38383, 18349276, 183492761, 618349276
Falsy:
10, 11, 50, 53, 55, 65, 95, 100, 180, 182, 184, 185, 186, 187, 188, 189, 209, 305, 2009, 5030, 3838384, 4838383, 183492760
78963214
, repeated over and over. Count the distances – it's always four, one way or the other. I should've been clearer and explicitly said that you have to write it in circle order. \$\endgroup\$123...9
. Sorry \$\endgroup\$