#include using namespace std; vector> parse_crate_stack(const vector& crate_text){ int number_of_piles = (crate_text[0].length() - 3) / 4 + 1; int start_pile_height = crate_text.size() - 1; // cout << number_of_piles << " " << start_pile_height << endl; vector> crate_stack(number_of_piles); for(int i = start_pile_height - 1; i >= 0; i--){ int pile = 0; for(unsigned int j = 1; j < crate_text[0].length(); j += 4){ if(crate_text[i][j] != ' '){ crate_stack[pile].push(crate_text[i][j]); } pile++; } } return crate_stack; } vector> parse_crane_commands(const vector& command_text){ vector> command_stack; for(auto line : command_text){ stringstream ss(line); int number_of_moves, source, target; string garbage; ss >> garbage >> number_of_moves >> garbage >> source >> garbage >> target; command_stack.push_back(make_tuple(number_of_moves, source, target)); } return command_stack; } int main(){ ifstream input_file("input.txt"); string line; vector crate_pile_text; vector crane_command_text; bool reading_crates = true; // Parse the text (fuckin hate parsing) while(getline(input_file, line)){ if(line.length() <= 1){ reading_crates = false; continue; } if(reading_crates){ crate_pile_text.push_back(line); } else{ crane_command_text.push_back(line); } } auto crate_stacks = parse_crate_stack(crate_pile_text); auto command_stack = parse_crane_commands(crane_command_text); // Execute the commands for(auto command : command_stack){ int number_of_moves, source, target; tie(number_of_moves, source, target) = command; while(number_of_moves > 0){ char crate = crate_stacks[source - 1].top(); crate_stacks[source - 1].pop(); crate_stacks[target - 1].push(crate); number_of_moves--; } } // Print answer cout << "Top crates: "; for(auto s : crate_stacks){ cout << s.top(); } cout << endl; // Testing parsing // for(auto s : crate_pile_text){ // cout << s << endl; // } // for(auto s : crane_command_text){ // cout << s << endl; // } // for(auto s : crate_stacks){ // while(!s.empty()){ // char c = s.top(); // cout << c << " "; // s.pop(); // } // cout << endl; // } // for(auto t : command_stack){ // int number_of_moves, source, target; // tie(number_of_moves, source, target) = t; // cout << number_of_moves << " " << source << " " << target << endl; // } return 0; }