Used Ruby to solve this problem