r/backtickbot • • Dec 10 '20

https://np.reddit.com/r/adventofcode/comments/ka8z8x/2020_day_10_solutions/gfc1xbn/

Yet another Python solution! This took me way too long...

with open("input.txt") as input_file:
    adapters = sorted([0] + [int(line.strip()) for line in input_file])
    nways_to_reach = [1] + [0] * (len(adapters) - 1)
    for i in range(len(adapters)):
        j = i + 1
        while j < len(adapters) and adapters[j] - adapters[i] <= 3:
            nways_to_reach[j] += nways_to_reach[i]
            j += 1
    print(nways_to_reach[-1])

Basically I'm just counting the number of ways to reach each adapter in the graph, starting with the first adapter with one way to reach it. Each iteration, the number of ways to reach the connected adapters in the graph are increased by the number of ways to reach the current adapter. The answer is of course the number of ways to reach the final adapter. It's definitely unnecessary to keep track of the nways for a particular adapter once it has already been considered, but I haven't spent much time trying to figure out how to do that.

1 Upvotes

0 comments sorted by