That's similar to Doug's suggestion.
FWIW, I did put pencil to paper yesterday for a while trying to figure out the best approach and so far it all does add up to "programming nightmare", but that's what I'm here for.
However, I might run across a simpler way to implement it.
On projects like this one, I find that time spent away from the computer with a legal pad and a pencil is the most productive time there is (used to have trouble convincing some clients of that -- for their money, they want to see you furiously banging out code the second you arrive) because even if I spend days doing this, I'll often find an approach that is orders of magnitude easier on the machine (I'm fanatical about this) and a lot easier to write.
It's often true that once I figure out a way to write something for maximum efficiency, it just falls into place at code-banging time because it's also the easiest way to write it.
So far, I haven't found that easy approach to this one, and might never find it.