Solution for Reddit Daily Programmer Challenge