-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc8.cpp
116 lines (94 loc) · 1.55 KB
/
aoc8.cpp
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
115
116
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <numeric>
using namespace std;
#define ll long long int
unordered_map<string, string> umpl, umpr;
vector<string> u;
string inst;
ll part1() {
ll sum = 0;
int n;
cin >> n;
cin >> inst;
int m;
cin >> m;
for(int i = 0; i < m; ++i) {
string s;
cin >> s;
string r;
cin >> r;
cin >> r;
umpl[s] = r.substr(1, 3);
cin >> r;
umpr[s] = r.substr(0, 3);
}
ll step = 0;
string curr = "AAA";
while(curr != "ZZZ") {
if (inst[step++ % n] == 'L') {
curr = umpl[curr];
}
else {
curr = umpr[curr];
}
}
return step;
}
ll part2() {
ll sum = 0;
int n;
cin >> n;
cin >> inst;
int m;
cin >> m;
for(int i = 0; i < m; ++i) {
string s;
cin >> s;
if(s[2] == 'A') {
u.push_back(s);
}
string r;
cin >> r;
cin >> r;
umpl[s] = r.substr(1, 3);
cin >> r;
umpr[s] = r.substr(0, 3);
}
for(string curr : u) {
ll step = 0;
while(curr[2] != 'Z') {
if (inst[step++ % n] == 'L') {
curr = umpl[curr];
}
else {
curr = umpr[curr];
}
}
cout << step << '\n';
}
// ll step = 0;
// while(any_of(u.begin(), u.end(), [](const auto & a) {
// return a[2] != 'Z';
// })) {
// if (inst[step++ % n] == 'L') {
// for(string & s : u) s = umpl[s];
// }
// else {
// for(string & s : u) s = umpr[s];
// }
// if(step % 42690 == 1) cout << step << '\n';
// }
return 0;
}
int main() {
cout << part2();
return 0;
}