Solution to https://www.hackerrank.com/challenges/saveprincess