-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path다트게임.js
53 lines (49 loc) · 1.44 KB
/
다트게임.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
const NOT_FOUND = -1;
const NOT_DETERMINED = -1;
const getScore = (startIdx, input) => {
const bonus = ["S", "D", "T"];
const bonusIdx = new Array(bonus.length).fill().reduce((result, _, i) => {
const idx = input.indexOf(bonus[i], startIdx);
if (idx !== NOT_FOUND) {
if (result === NOT_DETERMINED) {
return idx;
}
return Math.min(result, idx);
}
return result;
}, NOT_DETERMINED);
return [+input.substring(startIdx, bonusIdx), bonusIdx];
};
const calcBonusScore = (score, bonus) => {
const bonusCalcMap = {
S: (score) => score,
D: (score) => score ** 2,
T: (score) => score ** 3,
};
return bonusCalcMap[bonus](score);
};
function solution(dartResult) {
const gameScore = [];
let i = 0;
while (i < dartResult.length) {
const [score, bonusIdx] = getScore(i, dartResult);
const bonusScore = calcBonusScore(score, dartResult[bonusIdx]);
const optIdx = bonusIdx + 1;
if (dartResult[optIdx] === "*") {
if (gameScore.length !== 0) {
gameScore[gameScore.length - 1] *= 2;
}
gameScore.push(bonusScore * 2);
i = optIdx + 1;
} else if (dartResult[optIdx] === "#") {
gameScore.push(bonusScore * -1);
i = optIdx + 1;
} else {
gameScore.push(bonusScore);
i = bonusIdx + 1;
}
}
const answer = gameScore.reduce((sum, score) => sum + score, 0);
return answer;
}
module.exports = { solution, calcBonusScore, getScore };