Skip to content

Commit

Permalink
C++14 Day 8 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
vincmg committed Dec 8, 2016
1 parent b55501b commit dea6da0
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
77 changes: 77 additions & 0 deletions 2016/c++14/08/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Advent of Code 2016 Day 8

#include <iostream>
#include <vector>
#include <string>
#include <utility>

#include "screen.hpp"

enum class Instr{ rect, rotate_col, rotate_row, invalid_instr };

Instr getInstr(const std::string& line)
{
if (line.find("rect") == 0) return Instr::rect;
if (line.find("column") != std::string::npos) return Instr::rotate_col;
if (line.find("row") != std::string::npos) return Instr::rotate_row;

// else say it's an invalid line
return Instr::invalid_instr;
}

std::pair<int, int> getParams(const std::string& line, Instr instrType)
{
if (instrType == Instr::rect) {
// too lazy to type out std::string::size_type
auto instrEnd{line.find(' ')};
auto paramSeparator{line.find('x')};

std::string row{line.substr(instrEnd + 1, paramSeparator - instrEnd - 1)};
std::string col{line.substr(paramSeparator + 1, line.size() - paramSeparator)};

return std::pair<int, int>(std::stoi(row), std::stoi(col));

} else if (instrType == Instr::rotate_col || instrType == Instr::rotate_row) {
// too lazy to type out std::string::size_type
auto rowOrColBegin{line.find('=')};
auto amtBegin{line.find("by ")};


std::string rowOrCol{line.substr(rowOrColBegin + 1, amtBegin - rowOrColBegin - 2)};
std::string amt{line.substr(amtBegin + 3, line.size() - amtBegin + 3)};

return std::pair<int, int>(std::stoi(rowOrCol), std::stoi(amt));

} else {
return std::pair<int, int>(-1, -1); // should probably throw an exception but eh
}
}

void runInstr(Screen& s, Instr instrType, const std::pair<int, int>& params)
{
if (instrType == Instr::rect) {
s.rect(params.first, params.second);
} else if (instrType == Instr::rotate_col) {
s.rotateCol(params.first, params.second);
} else if (instrType == Instr::rotate_row) {
s.rotateRow(params.first, params.second);
}
}

int main()
{
Screen s;

std::string line;
while (std::getline(std::cin, line)) {
Instr instrType{getInstr(line)};
std::pair<int, int> params{getParams(line, instrType)};

runInstr(s, instrType, params);
}

s.displayScreen();
std::cout << "Number of on pixels: " << s.numPixelsOn() << std::endl;

return 0;
}
70 changes: 70 additions & 0 deletions 2016/c++14/08/screen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Advent of Code 2016 Day 8

#include "screen.hpp"

#include <iostream>

const int Screen::ROWLENGTH{50};
const int Screen::COLLENGTH{6};

Screen::Screen() : screen{}
{
for (auto& row : screen) {
row = std::array<char, 50>();
for (auto& col : row) {
col = '.';
}
}
}

void Screen::rect(int cols, int rows)
{
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
screen[i][j] = '#';
}
}
}

void Screen::rotateCol(int col, int amt)
{
for (int i = 0; i < amt; ++i) {
for (int row = COLLENGTH - 1; row > 0; --row) {
char temp{screen[row][col]};
screen[row][col] = screen[row - 1][col];
screen[row - 1][col] = temp;
}
}
}

void Screen::rotateRow(int row, int amt)
{
for (int i = 0; i < amt; ++i) {
for (int col = ROWLENGTH - 1; col > 0; --col) {
char temp{screen[row][col]};
screen[row][col] = screen[row][col - 1];
screen[row][col - 1] = temp;
}
}
}

int Screen::numPixelsOn()
{
int count{0};
for (const auto& row : screen) {
for (const auto& pixel : row) {
if (pixel != '.') ++count;
}
}
return count;
}

void Screen::displayScreen()
{
for (const auto& row : screen) {
for (const auto& pixel : row) {
std::cout << pixel;
}
std::cout << std::endl;
}
}
19 changes: 19 additions & 0 deletions 2016/c++14/08/screen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Advent of Code 2016 Day 8

#include <array>

class Screen {
public:
Screen();
void rect(int cols, int rows);
void rotateCol(int col, int amt);
void rotateRow(int row, int amt);

void displayScreen();
int numPixelsOn();

private:
static const int ROWLENGTH;
static const int COLLENGTH;
std::array<std::array<char, 50>, 6> screen;
};

0 comments on commit dea6da0

Please sign in to comment.