Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let count = Number(input.shift());
- let regexOne = /[star]/gi;
- let reg = /@(?<name>[a-zA-z]+)[^@\-!:>]*:(?<population>[0-9]*)[^@\-!:>]*!(?<type>[AD])![^@\-!:>]*->(?<soldiers>[0-9]+)/g;
- let attacked = [];
- let destroyed = [];
- for (const line of input) {
- let keyMatch = [];
- while ((match = regexOne.exec(line)) !== null) {
- keyMatch.push(match);
- }
- let key = keyMatch.length;
- let decrypted = "";
- for (const letter of line) {
- let newCode = letter.charCodeAt() - key;
- decrypted += String.fromCharCode(newCode);
- }
- while ((newmatch = reg.exec(decrypted)) !== null) {
- if(newmatch.groups.type === "D"){
- destroyed.push(newmatch.groups.name);
- }else{
- attacked.push(newmatch.groups.name);
- }
- }
- }
- console.log(`Attacked planets: ${attacked.length}`);
- if(attacked.length > 0 ){
- attacked.sort((a,b)=>a.localeCompare(b)).forEach(planet=>{
- console.log(`-> ${planet}`);
- })
- }
- console.log(`Destroyed planets: ${destroyed.length}`);
- if(destroyed.length > 0 ){
- destroyed.sort((a,b)=>a.localeCompare(b)).forEach(planet=>{
- console.log(`-> ${planet}`);
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement