Short of using a pencil and paper as the answer I imagine you could write a program.
First step is to map out a plan - perhaps even pseudocode.
After that a bit of coding based on the plan.
Show us something along those lines and come back. We need something we 'can poke a stick at' rather than just general advice or worse ... do it for you, an absolute no-no. :)