Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let n = arr.shift();//n – the number of messages
- arr = arr.slice(0, n);
- let attackedPlanets = [];
- let destroyedPlanets = [];
- arr.forEach(line => {
- let decryptionKey = line.match(/[s, t, a, r]/gi).length;
- line = line.split('').map(e => String.fromCharCode((e.charCodeAt(0) - decryptionKey))).join('');
- let pattern = /[^@!:>-]*@(?<planet>[A-Za-z]+)[^@!:>-]*:(?<population>[0-9]+)[^@!:>-]*!(?<attackType>[AD])![^@!:>-]*->(?<soldierCount>[0-9]+)/gm;
- let result = pattern.exec(line);
- if (result) {
- let attackType = result.groups.attackType;
- if (attackType === 'A') {
- attackedPlanets.push(result.groups.planet);
- } else if (attackType === 'D') {
- destroyedPlanets.push(result.groups.planet);
- }
- }
- });
- attackedPlanets.sort((a, b) => a.localeCompare(b));
- destroyedPlanets.sort((a, b) => a.localeCompare(b));
- console.log(`Attacked planets: ${attackedPlanets.length}`);
- if (attackedPlanets.length !== 0) {
- for (let planet of attackedPlanets) {
- console.log(`-> ${planet}`);
- }
- }
- console.log(`Destroyed planets: ${destroyedPlanets.length}`);
- if (destroyedPlanets.length !== 0) {
- for (let planet of destroyedPlanets) {
- console.log(`-> ${planet}`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement