Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function starEnigma(input) {
- let numberMessages = Number(input.shift());
- let planets = [];
- let patternStar = /[star]/g;
- for (let command of input) {
- let counter = 0;
- let splitArray = command.split("");
- for (let i = 0; i < splitArray.length; i++) {
- let currentChar = splitArray[i].toLowerCase();
- if (currentChar.match(patternStar)) {
- counter++;
- }
- }
- for (let i = 0; i < splitArray.length; i++) {
- let charCode = splitArray[i].charCodeAt(0);
- let result = String.fromCharCode(charCode - counter);
- splitArray[i] = result;
- }
- let finalString = splitArray.join("");
- planets.push(finalString)
- }
- let attackedCount = 0;
- let attackedPlanets = [];
- let destroyedCount = 0;
- let destroyedPlanets = [];
- let patternPlanetMessage = /@(?<name>[A-Z][a-z]+)[^-@!>\W]*:(?<population>[\d]+)[^-@!>\d]*!(?<command>[AD])![^-@!>]*->(?<soldiers>[\d]+)/g;
- let planetInfo = patternPlanetMessage.exec(planets);
- while (planetInfo !== null) {
- let name = planetInfo.groups['name'];
- let attackCommand = planetInfo.groups['command'];
- switch (attackCommand) {
- case "A":
- attackedCount++
- attackedPlanets.push(name);
- break;
- case "D":
- destroyedCount++;
- destroyedPlanets.push(name);
- break;
- }
- planetInfo = patternPlanetMessage.exec(planets);
- }
- attackedPlanets.sort((a, b) => a.localeCompare(b));
- destroyedPlanets.sort((a, b) => a.localeCompare(b));
- console.log(`Attacked planets: ${attackedCount}`);
- if (attackedPlanets.length > 0) {
- for (let planet of attackedPlanets) {
- console.log(`-> ${planet}`);
- }
- }
- console.log(`Destroyed planets: ${destroyedCount}`);
- if (destroyedPlanets.length > 0) {
- for (let planet of destroyedPlanets) {
- console.log(`-> ${planet}`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement