aocpp/2019/08.cpp

75 lines
1.6 KiB
C++
Raw Normal View History

2022-11-07 21:29:13 -08:00
#include <algorithm>
#include <cstdint>
#include <array>
#include <iostream>
#include <iterator>
#include <vector>
#include <aocpp/Startup.hpp>
namespace {
using Layer = std::array<char, 150>;
auto SplitLayers(std::istream & in) {
std::string line;
std::getline(in, line);
std::vector<Layer> layers;
auto it = line.begin();
auto layer_n = line.size() / 150;
for (std::size_t layer = 0; layer < layer_n; layer++) {
layers.push_back({});
for (std::size_t y = 0; y < 150; y++) {
layers.back()[y] = *it++;
}
}
return layers;
}
auto Part1(std::vector<Layer> const& layers) {
std::size_t part1 = 0;
std::size_t seen = std::numeric_limits<std::size_t>::max();
for (auto & layer : layers) {
auto zeros = std::count(layer.begin(), layer.end(), '0');
if (zeros < seen) {
seen = zeros;
part1
= std::count(layer.begin(), layer.end(), '1')
* std::count(layer.begin(), layer.end(), '2');
}
}
return part1;
}
auto Flatten(std::vector<Layer> const& layers) {
Layer merged {};
for (std::size_t i = 0; i < 150; i++) {
for (auto & layer : layers) {
if ('2' != layer[i]) {
merged[i] = layer[i];
break;
}
}
}
return merged;
}
auto Draw(Layer picture) {
for (std::size_t y = 0; y < 6; y++) {
for (std::size_t x = 0; x < 25; x++) {
std::cout << ('1' == picture[y*25+x] ? "" : "");
}
std::cout << std::endl;
}
}
} // namespace
auto main(int argc, char** argv) -> int {
auto layers = SplitLayers(aocpp::Startup(argc, argv));
std::cout << "Part 1: " << Part1(layers) << std::endl;
Draw(Flatten(layers));
}