So I managed to get part 1 of the day, but it took 2 seconds to run on the real input, which is a bad sign.

I can’t see any kind of optimisation that means I can skip checks and know how many combinations are in those skipped checks (aside from 0.) I can bail out of branches of combinations if the info so far won’t fit, but that still leads me to visiting every valid combination which in one of the examples is 500k. (And probably way more in the input, since if I can’t complete the example near instantly the input is not happening.)

Right now I take the string, then replace the first instance of a ? with the two possible options. Check it matches the check digits so far then use recursion on those two strings.

I can try to optimise the matching, but I don’t think that solves the real problem of visiting every combination.

I don’t think (or hope) it’s just bad code but this is my code so far (python.)

edit:

spoiler

a cache was the solution!

  • purplemonkeymad@programming.devOP
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    Memory is fine but I think I get what you mean. In the example:

    ????.###.????.### 1,3,1,3
    

    I’m checking the second unknowns combinations for each of the first, but if my state was say

    data: '...#.##.????.###'
    position -----^
    check_list: [1,3]
    

    And I get 4 combinations from recursion then, I know that is the same number of combination for any of the first unknowns.

    So I can then cache ".????.###",[1,3] -> 4.