Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let capacity = Number(input.shift())
- let line = input.shift()
- let users = {}
- while (line != "Statistics") {
- let [command, x, a, b] = line.split('=')
- if (command == 'Add') {
- if(!users.hasOwnProperty(x)) {
- users[x] = {sent: Number(a), recieved: Number(b)}
- }
- }else if (command == 'Message') {
- if(users.hasOwnProperty(x) && users.hasOwnProperty(a)){
- users[x].sent += 1
- users[a].recieved += 1
- if(users[x].sent + users[x].recieved >= capacity) {
- delete users[x]
- console.log(`${x} reached the capacity!`)
- }
- if(users[a].sent + users[a].recieved >= capacity) {
- delete users[a]
- console.log(`${a} reached the capacity!`)
- }
- }
- }else if (command == 'Empty'){
- if(users.hasOwnProperty(x)) {
- delete users[x]
- }
- if (x == "All"){
- users = {}
- }
- }
- line = input.shift()
- }
- let usersCount = Object.keys(users).length
- console.log(`Users count: ${usersCount}`)
- let sorted = Object.entries(users).sort((a,b) => Number(b[1].recieved) - Number(a[1].recieved) || a[0].localeCompare(b[0]))
- for(let user of sorted){
- console.log(`${user[0]} - ${Number(user[1].sent) + Number(user[1].recieved)}`)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment