Skip to content

Instantly share code, notes, and snippets.

@fmad
Last active December 16, 2023 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmad/afb7644f1c4a16e2e21a7c099e083598 to your computer and use it in GitHub Desktop.
Save fmad/afb7644f1c4a16e2e21a7c099e083598 to your computer and use it in GitHub Desktop.
Number 12
Instead of trying to see which combinations map to the desired outcome,
I did it the other way around: look at the desired outcome and stop as soon as we can't match.
E.g.: ?###???????? 3,2,1
It HAS to start with a group of 3, so starting from the 1st, it could be possible, so I'd imagine that's a #.
However, that quickly rules it out as it then would be 4 consecutive # and we need to to start with a 3, so we can now safely
skip to position 1, the 1st #, and ignore all possible combinations for position 0 as they will not work.
Then, looking at position 1, we see we have exactly 3 #, so that could work, so we can try again, this time with a subset of
what we have left, i.e., we can safely skip ?###? and the first 3, so now we look for combinations with ??????? 2,1 to see
possible options. We keep doing this, recursively, until we run out of options but this avoids doing all trillion or quintillion
combinations as we can often break up early.
To speed it up further, I cache the outcome, e.e., if ?#? 2 would end up with, say, 2 combinations that work, I cache that,
so the next time I try to compute this for another part, I can simply lookup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment