-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdominoes.cpp
More file actions
179 lines (165 loc) · 5.68 KB
/
dominoes.cpp
File metadata and controls
179 lines (165 loc) · 5.68 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Created by Mayank Parasar on 2019-12-09.
//
/*
Given a string with the initial condition of dominoes, where:
. represents that the domino is standing still
L represents that the domino is falling to the left side
R represents that the domino is falling to the right side
Figure out the final position of the dominoes. If there are dominoes that get pushed on both ends,
the force cancels out and that domino remains upright.
Example:
Input: ..R...L..R.
Output: ..RR.LL..RR
* */
// string::operator[]
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string initial = "..R...L..R.";
//string initial = "..RR.LL..R.";
void update_wavefront(vector<int>& waveR, vector<int>& waveL) {
// Reset both wavefronts
waveR.clear();
waveL.clear();
// initialize the wavefronts:
for(int ii=0; ii < initial.length(); ii++) { // start from idx-1
// if 'R' is on the right edge then no point
// of adding it to the wavefront
// if 'L' is on the left edge then no point of
// adding it to the wavefront
if (ii+1 < initial.length()) { // boundary check
if (initial[ii] == 'R' &&
initial[ii + 1] == '.') {
// put 'ii' in the wavefrontR
waveR.push_back(ii);
}
}
if (ii > 0) { // boundary check
if (initial[ii] == 'L' &&
initial[ii - 1] == '.') {
// put 'ii' in the wavefrontL
waveL.push_back(ii);
}
}
}
vector<int> delete_waveR_entries; // these contain the index
vector<int> delete_waveL_entries; // these contain the index
// delete the dublious entries from wavefronts
for(int idxR=0; idxR < waveR.size(); ++idxR) {
for(int idxL=0; idxL < waveL.size(); ++idxL) {
if((waveR[idxR] + 1) == (waveL[idxL] - 1)) {
// then log this entry to be deleted
delete_waveR_entries.push_back(idxR);
delete_waveL_entries.push_back(idxL);
}
}
}
// now delete the respective entries from respective wavefronts
for(auto i : delete_waveR_entries) {
waveR.erase(waveR.begin() + i);
}
for(auto i : delete_waveL_entries) {
waveL.erase(waveL.begin() + i);
}
}
int main() {
bool makeR = false; // flags
bool makeL = false; // flags
// string initial = "..R...L..R.";
// initial = "R....L....L";
// string final = initial;
cout << "initial state of the dominos is: " << initial << endl;
cout << "length of string is: " << initial.length() << endl;
vector<int> wavefrontR;
vector<int> wavefrontL;
int timestep = 0;
// initialize the wavefronts:
/* for(int ii=0; ii < initial.length(); ii++) {
// perform actions based on flags:
if(makeR == true) {
initial[ii] = 'R';
}
// set flags:
if (initial[ii] == 'L') {
makeL = true;
}
else if (initial[ii] == 'R') {
makeR = true;
}
cout << final[ii] << endl;
}*/
// initialize the wavefronts:
for(int ii=0; ii < initial.length(); ii++) { // start from idx-1
// if 'R' is on the right edge then no point
// of adding it to the wavefront
// if 'L' is on the left edge then no point of
// adding it to the wavefront
if (ii+1 < initial.length()) { // boundary check
if (initial[ii] == 'R' &&
initial[ii + 1] == '.') {
// put 'ii' in the wavefrontR
wavefrontR.push_back(ii);
}
}
if (ii > 0) { // boundary check
if (initial[ii] == 'L' &&
initial[ii - 1] == '.') {
// put 'ii' in the wavefrontL
wavefrontL.push_back(ii);
}
}
}
vector<int> delete_waveR_entries; // these contain the index
vector<int> delete_waveL_entries; // these contain the index
// delete the dublious entries from wavefronts
for(int idxR=0; idxR < wavefrontR.size(); ++idxR) {
for(int idxL=0; idxL < wavefrontL.size(); ++idxL) {
if((wavefrontR[idxR] + 1) == (wavefrontL[idxL] - 1)) {
// then log this entry to be deleted
delete_waveR_entries.push_back(idxR);
delete_waveL_entries.push_back(idxL);
}
}
}
// now delete the respective entries from respective wavefronts
for(auto i : delete_waveR_entries) {
wavefrontR.erase(wavefrontR.begin() + i);
}
for(auto i : delete_waveL_entries) {
wavefrontL.erase(wavefrontL.begin() + i);
}
cout << "wavefront-R: " << endl;
for (auto i : wavefrontR)
std::cout << i << ' ';
cout << endl << "wavefront-L: " << endl;
for (auto i : wavefrontL)
std::cout << i << ' ';
// each iteration mimics the timestep
// time loop
while(wavefrontL.size() > 0 ||
wavefrontR.size() > 0) {
// propagate the wave-R
for (auto i : wavefrontR) {
if(initial[i+1] == '.')
initial[i+1] = 'R';
}
// propagate the wave-L
for (auto i : wavefrontL) {
if(initial[i-1] == '.')
initial[i-1] = 'L';
}
// update wavefronts
update_wavefront(wavefrontR, wavefrontL);
cout << endl << "new string: " << initial << endl;
cout << "wavefront-R: " << endl;
for (auto i : wavefrontR)
std::cout << i << ' ';
cout << endl << "wavefront-L: " << endl;
for (auto i : wavefrontL)
std::cout << i << ' ';
}
cout << endl << "new string: " << initial << endl;
return 0;
}