-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm18.js
59 lines (54 loc) · 1.41 KB
/
algorithm18.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
function solution(answers) {
var answer = [];
let rule1 = [1, 2, 3, 4, 5]
let rule2 = [1, 3, 4, 5]
let rule3 = [3, 1, 2, 4, 5]
let player1 = []
let player2 = []
let player3 = []
let counter1 = 0
let counter2 = 0
let counter3 = 0
let score = [0, 0, 0]
for(let i=0; i<answers.length; i++){
player1.push(rule1[i%5])
if(i%2 == 0){
player2.push(2)
} else {
player2.push(rule2[counter3%4])
counter3 += 1
}
if(i%2 == 0){
player3.push(rule3[counter1%5])
counter1 += 1
} else {
player3.push(rule3[counter2%5])
counter2 += 1
}
}
// console.log(player2)
for(let i=0; i<answers.length; i++){
if(answers[i] == player1[i]){
score[0] += 1
}
if(answers[i] == player2[i]){
score[1] += 1
}
if(answers[i] == player3[i]){
score[2] += 1
}
}
console.log(score)
let val = score.map(c => c == Math.max(...score))
for(let i=0; i< val.length; i++){
let temp = val.indexOf(true, i)
if(temp != -1){
answer.push(temp+1)
i = val.indexOf(true, i)
} else {
break
}
}
if(answer[answer.length-1] == -1){answer.splice(answer.length-1, 1)}
return answer;
}