Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- // PQ@Alderaa1:30000!A!->20000
- // @Cantonica:3000!D!->4000NM
- let pattern = /.*@(?<planet>[A-Za-z]+){1}[^@!\-:>]*:(?<population>\d+){1}[^@!\-:>]*!(?<action>[AD]){1}![^@!\-:>]*->(?<army>\d+){1}.*/m;
- let arr3 = [];
- let letterCheck = /[s,t,a,r]+/gi;
- let obj = {
- attack: [],
- destr: []
- };
- for (let i = 1; i <= input[0]; i++) {
- if (input[i].match(letterCheck) !== null) {
- let count = input[i].match(letterCheck).join('').length;
- let arr = input[i].split('');
- let arr2 = [];
- for (let j = 0; j < arr.length; j++) {
- arr2.push(String.fromCharCode(arr[j].charCodeAt() - count));
- }
- arr3.push(arr2.join(''));
- if (arr3[arr3.length - 1].match(pattern) === null) {
- arr3.pop();
- }
- } else {
- arr3.push(input[i]);
- }
- }
- // planet name -> planet population -> attack type -> soldier count
- for (let i = 0; i < arr3.length; i++) {
- let match = pattern.exec(arr3[i]);
- if (match !== null) {
- let [_, name, popul, attType, solCount] = match;
- if (attType === 'A') {
- obj.attack.push(name);
- } else if (attType === 'D') {
- obj.destr.push(name);
- }
- }
- }
- // console.log(obj);
- for (let key in obj) {
- obj[key] = obj[key].sort((a, b) => a.localeCompare(b));
- }
- for (let key in obj) {
- // Attacked planets: {attackedPlanetsCount}
- // -> {planetName}
- // Destroyed planets: {destroyedPlanetsCount}
- // -> {planetName}
- if (obj.hasOwnProperty(['attack'])) {
- console.log(`Attacked planets: ${obj.attack.length}`);
- if (obj.attack.length > 0) {
- for (let el of obj.attack) {
- console.log(`-> ${el}`);
- }
- }
- delete obj.attack;
- }
- if (obj.hasOwnProperty(['destr'])) {
- console.log(`Destroyed planets: ${obj.destr.length}`);
- if (obj.destr.length > 0) {
- for (let el of obj.destr) {
- console.log(`-> ${el}`);
- }
- }
- delete obj.destr;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement