aocpp/2019/08.cpp

65 lines
1.4 KiB
C++
Raw Normal View History

2022-11-07 21:29:13 -08:00
#include <algorithm>
#include <cstdint>
#include <array>
#include <iostream>
#include <iterator>
#include <vector>
2022-11-08 09:50:21 -08:00
#include <string_view>
2022-11-11 19:21:39 -08:00
#include <numeric>
2022-11-07 21:29:13 -08:00
#include <aocpp/Startup.hpp>
namespace {
2022-11-08 09:50:21 -08:00
auto Part1(std::string const& line) {
2022-11-07 21:29:13 -08:00
std::size_t part1 = 0;
2022-11-08 09:50:21 -08:00
auto seen = std::numeric_limits<std::size_t>::max();
for (auto it = line.begin();
std::distance(it, line.end()) >= 150;
std::advance(it, 150))
{
2022-11-11 08:27:18 -08:00
auto zeros = std::size_t(std::count(it, it + 150, '0'));
2022-11-07 21:29:13 -08:00
if (zeros < seen) {
seen = zeros;
part1
2022-11-08 09:50:21 -08:00
= std::count(it, it + 150, '1')
* std::count(it, it + 150, '2');
2022-11-07 21:29:13 -08:00
}
}
return part1;
}
2022-11-08 09:50:21 -08:00
auto Flatten(std::string const& line) {
std::string merged(150, ' ');
2022-11-07 21:29:13 -08:00
for (std::size_t i = 0; i < 150; i++) {
2022-11-08 09:50:21 -08:00
for (auto it = line.begin();
std::distance(it, line.end()) >= 150;
std::advance(it, 150))
{
if ('2' != it[i]) {
merged[i] = it[i];
2022-11-07 21:29:13 -08:00
break;
}
}
}
return merged;
}
2022-11-08 09:50:21 -08:00
auto Draw(std::string const& picture) {
2022-11-07 21:29:13 -08:00
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 {
2022-11-08 09:50:21 -08:00
std::string line;
std::getline(aocpp::Startup(argc, argv), line);
std::cout << "Part 1: " << Part1(line) << std::endl;
Draw(Flatten(line));
2022-11-07 21:29:13 -08:00
}