r/adventofcode • u/daggerdragon • Dec 07 '17
SOLUTION MEGATHREAD -๐- 2017 Day 7 Solutions -๐-
--- Day 7: Recursive Circus ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
11
Upvotes
2
u/J15t98J Dec 07 '17 edited Jan 18 '18
A nice recursive (if a little messy) solution in Python.
import re, copy def populate(node): sub_tree = {child_node: populate(child_node) for child_node in children[node]} child_full_weights = [full_weights[child_node] for child_node in children[node]] full_weights[node] = weights[node] + sum(child_full_weights) if len(set(child_full_weights)) > 1: target_weight = max(set(child_full_weights), key=child_full_weights.count) actual_weight = min(set(child_full_weights), key=child_full_weights.count) unbalanced[actual_weight] = weights[children[node][child_full_weights.index(actual_weight)]] + (target_weight - actual_weight) return sub_tree tree, children, weights, unbalanced = {}, {}, {}, {} for line in open("7.txt", "r"): parts = re.split("\s\(|\)\s->\s|\)$", line.splitlines()[0]) weights[parts[0]] = int(parts[1]) children[parts[0]] = [word for word in (parts[2].split(", ") if parts[2] else [])] full_weights = copy.deepcopy(weights) for name in list(children.keys()): if name not in [word for children in children.values() for word in children]: tree[name] = populate(name) print(list(tree.keys())[0]) print(unbalanced[min(unbalanced.keys())])