diff options
Diffstat (limited to '2022')
| -rw-r--r-- | 2022/src/day05.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/2022/src/day05.cpp b/2022/src/day05.cpp new file mode 100644 index 0000000..e9e2097 --- /dev/null +++ b/2022/src/day05.cpp | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <stack> | ||
| 5 | #include <string> | ||
| 6 | #include <tuple> | ||
| 7 | #include <vector> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | using instructions_t = vector<tuple<size_t, size_t, size_t>>; | ||
| 11 | using stacks_t = map<size_t, stack<char>>; | ||
| 12 | |||
| 13 | void | ||
| 14 | read_file(string_view filename, instructions_t& instructions, stacks_t& stacks) | ||
| 15 | { | ||
| 16 | fstream input{ filename }; | ||
| 17 | vector<string> lines; | ||
| 18 | |||
| 19 | instructions.clear(); | ||
| 20 | |||
| 21 | for ( string line; getline(input, line); ) { | ||
| 22 | if ( line.length() == 0 ) { | ||
| 23 | continue; | ||
| 24 | } | ||
| 25 | else if ( line.starts_with("move") ) { | ||
| 26 | size_t a, b, c; | ||
| 27 | sscanf(line.c_str(), "move %zu from %zu to %zu", &a, &b, &c); | ||
| 28 | instructions.emplace_back(a, b, c); | ||
| 29 | } | ||
| 30 | else if ( line.find('[') != string::npos ) { | ||
| 31 | lines.emplace_back(line); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | reverse(begin(lines), end(lines)); | ||
| 36 | stacks.clear(); | ||
| 37 | |||
| 38 | for ( const auto& line: lines ) { | ||
| 39 | for ( size_t col = 0; col * 4 < line.length(); ++col ) { | ||
| 40 | auto chr = line[col * 4 + 1]; | ||
| 41 | if ( isalpha(chr) != 0 ) { | ||
| 42 | stacks[col + 1].push(chr); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | void | ||
| 49 | part1(const instructions_t& instructions, stacks_t stacks) | ||
| 50 | { | ||
| 51 | for ( const auto& [amount, from, to]: instructions ) { | ||
| 52 | for ( size_t i = 0; i != amount; ++i ) { | ||
| 53 | const auto value = stacks[from].top(); | ||
| 54 | stacks[from].pop(); | ||
| 55 | stacks[to].emplace(value); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | for ( size_t i = 0; i != stacks.size(); ++i ) { | ||
| 59 | cout << stacks[i + 1].top(); | ||
| 60 | } | ||
| 61 | cout << endl; | ||
| 62 | } | ||
| 63 | |||
| 64 | void | ||
| 65 | part2(const instructions_t& instructions, stacks_t stacks) | ||
| 66 | { | ||
| 67 | for ( const auto& [amount, from, to]: instructions ) { | ||
| 68 | vector<char> values; | ||
| 69 | |||
| 70 | for ( size_t i = 0; i != amount; ++i ) { | ||
| 71 | const auto value = stacks[from].top(); | ||
| 72 | stacks[from].pop(); | ||
| 73 | values.emplace_back(value); | ||
| 74 | } | ||
| 75 | |||
| 76 | reverse(begin(values), end(values)); | ||
| 77 | |||
| 78 | for ( const auto& value: values ) { | ||
| 79 | stacks[to].emplace(value); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | for ( size_t i = 0; i != stacks.size(); ++i ) { | ||
| 83 | cout << stacks[i + 1].top(); | ||
| 84 | } | ||
| 85 | cout << endl; | ||
| 86 | } | ||
| 87 | |||
| 88 | int | ||
| 89 | main() | ||
| 90 | { | ||
| 91 | instructions_t instructions; | ||
| 92 | stacks_t stacks; | ||
| 93 | |||
| 94 | read_file("data/day05.txt", instructions, stacks); | ||
| 95 | part1(instructions, stacks); | ||
| 96 | part2(instructions, stacks); | ||
| 97 | } | ||
