chunk in place

This commit is contained in:
Eric Mertens 2022-11-08 09:50:21 -08:00
parent 177e38cd14
commit 679dd89b87

View File

@ -4,51 +4,39 @@
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
#include <vector> #include <vector>
#include <string_view>
#include <aocpp/Startup.hpp> #include <aocpp/Startup.hpp>
namespace { namespace {
using Layer = std::array<char, 150>; auto Part1(std::string const& line) {
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 part1 = 0;
std::size_t seen = std::numeric_limits<std::size_t>::max(); auto seen = std::numeric_limits<std::size_t>::max();
for (auto & layer : layers) { for (auto it = line.begin();
auto zeros = std::count(layer.begin(), layer.end(), '0'); std::distance(it, line.end()) >= 150;
std::advance(it, 150))
{
auto zeros = std::count(it, it + 150, '0');
if (zeros < seen) { if (zeros < seen) {
seen = zeros; seen = zeros;
part1 part1
= std::count(layer.begin(), layer.end(), '1') = std::count(it, it + 150, '1')
* std::count(layer.begin(), layer.end(), '2'); * std::count(it, it + 150, '2');
} }
} }
return part1; return part1;
} }
auto Flatten(std::vector<Layer> const& layers) { auto Flatten(std::string const& line) {
Layer merged {}; std::string merged(150, ' ');
for (std::size_t i = 0; i < 150; i++) { for (std::size_t i = 0; i < 150; i++) {
for (auto & layer : layers) { for (auto it = line.begin();
if ('2' != layer[i]) { std::distance(it, line.end()) >= 150;
merged[i] = layer[i]; std::advance(it, 150))
{
if ('2' != it[i]) {
merged[i] = it[i];
break; break;
} }
} }
@ -56,7 +44,7 @@ auto Flatten(std::vector<Layer> const& layers) {
return merged; return merged;
} }
auto Draw(Layer picture) { auto Draw(std::string const& picture) {
for (std::size_t y = 0; y < 6; y++) { for (std::size_t y = 0; y < 6; y++) {
for (std::size_t x = 0; x < 25; x++) { for (std::size_t x = 0; x < 25; x++) {
std::cout << ('1' == picture[y*25+x] ? "" : ""); std::cout << ('1' == picture[y*25+x] ? "" : "");
@ -68,7 +56,8 @@ auto Draw(Layer picture) {
} // namespace } // namespace
auto main(int argc, char** argv) -> int { auto main(int argc, char** argv) -> int {
auto layers = SplitLayers(aocpp::Startup(argc, argv)); std::string line;
std::cout << "Part 1: " << Part1(layers) << std::endl; std::getline(aocpp::Startup(argc, argv), line);
Draw(Flatten(layers)); std::cout << "Part 1: " << Part1(line) << std::endl;
Draw(Flatten(line));
} }