Tri-Hexagonal Puzzle - Algorithm?

Can someone help me to deduce the algorithm for http://www.codechef.com/problems/N4 problem?

//