Your solution seemed very smart, and I have no doubt that you are an excellent programmer. However, I think you way over-complicated things by choosing to go the functional route. Here is my go in imperative Python. The base-15 trick is still needed, but the rest of it seems a lot easier to understand for someone reading the code later.
https://pastebin.com/trD9Ezf1