diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-01-03 23:35:54 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-01-03 23:35:54 +0100 |
| commit | 56e890cec0a28c0a485212ccebfaf774235a79a2 (patch) | |
| tree | d9c6241a1aa247e06ab5ba2f6f11967b77d458ec /src/day13.cpp | |
| parent | e9a1cb0441d137d7a26cb303b7e72d3424b7392d (diff) | |
| download | advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.gz advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.bz2 advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.zip | |
prepare for more puzzles ... :)
Diffstat (limited to 'src/day13.cpp')
| -rw-r--r-- | src/day13.cpp | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/src/day13.cpp b/src/day13.cpp deleted file mode 100644 index 549457b..0000000 --- a/src/day13.cpp +++ /dev/null | |||
| @@ -1,115 +0,0 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <iterator> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | string | ||
| 11 | read_file(string_view filename) | ||
| 12 | { | ||
| 13 | fstream input{ filename }; | ||
| 14 | return { istreambuf_iterator<char>{ input }, istreambuf_iterator<char>{} }; | ||
| 15 | } | ||
| 16 | |||
| 17 | vector<string> | ||
| 18 | split(string_view line, string_view delimiter) | ||
| 19 | { | ||
| 20 | size_t pos_start = 0; | ||
| 21 | size_t pos_end = 0; | ||
| 22 | |||
| 23 | vector<string> res; | ||
| 24 | |||
| 25 | while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) { | ||
| 26 | auto token = line.substr(pos_start, pos_end - pos_start); | ||
| 27 | pos_start = pos_end + delimiter.length(); | ||
| 28 | |||
| 29 | res.emplace_back(token); | ||
| 30 | } | ||
| 31 | |||
| 32 | res.emplace_back(line.substr(pos_start)); | ||
| 33 | return res; | ||
| 34 | } | ||
| 35 | |||
| 36 | vector<string> | ||
| 37 | transpose(const vector<string>& lines) | ||
| 38 | { | ||
| 39 | vector<string> result(lines[0].size()); | ||
| 40 | |||
| 41 | for ( const auto& line: lines ) { | ||
| 42 | for ( size_t i = 0; i < line.size(); ++i ) { | ||
| 43 | result[i] += line[i]; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | return result; | ||
| 47 | } | ||
| 48 | |||
| 49 | long | ||
| 50 | find_mirror(const vector<string>& input) | ||
| 51 | { | ||
| 52 | for ( size_t idx = 1; idx < input.size(); ++idx ) { | ||
| 53 | bool equal = true; | ||
| 54 | for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) { | ||
| 55 | if ( !(input[idx + cnt] == input[idx - 1 - cnt]) ) { | ||
| 56 | equal = false; | ||
| 57 | break; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | if ( equal ) { | ||
| 61 | return long(idx); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | long | ||
| 68 | count_differences(string_view str1, string_view str2) | ||
| 69 | { | ||
| 70 | long diffs = 0; | ||
| 71 | for ( size_t idx = 0; idx != str1.size(); ++idx ) { | ||
| 72 | diffs += long(str1[idx] != str2[idx]); | ||
| 73 | } | ||
| 74 | return diffs; | ||
| 75 | } | ||
| 76 | |||
| 77 | long | ||
| 78 | find_mirror_part2(const vector<string>& input) | ||
| 79 | { | ||
| 80 | for ( size_t idx = 1; idx < input.size(); ++idx ) { | ||
| 81 | long errs = 0; | ||
| 82 | for ( size_t i = 0; i != min(idx, input.size() - idx); ++i ) { | ||
| 83 | errs += count_differences(input[idx + i], input[idx - 1 - i]); | ||
| 84 | } | ||
| 85 | if ( errs == 1 ) { | ||
| 86 | return long(idx); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | void | ||
| 93 | solve(const function<long(const vector<string>&)>& find_mirror) | ||
| 94 | { | ||
| 95 | static const long multiplier = 100; | ||
| 96 | |||
| 97 | auto contents = read_file("data/day13.txt"); | ||
| 98 | auto parts = split(contents, "\n\n"); | ||
| 99 | |||
| 100 | long sum = 0; | ||
| 101 | for ( const auto& part: parts ) { | ||
| 102 | auto lines = split(part, "\n"); | ||
| 103 | |||
| 104 | sum += find_mirror(transpose(lines)); | ||
| 105 | sum += find_mirror(lines) * multiplier; | ||
| 106 | } | ||
| 107 | cout << sum << endl; | ||
| 108 | } | ||
| 109 | |||
| 110 | int | ||
| 111 | main() | ||
| 112 | { | ||
| 113 | solve(find_mirror); | ||
| 114 | solve(find_mirror_part2); | ||
| 115 | } | ||
