diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-12 22:20:29 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-12 22:20:29 +0100 |
| commit | c0617c13c588ff2748e2660e8fc6e19f6e93b2f9 (patch) | |
| tree | 993e76bbe72faad99de93664cb1b1f9435abc099 /src | |
| parent | 85e491d76f1eabe8117c25ab43da3ada73b34442 (diff) | |
| download | advent-of-code-c0617c13c588ff2748e2660e8fc6e19f6e93b2f9.tar.gz advent-of-code-c0617c13c588ff2748e2660e8fc6e19f6e93b2f9.tar.bz2 advent-of-code-c0617c13c588ff2748e2660e8fc6e19f6e93b2f9.zip | |
Lösung für Tag 12, Teil 1
Diffstat (limited to 'src')
| -rw-r--r-- | src/day12.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/day12.cpp b/src/day12.cpp new file mode 100644 index 0000000..31a2739 --- /dev/null +++ b/src/day12.cpp | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <sstream> | ||
| 4 | #include <string> | ||
| 5 | #include <vector> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | vector<string> | ||
| 9 | read_file(string_view filename) | ||
| 10 | { | ||
| 11 | fstream input{ filename }; | ||
| 12 | vector<string> data; | ||
| 13 | |||
| 14 | for ( string line; getline(input, line); ) { | ||
| 15 | data.emplace_back(line); | ||
| 16 | } | ||
| 17 | |||
| 18 | return data; | ||
| 19 | } | ||
| 20 | |||
| 21 | vector<string> | ||
| 22 | split(const string& line, char sep) | ||
| 23 | { | ||
| 24 | vector<string> parts{}; | ||
| 25 | stringstream input{ line }; | ||
| 26 | |||
| 27 | for ( string part; getline(input, part, sep); ) { | ||
| 28 | parts.emplace_back(part); | ||
| 29 | } | ||
| 30 | |||
| 31 | return parts; | ||
| 32 | } | ||
| 33 | |||
| 34 | template<typename T = long> | ||
| 35 | vector<T> | ||
| 36 | read_ints(const string& line) | ||
| 37 | { | ||
| 38 | vector<T> parts{}; | ||
| 39 | stringstream input{ line }; | ||
| 40 | |||
| 41 | for ( string part; getline(input, part, ','); ) { | ||
| 42 | parts.emplace_back(stol(part)); | ||
| 43 | } | ||
| 44 | |||
| 45 | return parts; | ||
| 46 | } | ||
| 47 | |||
| 48 | vector<long> | ||
| 49 | count_groups(string_view str) | ||
| 50 | { | ||
| 51 | vector<long> groups; | ||
| 52 | |||
| 53 | size_t idx = 0U; | ||
| 54 | while ( idx != str.length() ) { | ||
| 55 | while ( idx != str.length() && str[idx] == '.' ) { | ||
| 56 | ++idx; | ||
| 57 | } | ||
| 58 | if ( idx != str.length() && str[idx] == '#' ) { | ||
| 59 | auto num = 0L; | ||
| 60 | while ( idx != str.length() && str[idx] == '#' ) { | ||
| 61 | ++num; | ||
| 62 | ++idx; | ||
| 63 | } | ||
| 64 | groups.emplace_back(num); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | return groups; | ||
| 69 | } | ||
| 70 | |||
| 71 | long | ||
| 72 | brute_force(string_view puzzle, const vector<long>& nums) | ||
| 73 | { | ||
| 74 | long counts = count_if(puzzle.begin(), puzzle.end(), [](char chr) { return chr == '?'; }); | ||
| 75 | |||
| 76 | string test_pattern; | ||
| 77 | |||
| 78 | auto arrangements = 0L; | ||
| 79 | for ( size_t counter = 0; counter != (1U << size_t(counts)); ++counter ) { | ||
| 80 | auto bit_pattern = counter; | ||
| 81 | |||
| 82 | for ( char chr: puzzle ) { | ||
| 83 | if ( chr == '?' ) { | ||
| 84 | if ( (bit_pattern & 1U) != 0U ) { | ||
| 85 | test_pattern += '.'; | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | test_pattern += '#'; | ||
| 89 | } | ||
| 90 | bit_pattern >>= 1U; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | test_pattern += chr; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | if ( nums == count_groups(test_pattern) ) { | ||
| 97 | arrangements++; | ||
| 98 | } | ||
| 99 | |||
| 100 | test_pattern.clear(); | ||
| 101 | } | ||
| 102 | |||
| 103 | return arrangements; | ||
| 104 | } | ||
| 105 | |||
| 106 | void | ||
| 107 | part1() | ||
| 108 | { | ||
| 109 | const auto input = read_file("data/day12.txt"); | ||
| 110 | |||
| 111 | auto sum = 0L; | ||
| 112 | for ( const auto& line: input ) { | ||
| 113 | auto parts = split(line, ' '); | ||
| 114 | auto puzzle = parts[0]; | ||
| 115 | auto nums = read_ints(parts[1]); | ||
| 116 | |||
| 117 | sum += brute_force(puzzle, nums); | ||
| 118 | } | ||
| 119 | cout << sum << endl; | ||
| 120 | } | ||
| 121 | |||
| 122 | int | ||
| 123 | main() | ||
| 124 | { | ||
| 125 | part1(); | ||
| 126 | } | ||
