Since 2010

Advent of Code 2020 day 10 part 2

Advent of Code 2020 day 10 part two. Not in the mood for this one. My solution has been stolen from /u/ald_loop’s incredibly thorough and generous explanation.

No amount of stepping through this line-by-line in a debugger has lifted the brain-fog. Obviously the code is programming-101 stuff, it’s simply that I’m unable to link the logic to the problem and the solution.

I’ll revisit this in the future, but for now I’m just going to let it go.

def part_two(parsed_input):
    # Add charging outlet and device to list of adapters
    parsed_input += [0, max(parsed_input)+3]
    parsed_input = sorted(parsed_input)

    dp = [0]*len(parsed_input)
    # There's only one path to the charging outlet
    dp[0] = 1
    for i in range(1, len(parsed_input)):
        sm = 0
        for j in range(1,4): # 1, 2, 3
            if i-j < 0:
                # Skip out-of-range checks at the start of the process
            if parsed_input[i]-parsed_input[i-j] <= 3:
                # Candidate adapter
                sm += dp[i-j]
        dp[i] = sm
    return dp[-1]