Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function starEnigma(arr) {
- let decryption = /[star]/gim;
- let planetRegex = /@([A-Za-z]+)[^@\-!:>]*:(\d+)[^@\-!:>]*!([AD])![^@\-!:>]*->(\d+)/;
- let numberOfMessages = arr.shift()
- // let newString = ""
- let attackedPlanets = []
- let destroyedPlanets = []
- for (let message of arr) {
- let currentMessage = []
- // let match = message.match(decryption)
- if (message.match(decryption) !== null) {
- matches = message.match(decryption).length
- } else {
- matches = 0
- }
- for (let letter of message) {
- let letterCharCode = letter.charCodeAt(0) - matches
- currentMessage.push(letterCharCode)
- }
- let newString = String.fromCharCode(...currentMessage)
- let planet = planetRegex.exec(newString)
- if (planet !== null) {
- let [_, planetName, population, planetType, soldiers] = planet;
- // let planetName = planet.groups.name
- // let planetType = planet.groups.attackType
- if (planetType == "A") {
- attackedPlanets.push(planetName)
- } else if (planetType == "D") {
- destroyedPlanets.push(planetName)
- }
- }
- };
- console.log(`Attacked planets: ${attackedPlanets.length}`);
- if (attackedPlanets.length > 0) {
- logPlanets(attackedPlanets)
- }
- console.log(`Destroyed planets: ${destroyedPlanets.length}`);
- if (destroyedPlanets.length > 0) {
- logPlanets(destroyedPlanets)
- }
- function logPlanets(arr) {
- arr.sort((a, b) => a.localeCompare(b)).forEach(planet => {
- console.log(`-> ${planet}`);
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement