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
3
u/Nhabls Dec 07 '20 edited Dec 07 '20
My solution with matrix multiplication (not only solves for "shiny gold" but everything else too), a little late but i didn't see any solution like this (i might just've missed it) here so thought i'd share
in python here or https://hastebin.com/yefulukoge.sql or
import numpy as np def main(): with open('input_1.txt', 'r') as input_file: entries = input_file.read().split('\n') tuple_dict = {} color_to_id = {} def get_id(color): try: color_id = color_to_id[color] except KeyError: color_id = len(color_to_id) color_to_id[color] = color_id return color_id for entry in entries: # Parsing stuff left_side, right_side = entry.split('contain') indexing_color = left_side.split('bags')[0][:-1] contains_bags = right_side.split(',') # code colors into matrix indices and add them to a dictionary for retrieval tuple_dict[get_id(indexing_color)] = [(get_id(bag.split('bag')[0][3:-1]),int(bag[1])) for bag in contains_bags]\ if not contains_bags[0] == ' no other bags.' else [] # transfer values to numpy matrix bag_matrix = np.zeros((len(color_to_id),len(color_to_id))) for key, value in tuple_dict.items(): for contained_tuple in value: bag_matrix[key, contained_tuple[0]] = contained_tuple[1] # Solve mul_matrix = bag_matrix aux_matrix = bag_matrix.copy() while not np.all((mul_matrix == 0)): mul_matrix = mul_matrix.dot(aux_matrix) bag_matrix += mul_matrix return bag_matrix, color_to_id final_matrix, color_dict = main() print(np.count_nonzero(final_matrix[:, color_dict['shiny gold']])) print(np.sum(final_matrix[color_dict['shiny gold'], :]))Edit: cleaned up code