solved 12, ineligantly, but it is solved...
refactorsed 012, it is less disgusting now. However, the algorithm itself could do with some inventive optimization.