-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
136 lines (132 loc) · 3.67 KB
/
main.js
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
var input = "AxBxC";
input = input.replace(" ", "");
var inStr = input;
input = input.split("");
var variables = [];
var result = [];
var operations = ["n", "o", "a", "x", "i", "e", "("];
input.forEach(char => {
if (char == char.toUpperCase() && char != char.toLowerCase()) {
if (!variables.includes(char)) variables.push(char);
}
});
result.push(variables.concat(["k"]));
for (var n = 0; n < 2 ** variables.length; n++) {
var str = n.toString(2).split("");
var values = [];
variables.forEach((val, index) => {
if (str[str.length - index - 1] == "1") {
values[variables.length - index - 1] = true;
} else {
values[variables.length - index - 1] = false;
}
});
var invalidInput = false;
var modInput = inStr.split("");
modInput.forEach((char, index) => {
if (variables.includes(char)) {
modInput[index] = values[variables.indexOf(char)];
}
})
while (modInput.length > 1) {
var success = false;
for (var i = 0; i < modInput.length; i++) {
if (operations.includes(modInput[i])) {
switch (modInput[i]) {
case "n":
if (i <= modInput.length - 2) {
if (typeof modInput[i + 1] == "boolean") {
modInput[i + 1] = !modInput[i + 1];
modInput.splice(i, 1);
success = true;
}
}
break;
case "o":
if (i >= 1 && i <= modInput.length - 2) {
if (typeof modInput[i - 1] == "boolean" && typeof modInput[i + 1] == "boolean") {
modInput[i - 1] = modInput[i - 1] || modInput[i + 1];
modInput.splice(i, 2);
success = true;
}
}
break;
case "a":
if (i >= 1 && i <= modInput.length - 2) {
if (typeof modInput[i - 1] == "boolean" && typeof modInput[i + 1] == "boolean") {
modInput[i - 1] = modInput[i - 1] && modInput[i + 1];
modInput.splice(i, 2);
success = true;
}
}
break;
case "x":
if (i >= 1 && i <= modInput.length - 2) {
if (typeof modInput[i - 1] == "boolean" && typeof modInput[i + 1] == "boolean") {
modInput[i - 1] = (modInput[i - 1] || modInput[i + 1]) && !(modInput[i - 1] && modInput[i + 1]);
modInput.splice(i, 2);
success = true;
}
}
break;
case "i":
if (i >= 1 && i <= modInput.length - 2) {
if (typeof modInput[i - 1] == "boolean" && typeof modInput[i + 1] == "boolean") {
if (modInput[i + 1]) {
modInput[i - 1] = true;
}
else {
modInput[i - 1] = !modInput[i - 1];
}
modInput.splice(i, 2);
success = true;
}
}
break;
case "e":
if (i >= 1 && i <= modInput.length - 2) {
if (typeof modInput[i - 1] == "boolean" && typeof modInput[i + 1] == "boolean") {
modInput[i - 1] = modInput[i - 1] == modInput[i + 1]
modInput.splice(i, 2);
success = true;
}
}
break;
case "(":
if (i <= modInput.length - 3) {
if (typeof modInput[i + 1] == "boolean" && modInput[i + 2] == ")") {
modInput.splice(i + 2, 1);
modInput.splice(i, 1);
success = true;
}
}
break;
default:
break;
}
}
if (success) {
break;
}
}
if (!success) {
console.log("Hibs bemenet");
invalidInput = true;
break;
}
}
if (invalidInput) {
result = "Hibás bemenet!";
break;
} else {
if(modInput[0]) modInput = "1";
else modInput = "0";
var newValues = [];
values.forEach(value => {
if(value) newValues.push("1");
else newValues.push("0");
})
result.push(newValues.concat([modInput]));
}
}
console.log(result);