diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-13 19:15:25 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-13 19:15:25 +0100 |
| commit | 72253f5efb10c7892fdb09d52343764124cd717c (patch) | |
| tree | 338d56a10baa001f6cbe1a279acf3aa4f879a317 | |
| parent | c0617c13c588ff2748e2660e8fc6e19f6e93b2f9 (diff) | |
| download | advent-of-code-72253f5efb10c7892fdb09d52343764124cd717c.tar.gz advent-of-code-72253f5efb10c7892fdb09d52343764124cd717c.tar.bz2 advent-of-code-72253f5efb10c7892fdb09d52343764124cd717c.zip | |
Lösung für Tag 13
| -rw-r--r-- | makefile | 3 | ||||
| -rw-r--r-- | src/day13.cpp | 132 |
2 files changed, 134 insertions, 1 deletions
| @@ -11,7 +11,8 @@ all: bin/day01 \ | |||
| 11 | bin/day09 \ | 11 | bin/day09 \ |
| 12 | bin/day10 \ | 12 | bin/day10 \ |
| 13 | bin/day11 \ | 13 | bin/day11 \ |
| 14 | bin/day12 | 14 | bin/day12 \ |
| 15 | bin/day13 | ||
| 15 | 16 | ||
| 16 | bin: | 17 | bin: |
| 17 | mkdir $@ | 18 | mkdir $@ |
diff --git a/src/day13.cpp b/src/day13.cpp new file mode 100644 index 0000000..43fd39e --- /dev/null +++ b/src/day13.cpp | |||
| @@ -0,0 +1,132 @@ | |||
| 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>& input) | ||
| 38 | { | ||
| 39 | vector<string> result(input[0].size()); | ||
| 40 | |||
| 41 | for ( const auto& i: input ) { | ||
| 42 | for ( size_t j = 0; j < i.size(); j++ ) { | ||
| 43 | result[j] += i[j]; | ||
| 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 | auto cmp_result = (input.at(idx + cnt) == input.at(idx - 1 - cnt)); | ||
| 56 | if ( !cmp_result ) { | ||
| 57 | equal = false; | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | if ( equal ) { | ||
| 62 | return long(idx); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 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 | ||
| 85 | count_differences(string_view a, string_view b) // NOLINT | ||
| 86 | { | ||
| 87 | long errs = 0; | ||
| 88 | for (size_t idx = 0; idx != a.size(); ++idx) { | ||
| 89 | if (a[idx] != b[idx]) { | ||
| 90 | ++errs; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return errs; | ||
| 94 | } | ||
| 95 | |||
| 96 | long | ||
| 97 | find_mirror2(const vector<string>& input) | ||
| 98 | { | ||
| 99 | for ( size_t idx = 1; idx < input.size(); ++idx ) { | ||
| 100 | long errs = 0; | ||
| 101 | for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) { | ||
| 102 | errs += count_differences(input.at(idx + cnt), input.at(idx - 1 - cnt)); | ||
| 103 | } | ||
| 104 | if ( errs == 1 ) { | ||
| 105 | return long(idx); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | return 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | void | ||
| 112 | part2() | ||
| 113 | { | ||
| 114 | auto contents = read_file("data/day13.txt"); | ||
| 115 | auto parts = split(contents, "\n\n"); | ||
| 116 | |||
| 117 | long sum = 0; | ||
| 118 | for ( const auto& part: parts ) { | ||
| 119 | auto lines = split(part, "\n"); | ||
| 120 | |||
| 121 | sum += find_mirror2(transpose(lines)); | ||
| 122 | sum += find_mirror2(lines) * 100; | ||
| 123 | } | ||
| 124 | cout << sum << endl; | ||
| 125 | } | ||
| 126 | |||
| 127 | int | ||
| 128 | main() | ||
| 129 | { | ||
| 130 | // part1(); | ||
| 131 | part2(); | ||
| 132 | } | ||
