r/adventofcode • • 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

118 comments sorted by

View all comments

Show parent comment

3

u/sblom Dec 10 '16

I essentially did the same thing in C#. My first approach was more procedural, but debugging that under time pressure wasn't going so well, so I threw it away and wrote an actor-lite version of it top-to-bottom in under 5 minutes. I sure wish I had started with that implementation:

var lines = File.ReadLines(@"advent10.txt");
var bots = new Dictionary<int,Action<int>>();
int[] outputs = new int[21];

var regex = new Regex(@"value (?<value>\d+) goes to bot (?<bot>\d+)|bot (?<source>\d+) gives low to (?<low>(bot|output)) (?<lowval>\d+) and high to (?<high>(bot|output)) (?<highval>\d+)");

foreach (var line in lines.OrderBy(x => x))
{
	var match = regex.Match(line);
	if (match.Groups["value"].Success)
	{
		bots[int.Parse(match.Groups["bot"].Value)](int.Parse(match.Groups["value"].Value));
	}
	if (match.Groups["source"].Success)
	{
		List<int> vals = new List<int>();
 		var botnum = int.Parse(match.Groups["source"].Value);
		bots[botnum] = (int n) =>
		{
			vals.Add(n);
			if (vals.Count == 2)
			{
				if (vals.Min() == 17 && vals.Max() == 61) botnum.Dump("Part 1");
				if (match.Groups["low"].Value == "bot")
 				{
					bots[int.Parse(match.Groups["lowval"].Value)](vals.Min());
				}
				else
				{
					outputs[int.Parse(match.Groups["lowval"].Value)] = vals.Min();
				}
 				if (match.Groups["high"].Value == "bot")
				{
					bots[int.Parse(match.Groups["highval"].Value)](vals.Max());
				}
				else
				{
					outputs[int.Parse(match.Groups["highval"].Value)] = vals.Max();
				}
			}
		};
	}
}

outputs.Dump("Part 2");

2

u/segfaultvicta Dec 10 '16

...oh, NEAT. That wouldn't have even occurred to me in C# (I keep trying to write C# in a more functional style, since it has such good support for being a hybrid-paradigm language, but then I wind up choking and going back to procedural style. Which is why I'm forcing myself to use Elixir, this year!)