r/adventofcode • u/daggerdragon • Dec 10 '16
SOLUTION MEGATHREAD --- 2016 Day 10 Solutions ---
--- Day 10: Balance Bots ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".
SEEING MOMMY KISSING SANTA CLAUS IS MANDATORY [?]
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!
12
Upvotes
View all comments
Show parent comment
6
u/askalski Dec 10 '16
To understand how it works, you first need to understand the structure of the input. For example, this post; note there's an error in the graph; scroll down and look for a corrected version by /u/p_tseng in the comments.
The heart of the script is the
tsort(topological sort) command, which it uses to find the order (low to high) of the row of bots connected to the outputs. It then maps those bot numbers to the output numbers, handling the special case of the one bot that has two outputs. This sorts the outputs from low to high.With that done, it sorts the input
valuesand joins them to the sorted list of outputs. Now that it has values mapped to outputs, you should be able to figure out the rest.The script depends heavily on the structure of the input; if you want to see a more general purpose shell implementation, look at my other thread.
tsortis one of the lesser-known and underappreciated utilities in thecoreutilssuite, and is worth learning. I also used it as part of a solution for 2015 Day 7 (logic gates), to figure out the order in which to evaluate the input.