Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- ```
- init = [3,4,3,1,2]
- days = [...(Array(81))].map((_, index) => index);
- populationAges = [...init].reduce((stack, age) => {
- stack[age] = stack[age] ? stack[age] + 1 : 1;
- return stack;
- }, {});
- days.forEach((day) => {
- const population = Object.values(populationAges).reduce((sum, val) => (sum + val), 0);
- console.log(`Day #${day} population: ${population}`, populationAges);
- const newPopulationAges = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].reduce((stack, age) => {
- const newAge = age - 1;
- const agePopulation = populationAges[age] || 0;
- if (newAge !== -1) {
- stack[newAge] = stack[newAge] ? (stack[newAge] + agePopulation) : agePopulation;
- } else {
- stack[6] = stack[newAge] ? (stack[6] + agePopulation) : agePopulation;
- stack[8] = stack[newAge] ? (stack[8] + agePopulation) : agePopulation;
- }
- return stack;
- }, {});
- populationAges = { ...newPopulationAges };
- });
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement