From 025363cc0c3907222b6e3451bd2330df0e114e8a Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Mon, 7 Nov 2022 21:29:13 -0800 Subject: [PATCH] 8 --- CMakeLists.txt | 3 ++ day08.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 day08.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 56faaf3..d9aa738 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/day08.cpp b/day08.cpp new file mode 100644 index 0000000..cd71a24 --- /dev/null +++ b/day08.cpp @@ -0,0 +1,74 @@ +#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)); +}