Day5
This commit is contained in:
parent
78e28f6959
commit
a03473f567
|
@ -0,0 +1,91 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
bool instruction_mode = false;
|
||||
string line;
|
||||
unordered_map<int, string> stacks;
|
||||
|
||||
struct Instruction
|
||||
{
|
||||
int quantity;
|
||||
int from;
|
||||
int to;
|
||||
};
|
||||
|
||||
vector<Instruction> instructions;
|
||||
|
||||
int stack_max = 0;
|
||||
|
||||
while (getline(cin, line))
|
||||
{
|
||||
if (0 == line.size())
|
||||
{
|
||||
instruction_mode = true;
|
||||
|
||||
for (auto &it : stacks)
|
||||
{
|
||||
it.second = it.second.substr(0, it.second.size()-1);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (instruction_mode)
|
||||
{
|
||||
Instruction instruction;
|
||||
line = line.substr(5);
|
||||
string::size_type n = line.find(' ');
|
||||
instruction.quantity = stoi(line.substr(0,n));
|
||||
line = line.substr(n+6);
|
||||
n = line.find(' ');
|
||||
instruction.from = stoi(line.substr(0,n));
|
||||
line = line.substr(n+4);
|
||||
instruction.to = stoi(line);
|
||||
|
||||
instructions.push_back(instruction);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 1, s = 1; i < line.size(); i+=4, s++)
|
||||
{
|
||||
if (line[i] == ' ')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!stacks.contains(s))
|
||||
{
|
||||
stacks[s] = "";
|
||||
}
|
||||
|
||||
stacks[s] += line[i];
|
||||
|
||||
if (s > stack_max)
|
||||
{
|
||||
stack_max = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < instructions.size(); i++)
|
||||
{
|
||||
string taken = stacks[instructions[i].from].substr(0, instructions[i].quantity);
|
||||
reverse(taken.begin(), taken.end());
|
||||
stacks[instructions[i].from] = stacks[instructions[i].from].substr(instructions[i].quantity);
|
||||
stacks[instructions[i].to] = taken + stacks[instructions[i].to];
|
||||
}
|
||||
|
||||
for(int s = 1; s <= stack_max; s++)
|
||||
{
|
||||
cout << stacks[s][0];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
bool instruction_mode = false;
|
||||
string line;
|
||||
unordered_map<int, string> stacks;
|
||||
|
||||
struct Instruction
|
||||
{
|
||||
int quantity;
|
||||
int from;
|
||||
int to;
|
||||
};
|
||||
|
||||
vector<Instruction> instructions;
|
||||
|
||||
int stack_max = 0;
|
||||
|
||||
while (getline(cin, line))
|
||||
{
|
||||
if (0 == line.size())
|
||||
{
|
||||
instruction_mode = true;
|
||||
|
||||
for (auto &it : stacks)
|
||||
{
|
||||
it.second = it.second.substr(0, it.second.size()-1);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (instruction_mode)
|
||||
{
|
||||
Instruction instruction;
|
||||
line = line.substr(5);
|
||||
string::size_type n = line.find(' ');
|
||||
instruction.quantity = stoi(line.substr(0,n));
|
||||
line = line.substr(n+6);
|
||||
n = line.find(' ');
|
||||
instruction.from = stoi(line.substr(0,n));
|
||||
line = line.substr(n+4);
|
||||
instruction.to = stoi(line);
|
||||
|
||||
instructions.push_back(instruction);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 1, s = 1; i < line.size(); i+=4, s++)
|
||||
{
|
||||
if (line[i] == ' ')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!stacks.contains(s))
|
||||
{
|
||||
stacks[s] = "";
|
||||
}
|
||||
|
||||
stacks[s] += line[i];
|
||||
|
||||
if (s > stack_max)
|
||||
{
|
||||
stack_max = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < instructions.size(); i++)
|
||||
{
|
||||
string taken = stacks[instructions[i].from].substr(0, instructions[i].quantity);
|
||||
stacks[instructions[i].from] = stacks[instructions[i].from].substr(instructions[i].quantity);
|
||||
stacks[instructions[i].to] = taken + stacks[instructions[i].to];
|
||||
}
|
||||
|
||||
for(int s = 1; s <= stack_max; s++)
|
||||
{
|
||||
cout << stacks[s][0];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
Loading…
Reference in New Issue