Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let legendary = {
- shards: 'Shadowmourne',
- fragments:'Valanyr',
- motes:'Dragonwrath',
- }
- let mainResource = {
- shards:0,
- fragments:0,
- motes:0
- };
- let regularResource = {};
- function populate(material,quantity){
- let currentQuantity = 0;
- if(!mainResource.hasOwnProperty(material)){
- if(!regularResource.hasOwnProperty(material)){
- regularResource[material] = quantity;
- }else{
- currentQuantity = regularResource[material];
- currentQuantity += quantity;
- regularResource[material] = currentQuantity;
- }
- }else{
- currentQuantity = mainResource[material];
- currentQuantity += quantity;
- mainResource[material] = currentQuantity;
- }
- }
- let incomingResources = input.split(' ');
- for(let i = 0; i < incomingResources.length; i+=2){
- let quantity = Number(incomingResources[i]);
- let material = incomingResources[i+1].toLowerCase();
- populate(material,quantity);
- if(mainResource[material] >= 250){
- console.log(`${legendary[material]} obtained!`);
- let value = mainResource[material];
- value -=250;
- mainResource[material] = value;
- break;
- }
- }
- Object.entries(mainResource).sort((a,b)=>{
- if(a[1] === b[1]){
- return a[0].localeCompare(b[0]);
- }
- return b[1] - a[1];
- }).forEach(pair=>{
- let key = pair[0];
- let val = pair[1];
- console.log(`${key}: ${val}`);
- });
- Object.entries(regularResource).sort((a,b)=>{
- return a[0].localeCompare(b[0]);
- }).forEach(pair=>{
- console.log(`${pair[0]}: ${pair[1]}`);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment