/
second.js
77 lines (64 loc) · 1.48 KB
/
second.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
const input = require('./input2.js');
const rounds = input.split('\n');
let total = 0;
const score = {
A: 1,
B: 2,
C: 3,
X: 1,
Y: 2,
Z: 3,
win: 6,
lose: 0,
draw: 3,
}
for (let round of rounds) {
const [elf, self] = round.split(' ');
total = total + score[self];
if(score[elf] === score[self]) {
total = total + 3;
};
if ((score[elf] === 3 && score[self] === 1) || (score[elf] === 1 && score[self] === 2) || (score[elf] === 2 && score[self] === 3)) {
total = total + 6;
}
}
console.log('total', total);
let newTotal = 0;
function playScore(elf, result) {
let play = 0;
if(elf === 'A') {
if (result === 'X') {
play = play + 3;
} else if (result === 'Z') {
play = play + 2;
}
}
if (elf === 'B') {
if (result === 'X') {
play = play + 1;
} else if (result === 'Z') {
play = play + 3;
}
}
if (elf === 'C') {
if (result === 'X') {
play = play + 2;
} else if (result === 'Z') {
play = play + 1;
}
}
return play;
}
for (let round of rounds) {
const [elf, result] = round.split(' ');
if(result === 'X') {
newTotal = newTotal + playScore(elf, result);
}
if (result === 'Y') {
newTotal = newTotal + 3 + score[elf];
}
if (result === 'Z') {
newTotal = newTotal + 6 + playScore(elf, result);
}
}
console.log('newTotal', newTotal)