Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let pattern = /[star]/gim;
- let messages = Number(input.shift());
- let decryptedMessages = [];
- for (let i = 0; i < messages; i++) {
- let message = input.shift();
- let match = message.match(pattern);
- if (match) {
- let countSymbols = match.length;
- let decryptedMessage = [];
- for (let i = 0; i < message.length; i++) {
- let symbolIndex = message[i].charCodeAt(0);
- let newSymbolIndex = symbolIndex - countSymbols;
- let newSymbol = String.fromCharCode(newSymbolIndex);
- decryptedMessage.push(newSymbol);
- }
- decryptedMessages.push(decryptedMessage.join(""));
- } else {
- let countSymbols = 0;
- let decryptedMessage = [];
- for (let i = 0; i < message.length; i++) {
- let symbolIndex = message[i].charCodeAt(0);
- let newSymbolIndex = symbolIndex - countSymbols;
- let newSymbol = String.fromCharCode(newSymbolIndex);
- decryptedMessage.push(newSymbol);
- }
- decryptedMessages.push(decryptedMessage.join(""));
- }
- }
- let atackedPlanets = [];
- let destroyedPlanets = [];
- for (let item of decryptedMessages) {
- let patternTwo = /@(?<name>[A-Za-z]+)[^@\-!:>]*:(?<population>[0-9]+)[^@\-!:>]*![^@\-!:>]*(?<attackType>[A|D])![^@\-!:>]*\->(?<soldierCount>[0-9]+)/;
- let match = patternTwo.exec(item);
- if (match && match.groups.attackType === "A") {
- atackedPlanets.push(match.groups.name);
- } else if (match && match.groups.attackType === "D") {
- destroyedPlanets.push(match.groups.name);
- }
- }
- let sortedAttackedPlanets = atackedPlanets.sort((a, b) => a.localeCompare(b));
- let sortedDestroyedPlanets = destroyedPlanets.sort((a, b) =>
- a.localeCompare(b)
- );
- if (sortedAttackedPlanets.length > 0) {
- console.log(`Attacked planets: ${sortedAttackedPlanets.length}`);
- for (let planet of sortedAttackedPlanets) {
- console.log(`-> ${planet}`);
- }
- } else {
- console.log(`Attacked planets: 0`);
- }
- if (sortedDestroyedPlanets.length > 0) {
- console.log(`Destroyed planets: ${sortedDestroyedPlanets.length}`);
- for (let planet of sortedDestroyedPlanets) {
- console.log(`-> ${planet}`);
- }
- } else {
- console.log(`Destroyed planets: 0`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment