34
2
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
2
What's with chess knights today? :-D
– Luis Mendo – 2016-04-18T19:03:05.6001Hint: If you write the numbers out as wrapping line, then the knight is always jumping either four spaces clockwise or four spaces counter. I don't know if this is helpful. – Fund Monica's Lawsuit – 2016-04-18T19:05:04.290
3@LuisMendo Wrapping. As in, if you treat is as an endless list of
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. – Fund Monica's Lawsuit – 2016-04-18T19:37:55.983@QPaysTaxes Oh, I thought you meant circle but
123...9
. Sorry – Luis Mendo – 2016-04-18T19:41:16.527@LuisMendo No worries. Like I said, I should've been clearer about what I meant. – Fund Monica's Lawsuit – 2016-04-18T19:42:29.837
What is the expected behavior for
0
? Is it truthy just like the other 1-digit numbers? – Value Ink – 2016-04-18T20:02:19.703@KevinLau "Write a program or function that takes in a positive decimal integer" - i.e. you don't need to worry about 0. – Calvin's Hobbies – 2016-04-18T20:03:26.620
@HelkaHomba - Yes, but a number with more than 1 digit could have 0's and still be positive. I assume you are ruling those out? – Darrel Hoffman – 2016-04-18T21:38:39.763
@DarrelHoffman No. Some falsy examples clearly contain zeroes. Zero itself is not considered, but numbers with zeros cannot be ignored. – Calvin's Hobbies – 2016-04-18T21:40:48.677
@HelkaHomba - That's what I meant - the presence of a 0 automatically rules out the number, i.e. makes it not a knight-number. It'd be an interesting twist to allow 0, but have two of them since the 0-key is two keys wide, so you could go 305, or 406, but not 405 because you're on the wrong side of the 0 key. This would invalidate a lot of answers though, so that'd be an entirely different challenge. – Darrel Hoffman – 2016-04-18T21:52:04.740