-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainy.cpp
116 lines (93 loc) · 2.03 KB
/
brainy.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 <fstream>
#include <vector>
#include <algorithm>
int findClosing( int orders[], int time){
int health = 1;
int i = time;
while (health != 0){
i++;
if (orders[i] == 91){
health++;
} else if(orders[i] == 93){
health--;
}
}
return i;
}
int findOpening(int orders[], int time){
int health = 1;
int i = time;
while (health != 0){
i--;
if (orders[i] == 93){
health++;
} else if(orders[i] == 91){
health--;
}
}
return i;
}
int main(int argc, char **argv) {
if (argc == 1){
std::cout << "Need File!";
return 1;
} else {
std::ifstream myfile;
myfile.open (argv[1]);
std::vector<char> arr = std::vector<char>();
std::cout << "Reading: " << std::endl;
while (!myfile.eof()) {
char test;
myfile >> test ;
arr.push_back(test);;
}
arr.pop_back();
std::cout << std::endl << "Finished reading!" << std::endl;
int i = 1;
for (char x : arr){
if (x == 62){
i ++;
} else if( x == 60){
i--;
}
}
std::vector<int>orders(arr.size());
std::copy(arr.begin(), arr.end() , orders.begin());
std::cout << "memory size : " << i << std::endl;
std::vector<int>mem(i * 266);
i = 0;
for ( int o = 0; o < arr.size(); o++ ){
if (orders[o] == 62){
i++;
} else if(orders[o] == 60) {
if (i == 0){
return 1;
}else {
i--;
}
} else if (orders[o] == 43){
mem[i]++;
} else if (orders[o] == 45){
mem[i]--;
} else if (orders[o] == 46){
std::cout << char(mem[i]) ;
} else if (orders[o] == 44){
char temp ;
std::cout << "Input at " << i << ": ";
std::cin >> temp;
mem[i] = int(temp);
} else if (orders[o] == 91){
if (mem[i] == 0){
o = findClosing(orders.data(),o);
}
} else if (orders[o] == 93){
if (mem[i] != 0){
o = findOpening(orders.data(),o);
}
}
}
return 0;
}
return 1;
}