This commit is contained in:
Eric Mertens 2022-11-07 21:29:13 -08:00
parent 719b822d5b
commit 025363cc0c
2 changed files with 77 additions and 0 deletions

View File

@ -38,6 +38,9 @@ target_link_libraries(day06 aocpp)
add_executable(day07 day07.cpp)
target_link_libraries(day07 aocpp intcode)
add_executable(day08 day08.cpp)
target_link_libraries(day08 aocpp)
add_executable(day09 day09.cpp)
target_link_libraries(day09 aocpp intcode)

74
day08.cpp Normal file
View File

@ -0,0 +1,74 @@
#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));
}