ambiorixdr

Untitled

Feb 17th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function main(input) {
  2.  
  3.     let resources = new Map();
  4.     let i = 0;
  5.     let product = input[i];
  6.  
  7.     while (product !== "stop") {
  8.  
  9.         let quantity = parseInt(input[i + 1]);
  10.  
  11.         if (resources.has(product)) {
  12.  
  13.             let prevQuantity = resources.get(product);
  14.             resources.set(product, prevQuantity + quantity);
  15.         }
  16.         else {
  17.  
  18.             resources.set(product, quantity);
  19.         }
  20.  
  21.         i += 2;
  22.         product = input[i];
  23.     }
  24.  
  25.     for (resource of resources) {
  26.  
  27.         console.log("%s -> %s", resource[0], resource[1]);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment