#include #include #include #include #include #include #include #include using namespace std; namespace { struct Claim { int id; int left; int top; int width; int height; }; #if 0 ostream& operator<<(ostream& ostrm, const Claim& claim) { ostrm << "id: " << claim.id << " (" << claim.left << ", " << claim.top << ": " << claim.width << "x" << claim.height; return ostrm; } #endif vector read_file(const filesystem::path& filename) { ifstream file{ filename }; vector claims; regex pattern(R"#((\d+) @ (\d+),(\d+): (\d+)x(\d+))#"); smatch match; for ( string line; getline(file, line); ) { if ( !regex_search(line, match, pattern) ) { continue; } Claim claim{}; claim.id = stoi(match[1]); claim.left = stoi(match[2]); claim.top = stoi(match[3]); claim.width = stoi(match[4]); claim.height = stoi(match[5]); claims.emplace_back(claim); } return claims; } void solve(const vector& claims) { map, vector> fabric; set allIds; for ( const auto& claim: claims ) { for ( int x = claim.left; x < claim.left + claim.width; ++x ) { for ( int y = claim.top; y < claim.top + claim.height; ++y ) { fabric[{ x, y }].emplace_back(claim.id); } } allIds.emplace(claim.id); } int overlapCount = 0; for ( const auto& [pos, ids]: fabric ) { if ( ids.size() > 1 ) { ++overlapCount; for ( const auto& id: ids ) { allIds.erase(id); } } } cout << "Part 1: " << overlapCount << '\n'; cout << "Part 2: " << *allIds.begin() << '\n'; } } // namespace int main() { auto claims = read_file("data/day03.txt"); solve(claims); }