aocpp/2019/08.cpp

66 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;
}
2023-01-31 09:15:15 -08:00
auto Draw(std::ostream & out, 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++) {
2023-01-31 09:15:15 -08:00
out << ('1' == picture[y*25+x] ? "" : "");
2022-11-07 21:29:13 -08:00
}
2023-01-31 09:15:15 -08:00
out << std::endl;
2022-11-07 21:29:13 -08:00
}
}
} // namespace
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-31 08:58:42 -08:00
{
2022-11-08 09:50:21 -08:00
std::string line;
2023-01-31 08:58:42 -08:00
std::getline(in, line);
2023-01-31 09:15:15 -08:00
out << "Part 1: " << Part1(line) << std::endl;
Draw(out, Flatten(line));
2022-11-07 21:29:13 -08:00
}