Code Golf: Find the possible ways on a numpad

Posted by ikar on Stack Overflow See other posts from Stack Overflow or by ikar
Published on 2010-04-13T13:52:47Z Indexed on 2010/04/13 14:03 UTC
Read the original article Hit count: 238

Filed under:

I was bored today at school and so I tried to amuse myself using my calculator and a "game" I've invented which isn't really a game but keeps the boringness away. Also some time has passed since the last real code-golf here, so I decided to create this one.

Imagine a simplified numpad like you know it from your phone (I'll leave the 0 out for this code-golf as it kinda destroys all the fun)

1 2 3
4 5 6
7 8 9

Now the rules of the game were always:

  • At the end every digit must have been visited exactly once
  • You can start at any digit you want
  • You can always move one digit up, down, left or right. You can't move diagonally!

There a quite a lot of possible ways (or not; I haven't found out yet), here some trivial examples:

> > v
v < <
> > |

The output of the golf-program should look something like the above, I'll try to explain:

Symbols:

  • > Go right
  • < Go left
  • ^ Go up
  • v Go down
  • | End of the way

Example solutions:

(Program output can either be the numbers pressed
in the right order from beginning point to end, or an (ASCII) picture like above)

147852369

569874123

523698741

So if we speak out the example above it would be:

Start at 1, move right to 2, move right to 3, go down to 6, go left to 5, go left to 4, go down to 7, go right to 8 then go right to 9 and we are finished!

Now there are many different ways possible: You could as well start at 5 and go around it in a circle.

So the task would be: Write a program that can compute (using brute-force or whatever) the possible solutions for the numpad problem described above.

(Friendly rethorical question with smiley removed because it made some people think that this is homework)

© Stack Overflow or respective owner

Related posts about code-golf

  • Code Golf: Collatz Conjecture

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Inspired by http://xkcd.com/710/ here is a code golf for it. The Challenge Given a positive integer greater than 0, print out the hailstone sequence for that number. The Hailstone Sequence See Wikipedia for more detail.. If the number is even, divide it by two. If the number is odd, triple… >>> More

  • Code Golf - p day

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The Challenge The shortest code by character count to display a representation of a circle of radius R using the *character, followed by an approximation of p. Input is a single number, R. Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. The approximation… >>> More

  • Code Golf - PI day

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The Challenge The shortest code by character count to display a representation of a circle of radius R using the *character. Followed by an approximation of pi Input is a single number, R Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. The approximation… >>> More

  • Code Golf: Triforce

    as seen on Stack Overflow - Search for 'Stack Overflow'
    This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives you a numeric input ranging from 1 to 7. Input should be taken from the console, arguments are less desirable. When the input is 1, print the following: *********** *********… >>> More

  • Code Golf: Tic Tac Toe

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Post your shortest code, by character count, to check if a player has won, and if so, which. Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where: 0 = nothing set 1 = player 1 (X) 2 = player 2 (O) So, given the array b… >>> More