r/adventofcode • • Dec 10 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 10 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 12 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 10: Adapter Array ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:08:42, megathread unlocked!

69 Upvotes

1.1k comments sorted by

View all comments

2

u/Rowward Dec 10 '20

Again first thought I had, recursion with storing known sub lists

import hashlib

l = [int(x.strip()) for x in open("day_10_input.txt","r")]


sorted_list = [0] + sorted(l) + [max(l)+3]

counter = [0, 0, 0, 0]

print(sorted_list)

for i in range(len(sorted_list)-1):
    counter[sorted_list[i+1]-sorted_list[i]] += 1

    print(sorted_list[i+1], sorted_list[i], sorted_list[i+1]-sorted_list[i])

print(counter[1] * counter[3])


known_paths = {}

def count_child_lists(l):

    # recursive stop
    if len(l) == 1:
        return 1

    # call this function for all possible follow ups
    sub_sum = 0
    i = 1
    current_element = l[0]


    while i < len(l) and l[i]-current_element <= 3:

        # calcuate hash for already visited paths
        hash = hashlib.md5("_".join([str(x) for x in l[i:]]).encode()).hexdigest()

        if hash not in known_paths:
            known_paths[hash] = count_child_lists(l[i:])

        sub_sum += known_paths[hash]

        i += 1
    return sub_sum


print(count_child_lists(sorted_list))

1

u/daggerdragon Dec 10 '20

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.

Also, add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.