diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-12-12 13:42:16 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-12-12 13:42:16 +0100 |
| commit | 0464917d4de2143d3c464d54dc1c5590a541cbea (patch) | |
| tree | a4e5b68603f7aa94e45930f19793cccbf8120940 | |
| parent | 38a11596571d5281a9ba2c197d0e9cbebeb4039d (diff) | |
| download | advent-of-code-master.tar.gz advent-of-code-master.tar.bz2 advent-of-code-master.zip | |
| -rw-r--r-- | 2025/src/day12.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/2025/src/day12.cpp b/2025/src/day12.cpp new file mode 100644 index 0000000..d6f9277 --- /dev/null +++ b/2025/src/day12.cpp | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | #include <array> | ||
| 2 | #include <filesystem> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <map> | ||
| 6 | #include <regex> | ||
| 7 | #include <string> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | using namespace std; | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | using Region = tuple<long, long, array<long, 6>>; | ||
| 15 | |||
| 16 | tuple<map<long, long>, vector<Region>> | ||
| 17 | read_file(const filesystem::path& filename) | ||
| 18 | { | ||
| 19 | ifstream file{ filename }; | ||
| 20 | vector<string> lines; | ||
| 21 | |||
| 22 | for ( string line; getline(file, line); ) { | ||
| 23 | lines.emplace_back(line); | ||
| 24 | } | ||
| 25 | |||
| 26 | map<long, long> areas; | ||
| 27 | |||
| 28 | size_t idx = 0; | ||
| 29 | for ( ;; idx += 5 ) { | ||
| 30 | if ( lines.at(idx).at(1) != ':' ) { | ||
| 31 | break; | ||
| 32 | } | ||
| 33 | |||
| 34 | const long num = lines.at(idx).at(0) - '0'; | ||
| 35 | |||
| 36 | long count = 0; | ||
| 37 | for ( size_t j = idx + 1; j != idx + 4; ++j ) { | ||
| 38 | count += ranges::count(lines.at(j), '#'); | ||
| 39 | } | ||
| 40 | |||
| 41 | areas[num] = count; | ||
| 42 | } | ||
| 43 | |||
| 44 | vector<Region> regions; | ||
| 45 | for ( ; idx != lines.size(); ++idx ) { | ||
| 46 | static const regex rgx(R"(^(\d+)x(\d+):\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$)"); | ||
| 47 | |||
| 48 | smatch match; | ||
| 49 | if ( !regex_match(lines.at(idx), match, rgx) ) { | ||
| 50 | continue; | ||
| 51 | } | ||
| 52 | |||
| 53 | regions.emplace_back( | ||
| 54 | stol(match[1]), // width | ||
| 55 | stol(match[2]), // height | ||
| 56 | array<long, 6>{ | ||
| 57 | stol(match[3]), | ||
| 58 | stol(match[4]), | ||
| 59 | stol(match[5]), | ||
| 60 | stol(match[6]), | ||
| 61 | stol(match[7]), | ||
| 62 | stol(match[8]) } // counts | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | return { areas, regions }; | ||
| 67 | } | ||
| 68 | |||
| 69 | void | ||
| 70 | part1(const map<long, long>& areas, const vector<Region>& regions) | ||
| 71 | { | ||
| 72 | const auto result = ranges::count_if(regions, [&](const auto& region) { | ||
| 73 | const auto& [width, height, counts] = region; | ||
| 74 | |||
| 75 | long required = 0; | ||
| 76 | for ( size_t i = 0; i != counts.size(); ++i ) { | ||
| 77 | required += counts.at(i) * areas.at(static_cast<long>(i)); | ||
| 78 | } | ||
| 79 | return required < width * height; | ||
| 80 | }); | ||
| 81 | |||
| 82 | cout << "Part 1: " << result << '\n'; | ||
| 83 | } | ||
| 84 | |||
| 85 | } // namespace | ||
| 86 | |||
| 87 | int | ||
| 88 | main() | ||
| 89 | { | ||
| 90 | const auto [areas, regions] = read_file("data/day12.txt"); | ||
| 91 | part1(areas, regions); | ||
| 92 | } | ||
