Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let records = {};
- for (let line of input) {
- if (line === 'Results') {
- break;
- }
- let tokens = line.split(':');
- let command = tokens[0];
- if (command === 'Add') {
- let person = tokens[1];
- let health = Number(tokens[2]);
- let energy = Number(tokens[3]);
- if (!records.hasOwnProperty(person)) {
- records[person] = {};
- records[person].health = 0;
- records[person].energy = energy;
- }
- records[person].health += health;
- } else if (command === 'Attack') {
- let attackerName = tokens[1];
- let defenderName = tokens[2];
- let damage = Number(tokens[3]);
- if (records.hasOwnProperty(attackerName) && records.hasOwnProperty(defenderName)) {
- records[defenderName].health -= damage;
- if (records[defenderName].health <= 0) {
- console.log(`${defenderName} was disqualified!`);
- delete records[defenderName];
- }
- records[attackerName].energy -= 1;
- if (records[attackerName].energy <= 0) {
- console.log(`${attackerName} was disqualified!`);
- delete records[attackerName];
- }
- }
- } else if (command = 'Delete') {
- let username = tokens[1];
- if (username === "All") {
- for (let prop in records) {
- delete records[prop];
- }
- } else {
- if (records.hasOwnProperty(userName)) {
- delete records[userName];
- }
- }
- }
- else {
- continue;
- }
- }
- let count = 0;
- if (Object.keys(records) !== undefined) {
- count = Object.keys(records).length;
- console.log(`People count: ${count}`);
- let sorted = Object.entries(records);
- if (sorted.length > 0) {
- sorted.sort((a, b) => b[1].health - a[1].health || a[0].localeCompare(b[0]));
- sorted.forEach(element => {
- console.log(`${element[0]} - ${element[1].health} - ${element[1].energy}`);
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement