diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
| commit | 756f22d58bb198b8f34589c112e1003614ccdcd6 (patch) | |
| tree | 4cdeba335f1ea67f7ead9e9f763ba1c3c206fc4d /2017/src/day16.cpp | |
| parent | fcbb722bd4c5ca2bd34a7cf9c0ba48f2f955da13 (diff) | |
| download | advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.gz advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.bz2 advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.zip | |
aoc 2017, days 1-20
Diffstat (limited to '2017/src/day16.cpp')
| -rw-r--r-- | 2017/src/day16.cpp | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/2017/src/day16.cpp b/2017/src/day16.cpp new file mode 100644 index 0000000..ea048bb --- /dev/null +++ b/2017/src/day16.cpp | |||
| @@ -0,0 +1,174 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <filesystem> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <map> | ||
| 6 | #include <sstream> | ||
| 7 | #include <string> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | using namespace std; | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | vector<string> | ||
| 15 | read_line(const filesystem::path& filename) | ||
| 16 | { | ||
| 17 | ifstream file{ filename }; | ||
| 18 | vector<string> result; | ||
| 19 | |||
| 20 | for ( string line; getline(file, line, ','); ) { | ||
| 21 | result.emplace_back(line); | ||
| 22 | } | ||
| 23 | return result; | ||
| 24 | } | ||
| 25 | |||
| 26 | vector<string> | ||
| 27 | split(const string& line, char sep = '/') | ||
| 28 | { | ||
| 29 | stringstream strm{ line }; | ||
| 30 | vector<string> result; | ||
| 31 | |||
| 32 | for ( string line; getline(strm, line, sep); ) { | ||
| 33 | result.emplace_back(line); | ||
| 34 | } | ||
| 35 | return result; | ||
| 36 | } | ||
| 37 | |||
| 38 | void | ||
| 39 | spin(string& line, size_t n) | ||
| 40 | { | ||
| 41 | ranges::reverse(line); | ||
| 42 | reverse(line.begin(), line.begin() + static_cast<string::difference_type>(n)); | ||
| 43 | reverse(line.begin() + static_cast<string::difference_type>(n), line.end()); | ||
| 44 | } | ||
| 45 | |||
| 46 | void | ||
| 47 | exchange(string& line, size_t a, size_t b) | ||
| 48 | { | ||
| 49 | swap(line.at(a), line.at(b)); | ||
| 50 | } | ||
| 51 | |||
| 52 | void | ||
| 53 | partner(string& line, char a, char b) | ||
| 54 | { | ||
| 55 | auto iter_a = ranges::find(line, a); | ||
| 56 | auto iter_b = ranges::find(line, b); | ||
| 57 | iter_swap(iter_a, iter_b); | ||
| 58 | } | ||
| 59 | |||
| 60 | void | ||
| 61 | dance(const vector<string>& instructions, string& line) | ||
| 62 | { | ||
| 63 | for ( const auto& instr: instructions ) { | ||
| 64 | auto values = split(instr.substr(1)); | ||
| 65 | |||
| 66 | switch ( instr.at(0) ) { | ||
| 67 | case 's': | ||
| 68 | spin(line, stoul(values.at(0))); | ||
| 69 | break; | ||
| 70 | case 'x': | ||
| 71 | exchange(line, stoul(values.at(0)), stoul(values.at(1))); | ||
| 72 | break; | ||
| 73 | case 'p': | ||
| 74 | partner(line, values.at(0).at(0), values.at(1).at(0)); | ||
| 75 | break; | ||
| 76 | default: | ||
| 77 | throw runtime_error("inknown command"); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | void | ||
| 83 | part1(const vector<string>& instructions, string line) | ||
| 84 | { | ||
| 85 | dance(instructions, line); | ||
| 86 | cout << "Part1: " << line << '\n'; | ||
| 87 | } | ||
| 88 | |||
| 89 | #if defined(WASTE_TIME) | ||
| 90 | void | ||
| 91 | part2_bf(const vector<string>& instructions, string line) | ||
| 92 | { | ||
| 93 | for ( long round = 0; round != 1'000'000'000; ++round ) { | ||
| 94 | dance(instructions, line); | ||
| 95 | } | ||
| 96 | |||
| 97 | cout << "Part2-BF: " << line << '\n'; | ||
| 98 | } | ||
| 99 | #endif | ||
| 100 | |||
| 101 | void | ||
| 102 | part2(const vector<string>& instructions, string line) | ||
| 103 | { | ||
| 104 | map<string, size_t> seen; | ||
| 105 | vector<string> history; | ||
| 106 | |||
| 107 | size_t cycle_start = 0; | ||
| 108 | size_t cycle_length = 0; | ||
| 109 | |||
| 110 | for ( size_t round = 0;; ++round ) { | ||
| 111 | if ( seen.contains(line) ) { | ||
| 112 | cycle_start = seen.at(line); | ||
| 113 | cycle_length = round - cycle_start; | ||
| 114 | break; | ||
| 115 | } | ||
| 116 | |||
| 117 | seen[line] = round; | ||
| 118 | history.emplace_back(line); | ||
| 119 | |||
| 120 | dance(instructions, line); | ||
| 121 | } | ||
| 122 | |||
| 123 | const size_t total_rounds = 1'000'000'000L; | ||
| 124 | const size_t remaining = (total_rounds - cycle_start) % cycle_length; | ||
| 125 | |||
| 126 | cout << "Part2: " << history.at(cycle_start + remaining) << '\n'; | ||
| 127 | } | ||
| 128 | |||
| 129 | void | ||
| 130 | part2_alt(const vector<string>& instructions, const string& line) | ||
| 131 | { | ||
| 132 | auto slow = line; | ||
| 133 | dance(instructions, slow); | ||
| 134 | |||
| 135 | auto fast = line; | ||
| 136 | dance(instructions, fast); | ||
| 137 | dance(instructions, fast); | ||
| 138 | |||
| 139 | // find cycle | ||
| 140 | while ( slow != fast ) { | ||
| 141 | dance(instructions, slow); | ||
| 142 | dance(instructions, fast); | ||
| 143 | dance(instructions, fast); | ||
| 144 | } | ||
| 145 | |||
| 146 | // find cycle length | ||
| 147 | size_t cycle_length = 1; | ||
| 148 | dance(instructions, slow); | ||
| 149 | while ( slow != fast ) { | ||
| 150 | dance(instructions, slow); | ||
| 151 | ++cycle_length; | ||
| 152 | } | ||
| 153 | |||
| 154 | // process remaining rounds | ||
| 155 | const size_t total_rounds = 1'000'000'000L; | ||
| 156 | for ( auto remaining = total_rounds % cycle_length; remaining-- > 0; ) { | ||
| 157 | dance(instructions, slow); | ||
| 158 | } | ||
| 159 | |||
| 160 | cout << "Part2-Alt: " << slow << '\n'; | ||
| 161 | } | ||
| 162 | |||
| 163 | } // namespace | ||
| 164 | |||
| 165 | int | ||
| 166 | main() | ||
| 167 | { | ||
| 168 | auto instructions = read_line("data/day16.txt"); | ||
| 169 | auto line = "abcdefghijklmnop"s; | ||
| 170 | |||
| 171 | part1(instructions, line); | ||
| 172 | part2(instructions, line); | ||
| 173 | part2_alt(instructions, line); | ||
| 174 | } | ||
