Advertisement
Guest User

Untitled

a guest
Dec 6th, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. W pierwszym liczyłem każdą rybkę osobno ale przeglądarka nie dała rady i nawet node wywaliło po 1.5GB zużytej pamięci w drugim dopiero wtedy rozkminiłem, że można policzyć ile ma się rybek w jakim wieku i je grupami mnożyć różnica w chuj. :D
  2.  
  3. ```
  4. init = [3,4,3,1,2]
  5.  
  6. days = [...(Array(81))].map((_, index) => index);
  7.  
  8. populationAges = [...init].reduce((stack, age) => {
  9.     stack[age] = stack[age] ? stack[age] + 1 : 1;
  10.    
  11.     return stack;
  12. }, {});
  13.  
  14. days.forEach((day) => {
  15.     const population = Object.values(populationAges).reduce((sum, val) => (sum + val), 0);
  16.     console.log(`Day #${day} population: ${population}`, populationAges);
  17.    
  18.     const newPopulationAges = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].reduce((stack, age) => {
  19.         const newAge = age - 1;
  20.         const agePopulation = populationAges[age] || 0;
  21.        
  22.         if (newAge !== -1) {
  23.             stack[newAge] = stack[newAge] ? (stack[newAge] + agePopulation) : agePopulation;
  24.         } else {
  25.             stack[6] = stack[newAge] ? (stack[6] + agePopulation) : agePopulation;
  26.             stack[8] = stack[newAge] ? (stack[8] + agePopulation) : agePopulation;
  27.         }
  28.                
  29.         return stack;
  30.     }, {});
  31.    
  32.     populationAges = { ...newPopulationAges };
  33. });
  34. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement