-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript.js
65 lines (65 loc) · 2.2 KB
/
Javascript.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
let listCircuit = []; //List des circuits stocké de facon a pouvoir etre exploité pour la suite
let listSwitch = {}; //The list of switches with their state after tom's actions
const C = parseInt(readline());
for (let i = 0; i < C; i++) {
//Parsing to have a structure like this: circuit {name: string, elements: array} elements { type: char ('=' or '-'), switch: array}
const WIRING = readline().split(" ");
let circuit = { name: WIRING[0], elements: [] };
let actualTypeCircuit = null;
let actuelElInCircuit = [];
for (let j = 1; j < WIRING.length; j++) {
let el = WIRING[j];
if (el == "-" || el == "=") {
if (actualTypeCircuit != null) {
circuit.elements.push({ type: actualTypeCircuit, switch: actuelElInCircuit });
actuelElInCircuit = [];
}
actualTypeCircuit = el;
} else {
actuelElInCircuit.push(el);
listSwitch[el] = false;
}
}
circuit.elements.push({ type: actualTypeCircuit, switch: actuelElInCircuit });
listCircuit.push(circuit);
}
const A = parseInt(readline());
for (let i = 0; i < A; i++) {
//Change the state of the switches
const SWITCH = readline();
listSwitch[SWITCH] = !listSwitch[SWITCH];
}
for (let i = 0; i < C; i++) {
let element = listCircuit[i];
let isOn = true; //Stored the state of the circuit element
for (let j = 0; j < element.elements.length; j++) {
if (!isOn) {
break;
}
let el = element.elements[j];
if (el.type == "-") {
//If the switches are in series circuit
for (let k = 0; k < el.switch.length; k++) {
//Browse the state of all the switches in the list if one is turned off set isOn to false
if (!listSwitch[el.switch[k]]) {
isOn = false;
break;
}
}
} else {
//If the switches are in parallel circuit
let bool = false;
for (let k = 0; k < el.switch.length; k++) {
//Browse the state of all the switches in the list if one is turned on set bool to true
if (listSwitch[el.switch[k]]) {
bool = true;
}
}
if (!bool) {
isOn = false;
break;
} //if bool is false set isOn on false
}
}
console.log(element.name + " is " + (isOn ? "ON" : "OFF"));
}