Commit eb1a61f2 authored by amiessen's avatar amiessen
Browse files

added day 8, part 2

parent 335c51a5
......@@ -32,22 +32,25 @@ void readImageData(const std::string & data_path,
int getPixelNumber(const std::string & imageLayer, const int dim,
const int intLookedFor)
{
// in imageLayer, get number of pixels equal to intLookedFor
std::vector<int> imagePixels(dim);
int count = 0;
for (int j = 0; j < dim; j++) {
// get j'th character of i'th image string, convert char to int
imagePixels[j] = imageLayer[j] - '0';
if (imagePixels[j] == intLookedFor) { count++; }
if ((imageLayer[j] - '0') == intLookedFor) { count++; }
}
return count;
}
int getImLayer(const std::vector<std::string> & images, const int dim,
int getImLayer(const std::vector<std::string> & layers, const int dim,
const int intLookedFor, const std::string & minMax)
{
// get image layer (index in std::vector layers) that contains fewest/most
// (specified by minMax) number of pixels equal to intLookedFor
std::vector<int> imagePixels(dim);
int layerIndex = 0;
......@@ -61,8 +64,8 @@ int getImLayer(const std::vector<std::string> & images, const int dim,
return 1;
}
for (size_t i = 0; i < images.size(); i++) {
int count = getPixelNumber(images[i], dim, intLookedFor);
for (size_t i = 0; i < layers.size(); i++) {
int count = getPixelNumber(layers[i], dim, intLookedFor);
if (minMax == "min" && count < minMaxCount) {
minMaxCount = count;
......@@ -78,10 +81,10 @@ int getImLayer(const std::vector<std::string> & images, const int dim,
}
void checkImage(const std::vector<std::string> & images, const int dim) {
int layerFewest0 = getImLayer(images, dim, 0, "min");
int count1 = getPixelNumber(images[layerFewest0], dim, 1);
int count2 = getPixelNumber(images[layerFewest0], dim, 2);
void checkImage(const std::vector<std::string> & layers, const int dim) {
int layerFewest0 = getImLayer(layers, dim, 0, "min");
int count1 = getPixelNumber(layers[layerFewest0], dim, 1);
int count2 = getPixelNumber(layers[layerFewest0], dim, 2);
std::cout << "Image layer " << layerFewest0 << " contains fewest 0s (zeros)\n";
std::cout << "(number of 1-pixels) x (number of 2-pixels) on that layer: ";
......@@ -89,6 +92,41 @@ void checkImage(const std::vector<std::string> & images, const int dim) {
}
void render(const std::vector<std::string> & layers, const int dim,
std::vector<int> & finalImage)
{
// get finalImage by combining all image layers with pixel ints
// 2 = transparent, 1 = black, 0 = white.
// looping through all layers, the first pixel at position i containing
// a 1 or a 0 (i.e., different from 2) is copied to the i'th pixel in
// finalImage.
finalImage.resize(dim, 2);
for (int j = 0; j < dim; j++) {
int i = 0;
while (finalImage[j] == 2 && i < layers.size()) {
if (layers[i][j] != '2') { finalImage[j] = layers[i][j] - '0'; }
i++;
}
}
}
void printImage(const std::vector<int> & finalImage, const int nx, const int ny)
{
for (int i = 0; i < ny; i++) {
for (int j = 0; j < nx; j++) {
if (finalImage[j + i*nx] == 0 || finalImage[j + i*nx] == 2)
std::cout << " ";
else if (finalImage[j + i*nx] == 1)
std::cout << "0";
}
std::cout << "\n";
}
}
int main() {
std::string data_path = "in08.txt";
......@@ -97,9 +135,18 @@ int main() {
int ny = 6;
int dim = nx*ny;
std::vector<std::string> images;
std::vector<std::string> layers;
readImageData(data_path, layers, dim);
std::cout << "\n - - - PART 1 - - - \n";
std::cout << "check for image corrption:\n";
checkImage(layers, dim);
readImageData(data_path, images, dim);
std::cout << "\n - - - PART 2 - - - \n";
std::cout << "final (rendered) image reads:\n\n";
std::vector<int> finalImage;
render(layers, dim, finalImage);
checkImage(images, dim);
printImage(finalImage, nx, ny);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment