#include #include #include #include #include #include #include #include using namespace std; namespace { using Region = tuple>; tuple, vector> read_file(const filesystem::path& filename) { ifstream file{ filename }; vector lines; for ( string line; getline(file, line); ) { lines.emplace_back(line); } map areas; size_t idx = 0; for ( ;; idx += 5 ) { if ( lines.at(idx).at(1) != ':' ) { break; } const long num = lines.at(idx).at(0) - '0'; long count = 0; for ( size_t j = idx + 1; j != idx + 4; ++j ) { count += ranges::count(lines.at(j), '#'); } areas[num] = count; } vector regions; for ( ; idx != lines.size(); ++idx ) { static const regex rgx(R"(^(\d+)x(\d+):\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$)"); smatch match; if ( !regex_match(lines.at(idx), match, rgx) ) { continue; } regions.emplace_back( stol(match[1]), // width stol(match[2]), // height array{ stol(match[3]), stol(match[4]), stol(match[5]), stol(match[6]), stol(match[7]), stol(match[8]) } // counts ); } return { areas, regions }; } void part1(const map& areas, const vector& regions) { const auto result = ranges::count_if(regions, [&](const auto& region) { const auto& [width, height, counts] = region; long required = 0; for ( size_t i = 0; i != counts.size(); ++i ) { required += counts.at(i) * areas.at(static_cast(i)); } return required < width * height; }); cout << "Part 1: " << result << '\n'; } } // namespace int main() { const auto [areas, regions] = read_file("data/day12.txt"); part1(areas, regions); }