Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const calculateLantarns = (input, iterations) => {
- if (!input) {
- return []
- }
- const reSpawnRate = 7;
- const newSpawnRate = 9;
- const fishes = input.split(',').map(num => parseInt(num));
- const knownFish = fishes.length; // start length
- // Create array with days from current
- const dayBuffer = Array.from({length: iterations + 1}, () => 0)
- // Count how often a known fish is spawn and add to buffer
- fishes.forEach((fish) => {
- // amount of spawns of all remaining days
- const spawns = Math.floor((iterations - fish - 1) / reSpawnRate)
- for (let i = 0; i <= spawns; i++) {
- const spawnDay = fish + (reSpawnRate * i) + 1
- dayBuffer[spawnDay]++;
- }
- })
- // Increase the daily buffer with new fish
- // see per day how many fish will be added in remaining days
- for (let day = 0; day < iterations; day++) {
- const newFishesToday = dayBuffer[day]
- if (dayBuffer[day] > 0) {
- const spawns = Math.floor((iterations - day - newSpawnRate) / reSpawnRate)
- for (let i = 0; i <= spawns; i++) {
- const spawnDay = day + newSpawnRate + (reSpawnRate * i)
- dayBuffer[spawnDay] += newFishesToday;
- }
- }
- }
- // Sum of dayBuffer (amount of fish that are spawned each day)
- const spawnedFish = dayBuffer.reduce((accumulative, current) => accumulative + current);
- return knownFish + spawnedFish
- }
Advertisement
Add Comment
Please, Sign In to add comment