chunk in place
This commit is contained in:
parent
177e38cd14
commit
679dd89b87
57
2019/08.cpp
57
2019/08.cpp
@ -4,51 +4,39 @@
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
|
||||
#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) {
|
||||
auto Part1(std::string const& line) {
|
||||
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');
|
||||
auto seen = std::numeric_limits<std::size_t>::max();
|
||||
for (auto it = line.begin();
|
||||
std::distance(it, line.end()) >= 150;
|
||||
std::advance(it, 150))
|
||||
{
|
||||
auto zeros = std::count(it, it + 150, '0');
|
||||
if (zeros < seen) {
|
||||
seen = zeros;
|
||||
part1
|
||||
= std::count(layer.begin(), layer.end(), '1')
|
||||
* std::count(layer.begin(), layer.end(), '2');
|
||||
= std::count(it, it + 150, '1')
|
||||
* std::count(it, it + 150, '2');
|
||||
}
|
||||
}
|
||||
return part1;
|
||||
}
|
||||
|
||||
auto Flatten(std::vector<Layer> const& layers) {
|
||||
Layer merged {};
|
||||
auto Flatten(std::string const& line) {
|
||||
std::string merged(150, ' ');
|
||||
for (std::size_t i = 0; i < 150; i++) {
|
||||
for (auto & layer : layers) {
|
||||
if ('2' != layer[i]) {
|
||||
merged[i] = layer[i];
|
||||
for (auto it = line.begin();
|
||||
std::distance(it, line.end()) >= 150;
|
||||
std::advance(it, 150))
|
||||
{
|
||||
if ('2' != it[i]) {
|
||||
merged[i] = it[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -56,7 +44,7 @@ auto Flatten(std::vector<Layer> const& layers) {
|
||||
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 x = 0; x < 25; x++) {
|
||||
std::cout << ('1' == picture[y*25+x] ? "▓" : "░");
|
||||
@ -68,7 +56,8 @@ auto Draw(Layer picture) {
|
||||
} // 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));
|
||||
std::string line;
|
||||
std::getline(aocpp::Startup(argc, argv), line);
|
||||
std::cout << "Part 1: " << Part1(line) << std::endl;
|
||||
Draw(Flatten(line));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user