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 = [];
- 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('');
- }
- // loop through the input
- for (let i = 0; i < count; i++) {
- // parse encrypted message
- let encryptedMessage = input[i];
- // decrypt it by aplying star enigma decription rules
- let decryptedMessage = decrypt(encryptedMessage);
- // extract required elements from decrypted message by aplying RegEx with described rules
- let regexMessage = /@(?<planetName>[A-Za-z]+)[^@\-!:>]*:(?<population>\d+)[^@\-!:>]*!(?<attackType>[A|D])![^@\-!:>]*\->(?<soldierCount>\d+)[^@\-!:>]*/g;
- let result = regexMessage.exec(decryptedMessage);
- if (result !== null) {
- let attack = result.groups.attackType;
- if (attack === 'A') {
- attackedPlanets.push(result.groups.planetName);
- } else if (attack === 'D') {
- destroyedPlanets.push(result.groups.planetName);
- }
- }
- }
- console.log(`Attacked planets: ${attackedPlanets.length}`);
- attackedPlanets
- .sort((a, b) => a.localeCompare(b)) // sorting alphabetically
- .forEach(planet => console.log(`-> ${planet}`)); // print each planet of the attackedPlanets array
- console.log(`Destroyed planets: ${destroyedPlanets.length}`);
- destroyedPlanets
- .sort((a, b) => a.localeCompare(b)) // sorting alphabetically
- .forEach(planet => console.log(`-> ${planet}`)); // print each planet of the destroyedPlanets array
- }
- starEnigma([
- '2',
- 'STCDoghudd4=63333$D$0A53333',
- 'EHfsytsnhf?8555&I&2C9555SR',
- 'GQhrr|A977777(H(TTTT',
- ]);
- console.log('---');
- starEnigma([
- '3',
- "tt(''DGsvywgerx>6444444444%H%1B9444",
- 'GQhrr|A977777(H(TTTT',
- 'EHfsytsnhf?8555&I&2C9555SR'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement