Advertisement
Silviya7

Astro Adventure

Apr 17th, 2024
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3.     let NumberAstrounafts=Number(input.shift());
  4.  
  5.     let AllAstrounafts={};
  6.     for (let i = 0; i < NumberAstrounafts; i++) {
  7.      
  8.         let [name,oxigen, energy]=input[i].split(' ');
  9.         AllAstrounafts[name]={oxigen:Number(oxigen), energy:Number(energy)};
  10.        
  11.     }
  12.  
  13.     for (let j = 0; j <NumberAstrounafts; j++) {
  14.        
  15.         input.shift();
  16.     }
  17.  
  18.     let FirstLine= input.shift();
  19.     while(FirstLine !='End'){
  20.    const [cmd,name,typeneede]= FirstLine.split(' - ');
  21.          switch(cmd){
  22.         case 'Explore':
  23.             let needeenergy=Number(typeneede);
  24.             if(AllAstrounafts[name].energy >= needeenergy){
  25.                 AllAstrounafts[name].energy  -=needeenergy;
  26.                  let currentenergy=AllAstrounafts[name].energy ;
  27.                 console.log(`${name} has successfully explored a new area and now has ${currentenergy} energy!`)
  28.             }
  29.             else{
  30.                 console.log(`${name} does not have enough energy to explore!`)
  31.             }
  32.         break;
  33.         case 'Refuel':
  34.         let amount= Number(typeneede);
  35.         let lastnergy=0;
  36.         if(AllAstrounafts[name].energy <200){
  37.             lastnergy=200-AllAstrounafts[name].energy;
  38.             if(lastnergy <amount){
  39.                 amount=lastnergy;
  40.             }
  41.             AllAstrounafts[name].energy +=amount;
  42.  
  43.             if(  AllAstrounafts[name].energy >200){
  44.                 AllAstrounafts[name].energy=200;
  45.             }
  46.  
  47.             console.log(`${name} refueled their energy by ${amount}!`)        
  48.            
  49.         }
  50.        
  51.          break;
  52.         case 'Breathe':
  53.         let amount1=Number(typeneede);
  54.         let lastoxygen=100- AllAstrounafts[name].oxigen;
  55.         if(lastoxygen <amount1){
  56.             amount1=lastoxygen;
  57.         }
  58.         AllAstrounafts[name].oxigen +=amount1;
  59.  
  60.         if( AllAstrounafts[name].oxigen >100){
  61.             AllAstrounafts[name].oxigen=100;
  62.         }
  63.  
  64.         console.log(`${name} took a breath and recovered ${amount1} oxygen!`)
  65.         break;
  66.  
  67.          }
  68.         FirstLine= input.shift();
  69.  
  70.     }
  71.  
  72.  
  73.     for (const astrounaft in AllAstrounafts) {
  74.        console.log(`Astronaut: ${astrounaft}, Oxygen: ${AllAstrounafts[astrounaft].oxigen}, Energy: ${AllAstrounafts[astrounaft].energy}`)
  75.     }
  76.  
  77.  
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement