#include #include #include #include #include #include #include namespace { using Layer = std::array; auto SplitLayers(std::istream & in) { std::string line; std::getline(in, line); std::vector 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 const& layers) { std::size_t part1 = 0; std::size_t seen = std::numeric_limits::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 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)); }