Skip to content

Instantly share code, notes, and snippets.

@downthecrop
Last active December 6, 2021 07:25
Show Gist options
  • Save downthecrop/5ca5b9c175ed539942ad45c7c74b42e3 to your computer and use it in GitHub Desktop.
Save downthecrop/5ca5b9c175ed539942ad45c7c74b42e3 to your computer and use it in GitHub Desktop.
very interesting!
const days = 256;
const cycle = 6;
// Build a new 9 Length Array.
// Each array element will represent all fish
// with that remaining timer
const fishes = Array(cycle + 3).fill(0);
// load inputs into the index representing their timer
inputs.forEach(_ => fishes[_]++);
for (let i = 0; i < days; i++) {
// How many fish have a timer of 0
const newFish = fishes[0];
// Move the front element to the back
// think of these like the babies. because
// they are new they must have a timer of 8
// this is represented by placing them at index 8.
fishes.push(fishes.shift());
// think of this as reseting the timer for the fish that
// reproduced. 6 days from now they reproduced they will
// reproduce again. this is represented by them being added to
// the amount of fish reproducing in 6 days.
fishes[cycle] += newFish;
}
// Add all the fish together
let sum = 0
fishes.forEach(fishWithTimer => sum += fishWithTimer)
console.log(sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment