Solution with clever diagonal traversal implementation