diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-13 21:16:34 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-13 21:16:34 +0100 |
| commit | 515cb99d7bc5245fd9ffce413210daa5a1e5423d (patch) | |
| tree | f0306222f6abdbdf99e2990e50cab0ed4a914aee /src | |
| parent | 72253f5efb10c7892fdb09d52343764124cd717c (diff) | |
| download | advent-of-code-515cb99d7bc5245fd9ffce413210daa5a1e5423d.tar.gz advent-of-code-515cb99d7bc5245fd9ffce413210daa5a1e5423d.tar.bz2 advent-of-code-515cb99d7bc5245fd9ffce413210daa5a1e5423d.zip | |
Vereinfache den Code...
Diffstat (limited to 'src')
| -rw-r--r-- | src/day13.cpp | 59 |
1 files changed, 21 insertions, 38 deletions
diff --git a/src/day13.cpp b/src/day13.cpp index 43fd39e..549457b 100644 --- a/src/day13.cpp +++ b/src/day13.cpp | |||
| @@ -34,13 +34,13 @@ split(string_view line, string_view delimiter) | |||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | vector<string> | 36 | vector<string> |
| 37 | transpose(const vector<string>& input) | 37 | transpose(const vector<string>& lines) |
| 38 | { | 38 | { |
| 39 | vector<string> result(input[0].size()); | 39 | vector<string> result(lines[0].size()); |
| 40 | 40 | ||
| 41 | for ( const auto& i: input ) { | 41 | for ( const auto& line: lines ) { |
| 42 | for ( size_t j = 0; j < i.size(); j++ ) { | 42 | for ( size_t i = 0; i < line.size(); ++i ) { |
| 43 | result[j] += i[j]; | 43 | result[i] += line[i]; |
| 44 | } | 44 | } |
| 45 | } | 45 | } |
| 46 | return result; | 46 | return result; |
| @@ -52,8 +52,7 @@ find_mirror(const vector<string>& input) | |||
| 52 | for ( size_t idx = 1; idx < input.size(); ++idx ) { | 52 | for ( size_t idx = 1; idx < input.size(); ++idx ) { |
| 53 | bool equal = true; | 53 | bool equal = true; |
| 54 | for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) { | 54 | for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) { |
| 55 | auto cmp_result = (input.at(idx + cnt) == input.at(idx - 1 - cnt)); | 55 | if ( !(input[idx + cnt] == input[idx - 1 - cnt]) ) { |
| 56 | if ( !cmp_result ) { | ||
| 57 | equal = false; | 56 | equal = false; |
| 58 | break; | 57 | break; |
| 59 | } | 58 | } |
| @@ -65,41 +64,23 @@ find_mirror(const vector<string>& input) | |||
| 65 | return 0; | 64 | return 0; |
| 66 | } | 65 | } |
| 67 | 66 | ||
| 68 | void | ||
| 69 | part1() | ||
| 70 | { | ||
| 71 | auto contents = read_file("data/day13.txt"); | ||
| 72 | auto parts = split(contents, "\n\n"); | ||
| 73 | |||
| 74 | long sum = 0; | ||
| 75 | for ( const auto& part: parts ) { | ||
| 76 | auto lines = split(part, "\n"); | ||
| 77 | |||
| 78 | sum += find_mirror(transpose(lines)); | ||
| 79 | sum += find_mirror(lines) * 100; | ||
| 80 | } | ||
| 81 | cout << sum << endl; | ||
| 82 | } | ||
| 83 | |||
| 84 | long | 67 | long |
| 85 | count_differences(string_view a, string_view b) // NOLINT | 68 | count_differences(string_view str1, string_view str2) |
| 86 | { | 69 | { |
| 87 | long errs = 0; | 70 | long diffs = 0; |
| 88 | for (size_t idx = 0; idx != a.size(); ++idx) { | 71 | for ( size_t idx = 0; idx != str1.size(); ++idx ) { |
| 89 | if (a[idx] != b[idx]) { | 72 | diffs += long(str1[idx] != str2[idx]); |
| 90 | ++errs; | ||
| 91 | } | ||
| 92 | } | 73 | } |
| 93 | return errs; | 74 | return diffs; |
| 94 | } | 75 | } |
| 95 | 76 | ||
| 96 | long | 77 | long |
| 97 | find_mirror2(const vector<string>& input) | 78 | find_mirror_part2(const vector<string>& input) |
| 98 | { | 79 | { |
| 99 | for ( size_t idx = 1; idx < input.size(); ++idx ) { | 80 | for ( size_t idx = 1; idx < input.size(); ++idx ) { |
| 100 | long errs = 0; | 81 | long errs = 0; |
| 101 | for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) { | 82 | for ( size_t i = 0; i != min(idx, input.size() - idx); ++i ) { |
| 102 | errs += count_differences(input.at(idx + cnt), input.at(idx - 1 - cnt)); | 83 | errs += count_differences(input[idx + i], input[idx - 1 - i]); |
| 103 | } | 84 | } |
| 104 | if ( errs == 1 ) { | 85 | if ( errs == 1 ) { |
| 105 | return long(idx); | 86 | return long(idx); |
| @@ -109,8 +90,10 @@ find_mirror2(const vector<string>& input) | |||
| 109 | } | 90 | } |
| 110 | 91 | ||
| 111 | void | 92 | void |
| 112 | part2() | 93 | solve(const function<long(const vector<string>&)>& find_mirror) |
| 113 | { | 94 | { |
| 95 | static const long multiplier = 100; | ||
| 96 | |||
| 114 | auto contents = read_file("data/day13.txt"); | 97 | auto contents = read_file("data/day13.txt"); |
| 115 | auto parts = split(contents, "\n\n"); | 98 | auto parts = split(contents, "\n\n"); |
| 116 | 99 | ||
| @@ -118,8 +101,8 @@ part2() | |||
| 118 | for ( const auto& part: parts ) { | 101 | for ( const auto& part: parts ) { |
| 119 | auto lines = split(part, "\n"); | 102 | auto lines = split(part, "\n"); |
| 120 | 103 | ||
| 121 | sum += find_mirror2(transpose(lines)); | 104 | sum += find_mirror(transpose(lines)); |
| 122 | sum += find_mirror2(lines) * 100; | 105 | sum += find_mirror(lines) * multiplier; |
| 123 | } | 106 | } |
| 124 | cout << sum << endl; | 107 | cout << sum << endl; |
| 125 | } | 108 | } |
| @@ -127,6 +110,6 @@ part2() | |||
| 127 | int | 110 | int |
| 128 | main() | 111 | main() |
| 129 | { | 112 | { |
| 130 | // part1(); | 113 | solve(find_mirror); |
| 131 | part2(); | 114 | solve(find_mirror_part2); |
| 132 | } | 115 | } |
