Saving the princess on HackerRank.com