Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function starEnigma(input) {
- // parse how many teams we should loop through the input
- let count = Number(input.shift());
- let attackedPlanets = [];
- let destroyedPlanets = [];
- let decryptedStringsArr = [];
- function decrypt(string) {
- let regexCounter = /s|t|a|r/gi; // RegEx to extract 'star' letters
- let result = string.match(regexCounter); // gives us an array with symbols matched the regexCounter
- let numToDecrease = result.length; // count of star letters
- let array = string.split(''); // converting string to array
- let ASCIIArray = array.map(symbol => symbol.charCodeAt(0)); // converting array symbols to ASCII code
- let decreasedASCIIArray = ASCIIArray.map(a => a - numToDecrease); // reducing each ASCII code by count of matched 'star' letters(e.g. decryption key) in the initial message
- let backToSymbols = decreasedASCIIArray.map(a => String.fromCharCode(a)); // converting new ASCII codes to symbols again
- return backToSymbols.join('');
- }
- for (let i = 0; i < count; i++) {
- let decryptedString = decrypt(input[i]);
- decryptedStringsArr.push(decryptedString);
- }
- let decryptedStrings = decryptedStringsArr.join(' ');
- let regex = /@(?<planetName>[A-Za-z]+)[^@\-!:>]*:(?<population>\d+)[^@\-!:>]*!(?<attackType>[A|D])![^@\-!:>]*\->(?<soldierCount>\d+)[^@\-!:>]*/g;
- let result = '';
- while ((result = regex.exec(decryptedStrings)) !== null) {
- let planetName = result.groups.planetName;
- switch (result.groups.attackType) {
- case 'A': {
- attackedPlanets.push(planetName);
- };
- break;
- case 'D': {
- destroyedPlanets.push(planetName);
- };
- break;
- }
- }
- attackedPlanets.sort((a, b) => a.localeCompare(b));
- destroyedPlanets.sort((a, b) => a.localeCompare(b));
- console.log(`Attacked planets: ${attackedPlanets.length}`);
- attackedPlanets.forEach(planet => console.log(`-> ${planet}`));
- console.log(`Destroyed planets: ${destroyedPlanets.length}`);
- destroyedPlanets.forEach(planet => console.log(`-> ${planet}`));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement