The algorithm for Water Logic