diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-11-16 13:23:34 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-11-16 13:23:34 +0100 |
| commit | 8c96639ec1f6757570510fc27f1c5fabece35eaf (patch) | |
| tree | 3a2b2146b6e8a353fa4c5edd105790320cce2b26 /2018/src/day03.cpp | |
| parent | bdd35a7bee7f23c912d0c453abc263805d8d8ccf (diff) | |
| download | advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.tar.gz advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.tar.bz2 advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.zip | |
aoc 2018, days 1-11
Diffstat (limited to '2018/src/day03.cpp')
| -rw-r--r-- | 2018/src/day03.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/2018/src/day03.cpp b/2018/src/day03.cpp new file mode 100644 index 0000000..bed3acb --- /dev/null +++ b/2018/src/day03.cpp | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | #include <filesystem> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <regex> | ||
| 6 | #include <set> | ||
| 7 | #include <tuple> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | using namespace std; | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | struct Claim { | ||
| 15 | int id; | ||
| 16 | int left; | ||
| 17 | int top; | ||
| 18 | int width; | ||
| 19 | int height; | ||
| 20 | }; | ||
| 21 | |||
| 22 | #if 0 | ||
| 23 | ostream& | ||
| 24 | operator<<(ostream& ostrm, const Claim& claim) | ||
| 25 | { | ||
| 26 | ostrm << "id: " << claim.id | ||
| 27 | << " (" << claim.left << ", " << claim.top << ": " << claim.width << "x" << claim.height; | ||
| 28 | return ostrm; | ||
| 29 | } | ||
| 30 | #endif | ||
| 31 | |||
| 32 | vector<Claim> | ||
| 33 | read_file(const filesystem::path& filename) | ||
| 34 | { | ||
| 35 | ifstream file{ filename }; | ||
| 36 | vector<Claim> claims; | ||
| 37 | |||
| 38 | regex pattern(R"#((\d+) @ (\d+),(\d+): (\d+)x(\d+))#"); | ||
| 39 | smatch match; | ||
| 40 | |||
| 41 | for ( string line; getline(file, line); ) { | ||
| 42 | if ( !regex_search(line, match, pattern) ) { | ||
| 43 | continue; | ||
| 44 | } | ||
| 45 | |||
| 46 | Claim claim{}; | ||
| 47 | claim.id = stoi(match[1]); | ||
| 48 | claim.left = stoi(match[2]); | ||
| 49 | claim.top = stoi(match[3]); | ||
| 50 | claim.width = stoi(match[4]); | ||
| 51 | claim.height = stoi(match[5]); | ||
| 52 | |||
| 53 | claims.emplace_back(claim); | ||
| 54 | } | ||
| 55 | |||
| 56 | return claims; | ||
| 57 | } | ||
| 58 | |||
| 59 | void | ||
| 60 | solve(const vector<Claim>& claims) | ||
| 61 | { | ||
| 62 | map<tuple<int, int>, vector<int>> fabric; | ||
| 63 | set<int> allIds; | ||
| 64 | |||
| 65 | for ( const auto& claim: claims ) { | ||
| 66 | for ( int x = claim.left; x < claim.left + claim.width; ++x ) { | ||
| 67 | for ( int y = claim.top; y < claim.top + claim.height; ++y ) { | ||
| 68 | fabric[{ x, y }].emplace_back(claim.id); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | allIds.emplace(claim.id); | ||
| 72 | } | ||
| 73 | |||
| 74 | int overlapCount = 0; | ||
| 75 | for ( const auto& [pos, ids]: fabric ) { | ||
| 76 | if ( ids.size() > 1 ) { | ||
| 77 | ++overlapCount; | ||
| 78 | for ( const auto& id: ids ) { | ||
| 79 | allIds.erase(id); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | cout << "Part 1: " << overlapCount << '\n'; | ||
| 85 | cout << "Part 2: " << *allIds.begin() << '\n'; | ||
| 86 | } | ||
| 87 | |||
| 88 | } // namespace | ||
| 89 | |||
| 90 | int | ||
| 91 | main() | ||
| 92 | { | ||
| 93 | auto claims = read_file("data/day03.txt"); | ||
| 94 | solve(claims); | ||
| 95 | } | ||
