-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathThe-Dart-101.js
77 lines (72 loc) · 1.95 KB
/
The-Dart-101.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
// https://www.codingame.com/training/easy/the-dart-101
const players = [...Array(+readline())].map(() => {
const name = readline();
return { name, stats: {}, shots: [] };
});
const MISSED = 0;
const THROWS_MAX = 3;
const SCORE_TARGET = 101;
const SCORE_FIRST_MISS = 20;
const SCORE_SECOND_MISS = 30;
const SCORE_THIRD_MISS = 0;
players.forEach(
player =>
(player.shots = readline()
.split(' ')
.map(score => (score === 'X' ? MISSED : eval(score))))
);
players.forEach(
player =>
(player.stats = player.shots.reduce(
(stats, currScore) => {
if (stats.throws === THROWS_MAX) {
stats.rounds++;
stats.lastRoundScore = stats.score;
stats.throws = 0;
stats.missStreak = 0;
}
stats.throws++;
if (currScore === MISSED) {
stats.missStreak++;
stats.score =
stats.missStreak === 1
? stats.score - SCORE_FIRST_MISS
: stats.missStreak === 2
? stats.score - SCORE_SECOND_MISS
: 0;
// No negative score
stats.score = stats.score < 0 ? 0 : stats.score;
} else {
if (stats.score + currScore > SCORE_TARGET) {
stats.score = stats.lastRoundScore;
stats.throws = THROWS_MAX;
} else {
stats.missStreak = 0;
stats.score += currScore;
}
}
return stats;
},
{
score: 0,
lastRoundScore: 0,
throws: 0,
missStreak: 0,
rounds: 0
}
))
);
console.log(
players
.filter(player => player.stats.score === SCORE_TARGET)
.reduce((bestPlayer, currPlayer) => {
if (
(currPlayer.stats.rounds == bestPlayer.stats.rounds &&
currPlayer.stats.throws < bestPlayer.stats.throws) ||
currPlayer.stats.rounds < bestPlayer.stats.rounds
) {
return currPlayer;
}
return bestPlayer;
}).name
);