It's quite simple.

@tc, I'll write you a program for it if you need. I bet that took you at least an hour...