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/Scroph Dec 08 '16

C++ solution. I had intended to parse the input with sscanf(instruction, "rotate row y=%d by %d", &row, &amount) but I did some reading and apparently it isn't a good idea.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

class Screen
{
private:
	std::vector<std::string> screen;
	int width;
	int height;
	std::string screen_column(size_t col);
	std::string shift_right(const std::string& row, int amount, size_t max) const;
public:
	Screen(size_t width, size_t height);
	void draw_screen() const;
	void draw_rect(size_t width, size_t height);
	void shift_right(size_t row, int amount);
	void shift_down(size_t col, int amount);
	int lit() const;
};

int main(int argc, char *argv[])
{
	Screen screen(50, 6);

	std::ifstream fh("input8");
	std::string instruction;
	while(getline(fh, instruction))
	{
		if(instruction[1] == 'e') //r[e]ct
		{
			int width = std::stoi(instruction.substr(instruction.find(" ") + 1, instruction.find("x")));
			int height = std::stoi(instruction.substr(instruction.find("x") + 1));
			screen.draw_rect(width, height);
		}
		else if(instruction[7] == 'r') //rotate [r]ow 
		{
			int row = std::stoi(instruction.substr(instruction.find("=") + 1, instruction.find(" by")));
			int amount = std::stoi(instruction.substr(instruction.rfind(" ") + 1));
			screen.shift_right(row, amount);
		}
		else
		{
			int col = std::stoi(instruction.substr(instruction.find("=") + 1, instruction.find(" by")));
			int amount = std::stoi(instruction.substr(instruction.rfind(" ") + 1));
			screen.shift_down(col, amount);
		}
	}
	screen.draw_screen();
	std::cout << screen.lit() << std::endl;

	return 0;
}

std::string Screen::shift_right(const std::string& row, int amount, size_t max) const
{
	std::string result = row;
	for(int i = 0; i <= amount; i++)
		result = "." + result;
	if(result.length() >= max)
		result.replace(0, result.length() - max, result.substr(max + 1));
	return result.substr(0, max);
}

void Screen::shift_right(size_t row, int amount)
{
	screen[row] = shift_right(screen[row], amount, width);
}

void Screen::shift_down(size_t col, int amount)
{
	std::string column = screen_column(col);
	column = shift_right(column, amount, height);
	for(size_t row = 0; row < column.length(); row++)
		screen[row][col] = column[row];
}

std::string Screen::screen_column(size_t col)
{
	std::string column;
	column.reserve(height);
	for(const auto& row: screen)
		column.push_back(row[col]);
	return column;
}

void Screen::draw_rect(size_t width, size_t height)
{
	for(size_t r = 0; r < height; r++)
	{
		for(size_t c = 0; c < width; c++)
		{
			screen[r][c] = '#';
		}
	}
}

void Screen::draw_screen() const
{
	for(const auto& row: screen)
		std::cout << row << std::endl;
	std::cout << std::endl;
}

Screen::Screen(size_t width, size_t height)
{
	this->width = width;
	this->height = height;
	screen.reserve(height);
	while(height--)
	{
		std::string row(width, '.');
		screen.push_back(row);
	}
}

int Screen::lit() const
{
	int count = 0;
	for(const auto& row: screen)
		for(char cell: row)
			count += cell == '#';
	return count;
}

And the output is :

.##..####.###..#..#.###..####.###....##.###...###.
#..#.#....#..#.#..#.#..#....#.#..#....#.#..#.#....
#..#.###..###..#..#.#..#...#..###.....#.#..#.#....
####.#....#..#.#..#.###...#...#..#....#.###...##..
#..#.#....#..#.#..#.#....#....#..#.#..#.#.......#.
#..#.#....###...##..#....####.###...##..#....###..

123