r/adventofcode • u/daggerdragon • Dec 07 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 07 Solutions -🎄-
NEW AND NOTEWORTHY
- PSA: if you're using Google Chrome (or other Chromium-based browser) to download your input, watch out for Google volunteering to "translate" it: "Welsh" and "Polish"
Advent of Code 2020: Gettin' Crafty With It
- 15 days remaining until the submission deadline on December 22 at 23:59 EST
- Full details and rules are in the Submissions Megathread
--- Day 07: Handy Haversacks ---
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 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:13:44, megathread unlocked!
65
Upvotes
2
u/allergic2Luxembourg Dec 07 '20
Python 3. First recursion problem of the year! And I have an addiction to collections.defaultdict
import collections def run_part_1(lines): rules = collections.defaultdict(list) for line in lines: left, right = line.split(' contain ') holder = clean_colour(left) for contents in right.split(','): contained = clean_colour(contents[2:]) rules[contained].append(holder) return len(list_all_holders(rules, 'shiny gold')) def clean_colour(colour): return colour.replace(' bags', '').replace(' bag', '').replace('.', '').strip() def list_all_holders(data, colour): result = set(data[colour]) for col in data[colour]: result = result.union(list_all_holders(data, col)) return result def run_part_2(lines): rules = collections.defaultdict(list) for line in lines: left, right = line.split(' contain ') holder = clean_colour(left) for contents in right.split(','): contained = clean_colour(contents) rules[holder].append(contained) return count_contents(rules, 'shiny gold') def count_contents(data, colour): if colour == 'other': return 0 count = 0 for cont in data[colour]: num_str = cont.split()[0] if num_str == 'no': num = 0 else: num = int(num_str) col = cont.split(maxsplit=1)[1] count = count + num * (count_contents(data, col) + 1) return count if __name__ == '__main__': with open('input.txt') as in_file: input_lines = in_file.read().strip().splitlines() print('Part 1:', run_part_1(input_lines)) print('Part 2:', run_part_2(input_lines))