r/adventofcode • • Dec 08 '16

SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---

#AoC_Ops:

[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION

We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg


--- Day 8: Two-Factor Authentication ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


:(){ :|:& };: 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!

10 Upvotes

197 comments sorted by

View all comments

1

u/JakDrako Dec 08 '16

C#, LINQPad

void Main()
{
	int w = 50, h = 6;
	int[,] grid = new int[h, w];

	Func<string, int> ci32 = x => Convert.ToInt32(x);

	foreach (var line in input().Split('\r'))
	{
		var tok = line.Trim().Split(' ');
		if (tok[0] == "rect")
		{
			var xy = tok[1].Split('x');
			for (int x = 0; x < ci32(xy[0]); x++)
				for (int y = 0; y < ci32(xy[1]); y++)
					grid[y, x] = 1;
		}
		else if (tok[1] == "column") ColDown (ref grid, ci32(tok[4]), ci32(tok[6]));
		else if (tok[1] == "row")    RowRight(ref grid, ci32(tok[4]), ci32(tok[6]));
	}
	grid.Cast<int>().Sum(x => x).Dump("Part 1");
	String.Join("", grid.Cast<int>().Select((x, i) => (x == 1 ? "##" : "  ") + ((i + 1) % w == 0 ? "\n" : ""))).Dump("Part 2");
}

void ColDown(ref int[,] grid, int col, int dst)
{
	var h = grid.GetUpperBound(0);
	for (int i = 0; i < dst; i++)
	{
		var tmp = grid[h, col];
		for (int y = h; y >= 1; y--) grid[y, col] = grid[y - 1, col];
		grid[0, col] = tmp;
	}
}

void RowRight(ref int[,] grid, int row, int dst)
{
	var w = grid.GetUpperBound(1);
	for (int i = 0; i < dst; i++)
	{
		var tmp = grid[row, w];
		for (int x = w; x >= 1; x--) grid[row, x] = grid[row, x - 1];
		grid[row, 0] = tmp;
	}
}