-
Notifications
You must be signed in to change notification settings - Fork 17
/
(medium) Scrabble.js
48 lines (41 loc) · 1.47 KB
/
(medium) Scrabble.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
/** Scrabble (medium) https://www.codingame.com/training/medium/scrabble
* This puzzle makes you use nested loops and combine multiple associative
* arrays in order to build an efficient solution.
*
* Statement:
* The goal of this problem is to find a word from a dictionary to get the
* best score at the popular scrabble game. The solving of this problem
* relies on associative arrays and the methodology used to compute the
* best score. A good choice of the data structure is often the key to
* correctly solve a puzzle.
*
* Story:
* When you play scrabble on sundays with your family, the situation quickly
* goes out of hand. What's the best move ? Should you play "pool" or "loop"?
* What will grandma do ? Here we propose a new puzzle to calculate the
* best word to play out of a list.
*/
const value = {
e: 1, a: 1, i: 1, o: 1, n: 1, r: 1, t: 1, l: 1, s: 1, u: 1,
d: 2, g: 2,
b: 3, c: 3, m: 3, p: 3,
f: 4, h: 4, v: 4, w: 4, y: 4,
k: 5,
j: 8, x: 8,
q: 10, z: 10
};
const Words = [...Array(+readline())].map(_ => readline());
const LETTERS = readline();
let max = -Infinity;
let word = '';
Words.forEach((w) => {
let sum = 0;
const currentSet = [...LETTERS];
[...w].forEach((currentLetter) => {
sum += (currentSet.includes(currentLetter))
? (currentSet[currentSet.indexOf(currentLetter)] = '', value[currentLetter])
: -Infinity;
});
sum > max && (word = w, max = sum);
});
print(word);