-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.js
42 lines (37 loc) · 981 Bytes
/
security.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
const validate = ({ checksum, encryptedName, sectorId }) => {
const sequence = encryptedName.replace(/-/g, '').split('');
const topFiveLetters = [...new Set([...sequence])]
.map((letter) => {
return {
letter,
score: sequence.filter((x) => x === letter).length
};
})
.sort((a, b) => {
return a.score !== b.score
? b.score - a.score
: a.letter.charCodeAt(0) - b.letter.charCodeAt(0);
})
.slice(0, 5)
.map(({ letter }) => letter)
.join('');
return topFiveLetters === checksum ? sectorId : 0;
};
const sumSectorIds = (input) => {
return input
.split('\n')
.map((room) => {
const parts = room.match(/(.*)-(\d+)\[(\w+)\]/);
return {
encryptedName: parts[1].trim(),
sectorId: parseInt(parts[2]),
checksum: parts[3],
};
})
.map((room) => validate(room))
.reduce((a, b) => a + b, 0);
};
module.exports = {
sumSectorIds,
validate,
};