Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- const [, ...msgArr] = args;
- const rePat = /[@]{1}(?<name>[a-z]+)[^@\-!:>]*[:]{1}(?<population>[0-9]+)[^@\-!:>]*[!]{1}(?<typeAttack>[A|D])[!]{1}[^@\-!:>]*->(?<soldierCount>[0-9]+)/gi;
- const attackedPlanets = [];
- const destroyedPlanets = [];
- msgArr.forEach((line) => {
- const counterLet = line
- .split('')
- .map((x) => x.toLowerCase())
- .filter((x) => (x === 's' || x === 't' || x === 'a' || x === 'r'))
- .length;
- const decriptedMsg = line.split('').map((elem) => String.fromCharCode(elem.charCodeAt() - counterLet)).join('');
- if (decriptedMsg.match(rePat)) {
- const currentPlanet = rePat.exec(decriptedMsg);
- if (currentPlanet.groups.typeAttack === 'A') {
- attackedPlanets.push(currentPlanet.groups.name);
- } else if (currentPlanet.groups.typeAttack === 'D') {
- destroyedPlanets.push(currentPlanet.groups.name);
- }
- }
- });
- console.log(`Attacked planets: ${attackedPlanets.length}`);
- if (attackedPlanets !== 0) {
- attackedPlanets
- .sort((a, b) => a.localeCompare(b))
- .forEach((planet) => console.log(`-> ${planet}`));
- }
- console.log(`Destroyed planets: ${destroyedPlanets.length}`);
- if (destroyedPlanets !== 0) {
- destroyedPlanets
- .sort((a, b) => a.localeCompare(b))
- .forEach((planet) => console.log(`-> ${planet}`));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement