-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.cpp
More file actions
99 lines (82 loc) · 2.94 KB
/
Copy pathday5.cpp
File metadata and controls
99 lines (82 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
*/
#include <iostream>
#include <fstream>
#include <deque>
#include <stack>
#include <vector>
#include <string>
const std::string inputFilePath = "inputs/day5.txt";
const int CRATE_CHAR_LENGTH = 3;
void moveCrateBetweenStacks(std::deque<char> &from, std::deque<char> &to){
char crate = from.front();
from.pop_front();
to.push_front(crate);
}
void moveMultipleCratesBetweenStacks(std::deque<char> &from, std::deque<char> &to, int crateAmmount){
std::stack<char> temporaryStack;
for(int i=0; i< crateAmmount; i++){
char crate = from.front();
from.pop_front();
temporaryStack.push(crate);
}
for(int i=0; i< crateAmmount; i++){
const char crate = temporaryStack.top();
temporaryStack.pop();
to.push_front(crate);
}
}
int main(){
std::ifstream ifs(inputFilePath, std::ifstream::in);
std::string line;
// Create stacks
std::vector<std::deque<char>> stacks;
std::getline(ifs, line);
const int numberOfStacks = ((line.length() + 1) / (CRATE_CHAR_LENGTH + 1)); // +1 to account for ' ' between crates
for(int i = 0; i <numberOfStacks; i++){
std::deque<char> s; // We use deques instead of stacks so we can construct them while reading the file top down
stacks.push_back(s);
}
// Parse diagram
ifs.seekg (0, ifs.beg); // Reset to beggining
while(std::getline(ifs, line) && line != ""){
for(int i=0; i < line.length(); i++){
if (line.at(i) == '['){
stacks[i/4].push_back(line.at(i+1)); // +1 to go from '[' to char within brackets '[CHAR]'
}
}
}
std::vector<std::deque<char>> copyOfStacks = stacks; // To have separate copy for Ex2
// Parse instructions
while(std::getline(ifs, line)){
const int crateAmmount = std::stoi(line.substr(line.find("move") + 5, line.find("from") - line.find("move") - 6));
const int stackFrom = std::stoi(line.substr(line.find("from") + 5, line.find("to") - line.find("from") - 6));
const int stackTo = std::stoi(line.substr(line.find("to") + 3));
// Ex1:
for(int i=0; i < crateAmmount;i++){
moveCrateBetweenStacks(stacks[stackFrom-1], stacks[stackTo - 1]);
}
// Ex2:
moveMultipleCratesBetweenStacks(copyOfStacks[stackFrom-1], copyOfStacks[stackTo - 1], crateAmmount);
}
// Construct output strings from poping each stack
std::string topCrates = "";
for(int i=0; i < stacks.size();i++){
topCrates.push_back(stacks[i].front());
}
std::string topCratesEx2 = "";
for(int i=0; i < copyOfStacks.size();i++){
topCratesEx2.push_back(copyOfStacks[i].front());
}
std::cout << "--Ex1 Output: " << topCrates << std::endl;
std::cout << "--Ex2 Output: " << topCratesEx2 << std::endl;
return 0;
}