WindFell

Untitled

Oct 14th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. function solve(input) {
  2. let warehouse = new Map();
  3. input.forEach(i => {
  4. let [command, brand, coffee, date, quantity] = i.split(", ");
  5.  
  6. switch (command) {
  7. case "IN":
  8. if (!warehouse.has(brand)){
  9. warehouse.set(brand, new Map());
  10. }
  11.  
  12. if (!warehouse.get(brand).has(coffee)) {
  13. warehouse.get(brand).set(coffee, {date: date, quantity: quantity});
  14. }else{
  15. if (warehouse.get(brand).get(coffee).date.localeCompare(date) < 0) {
  16. warehouse.get(brand).set(coffee, {date: date, quantity: quantity});
  17. }else if (warehouse.get(brand).get(coffee).date.localeCompare(date) < 0 === 0) {
  18. warehouse.get(brand).get(coffee).quantity += quantity;
  19. }
  20. }
  21. break;
  22. case "OUT":
  23. if (warehouse.has(brand) && warehouse.get(brand).has(coffee)) {
  24. if (warehouse.get(brand).get(coffee).date.localeCompare(date) > 0) {
  25. warehouse.get(brand).get(coffee).quantity -= quantity;
  26. }
  27. }
  28. break;
  29. case "REPORT":
  30. console.log(">>>>> REPORT! <<<<<");
  31.  
  32. for (const [key, value] of warehouse) {
  33. console.log(`Brand: ${key}:`);
  34. for (const [k, v] of value) {
  35. console.log(`-> ${k} -> ${v.date} -> ${v.quantity}.`);
  36. }
  37. }
  38. break;
  39. case "INSPECTION":
  40. console.log(`>>>>> INSPECTION! <<<<<`);
  41. let sortedBrands = [...warehouse.keys()].sort((a, b) => a.localeCompare(b));
  42. for (const sortedBrand of sortedBrands) {
  43. console.log(`Brand: ${sortedBrand}:`);
  44.  
  45. let sortedCoffees = [...warehouse.get(sortedBrand).keys()]
  46. .sort((a, b) => warehouse.get(sortedBrand).get(b).quantity - warehouse.get(sortedBrand).get(a).quantity);
  47.  
  48. for (const sortedCoffee of sortedCoffees) {
  49. console.log(`-> ${sortedCoffee} -> ${warehouse.get(sortedBrand).get(sortedCoffee).date} -> ${warehouse.get(sortedBrand).get(sortedCoffee).quantity}.`);
  50. }
  51. }
  52.  
  53. break;
  54. }
  55. });
  56. }
  57.  
  58. solve([
  59. "IN, Batdorf & Bronson, Espresso, 2025-05-25, 20",
  60. "IN, Folgers, Black Silk, 2023-03-01, 14",
  61. "IN, Lavazza, Crema e Gusto, 2023-05-01, 5",
  62. "IN, Lavazza, Crema e Gusto, 2023-05-02, 5",
  63. "IN, Folgers, Black Silk, 2022-01-01, 10",
  64. "IN, Lavazza, Intenso, 2022-07-19, 20",
  65. "OUT, Dallmayr, Espresso, 2022-07-19, 5",
  66. "OUT, Dallmayr, Crema, 2022-07-19, 5",
  67. "OUT, Lavazza, Crema e Gusto, 2020-01-28, 2",
  68. "REPORT",
  69. "INSPECTION",
  70. ]
  71. );
Advertisement
Add Comment
Please, Sign In to add comment