Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let n= input.shift();//Number Astrounafts
- let AllAstrounafts={};
- for (let i = 0; i <n; i++) {
- const[astoname, oxygen1, energy1]=(input.shift()).split(' ');
- let oxygen= Number(oxygen1);
- let energy= Number(energy1);
- AllAstrounafts[astoname]={oxygen,energy};
- }
- let LineCmd= input.shift();
- while(LineCmd !='End'){
- const All=LineCmd.split(' - ');
- const Cmd= All[0];
- let astroname=All[1];
- switch(Cmd)
- {
- case 'Explore':
- let energyneeded=Number(All[2]);
- if( (AllAstrounafts[astroname].energy) >= energyneeded){
- AllAstrounafts[astroname].energy -=energyneeded;
- console.log(`${astroname} has successfully explored a new area and now has ${AllAstrounafts[astroname].energy} energy!`)
- }
- else{
- console.log(`${astroname} does not have enough energy to explore!`)
- }
- break;
- case 'Refuel':
- let neededenergy=200- AllAstrounafts[astroname].energy;
- let amount1=Number(All[2]);
- if((AllAstrounafts[astroname].energy + amount1) >200){
- AllAstrounafts[astroname].energy =200;
- console.log(`${astroname} refueled their energy by ${neededenergy}!`);
- }
- else{
- AllAstrounafts[astroname].energy +=amount1;
- console.log(`${astroname} refueled their energy by ${amount1}!`);
- }
- break;
- case 'Breathe':
- let neededoxygen=100- AllAstrounafts[astroname].oxygen;
- let amount2=Number(All[2]);
- if((AllAstrounafts[astroname].oxygen + amount2)> 100){
- AllAstrounafts[astroname].oxygen=100;
- console.log(`${astroname} took a breath and recovered ${neededoxygen} oxygen!`)
- }
- else{
- AllAstrounafts[astroname].oxygen +=amount2;
- console.log(`${astroname} took a breath and recovered ${amount2} oxygen!`)
- }
- break;
- }
- LineCmd= input.shift();
- }
- letsortedAstronafts= Object.entries(AllAstrounafts);
- for (const astronaft of letsortedAstronafts) {
- console.log(`Astronaut: ${astronaft[0]}, Oxygen: ${astronaft[1].oxygen}, Energy: ${astronaft[1].energy}`);
- }
- }
- solve([ '3',
- 'John 50 120',
- 'Kate 80 180',
- 'Rob 70 150',
- 'Explore - John - 50',
- 'Refuel - Kate - 30',
- 'Breathe - Rob - 20',
- 'End'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement