-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (56 loc) · 1.85 KB
/
index.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
const { readFile } = require('fs/promises');
(async () => {
let compareNames = [];
let strict = false;
// Process arguments.
process.argv.slice(2).forEach((argument) => {
if (argument.substring(0, 2) === '--') {
if (argument === '--strict') {
strict = true;
}
} else {
compareNames.push(argument.toUpperCase());
}
});
// Ensure we have at least two names.
if (compareNames.length < 2) {
console.error('You must provide at least two names to compare.');
process.exit(1);
}
// Get a full list of letters from the inputs.
const allLetters = compareNames.reduce((acc, item) => [...acc, ...item.split('')], []);
// Get a unique list of letters from the inputs.
const letters = allLetters.reduce((acc, item) => {
if (!acc.includes(item)) {
acc.push(item);
}
return acc;
}, []);
// Read in the list of names and convert it to an array.
const namesRaw = await readFile('./names.txt');
const names = namesRaw.toString().split("\n");
// Loop the names and find ones that can be made from just the unique letters.
const found = [];
names.forEach((name) => {
const remainingLetters = strict ? [...allLetters] : [];
for (let i = 0; i < name.length; i ++) {
// Determine if the unique letters contains this letter at all.
if (!letters.includes(name.charAt(i))) {
return;
}
// If we're in strict mode, ensure the letter exists in the remaining letters, then remove it.
if (strict) {
const pos = remainingLetters.findIndex((letter) => letter === name.charAt(i));
if (pos === -1) {
return;
}
remainingLetters.splice(pos, 1);
}
}
found.push(name);
});
// Print results alphabetically.
found.sort();
found.forEach((name) => console.log(name));
console.log(`Found: ${found.length}`);
})();