-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2205.cc
114 lines (100 loc) · 1.69 KB
/
aoc2205.cc
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "iostream"
#include "vector"
#include "sstream"
#include "deque"
using namespace std;
int main()
{
vector<string> a;
string s;
int i;
char c;
string res1, res2;
// int res, res2;
vector<deque<char>> vd1, vd2;
// 1st half of input - crates
while (getline(cin, s))
{
if (s == "")
break ;
a.push_back(s);
}
stringstream ss(a[a.size() - 1]);
int numc;
while (!ss.eof() && ss >> numc)
;;
i = -1;
while (++i < numc)
{
vd1.push_back(deque<char>());
vd2.push_back(deque<char>());
}
a.pop_back();
for (string line : a)
{
// 1st crate
i = 0;
if (line[1] <= 'Z' && line[1] >= 'A')
{
vd1[i].push_front(line[1]);
vd2[i].push_front(line[1]);
}
// subsequent crates
while (++i < numc)
{
c = line[1 + 4 * i];
if (!(c <= 'Z' && c >= 'A'))
continue ;
vd1[i].push_front(c);
vd2[i].push_front(c);
}
}
// 2nd half of input - operations
while (getline(cin, s))
{
int M, F, T;
int move, from, to;
sscanf(s.c_str(), "move %d from %d to %d", & M, & F, & T);
// part 1
from = F - 1;
to = T - 1;
move = M;
i = -1;
while (++i < move)
{
if (vd1[from].empty())
continue ;
c = vd1[from].back();
vd1[from].pop_back();
vd1[to].push_back(c);
}
// part 2
string E;
from = F - 1;
to = T - 1;
move = M;
i = -1;
while (++i < move)
{
if (vd2[from].empty())
continue ;
c = vd2[from].back();
E += c;
vd2[from].pop_back();
}
while (E != "")
{
c = E[E.length() - 1];
E.pop_back();
vd2[to].push_back(c);
}
}
i = -1;
while (++i < numc)
{
res1 += vd1[i].back();
res2 += vd2[i].back();
}
cout << "Star 1: " << res1 << endl;
cout << "Star 2: " << res2 << endl;
}