The latest weekly solution in my "because why not" series of solutions, each in a different language because languages are fun.
I made this week's submission in haskell. Since I'm not Intelligent enough to actually develop the algo, Here's an initial prototype I made in python:
The haskell program uses the same logic, except with a recursive function.
Edit: fixed a logic bug. Thank you @ThatSmart for pointing it out
If the python program and haskell program give different answers, let me know!