Latest News

## Wednesday, August 30, 2023

### Lanternfish Python Puzzle

Question:

Lanternfish

You are in presence of specific species of lanternfish. They have one special attribute, each lanternfish creates a new lanternfish once every 7 days.

However, this process isn’t necessarily synchronized between every lanternfish - one lanternfish might have 2 days left until it creates another lanternfish, while another might have 4. So, you can model each fish as a single number that represents the number of days until it creates a new lanternfish.

Furthermore, you reason, a new lanternfish would surely need slightly longer before it’s capable of producing more lanternfish: two more days for its first cycle.

So, suppose you have a lanternfish with an internal timer value of 3:

After one day, its internal timer would become 2.

After another day, its internal timer would become 1.

After another day, its internal timer would become 0.

After another day, its internal timer would reset to 6, and it would create a new lanternfish with an internal timer of 8.

After another day, the first lanternfish would have an internal timer of 5, and the second lanternfish would have an internal timer of 7.

A lanternfish that creates a new fish resets its timer to 6, not 7 (because 0 is included as a valid timer value). The new lanternfish starts with an internal timer of 8 and does not start counting down until the next day.

For example, suppose you were given the following list:

3,4,3,1,2

This list means that the first fish has an internal timer of 3, the second fish has an internal timer of 4, and so on until the fifth fish, which has an internal timer of 2. Simulating these fish over several days would proceed as follows:

Initial state: 3,4,3,1,2

After 1 day: 2,3,2,0,1

After 2 days: 1,2,1,6,0,8

After 3 days: 0,1,0,5,6,7,8

After 4 days: 6,0,6,4,5,6,7,8,8

After 5 days: 5,6,5,3,4,5,6,7,7,8

After 6 days: 4,5,4,2,3,4,5,6,6,7

After 7 days: 3,4,3,1,2,3,4,5,5,6

After 8 days: 2,3,2,0,1,2,3,4,4,5

After 9 days: 1,2,1,6,0,1,2,3,3,4,8

After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8

After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8

After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8

After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8

After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8

After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7

After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8

After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8

After 18 days: 6,0,6,4,5,6,0,1,1,2,6,0,1,1,1,2,2,3,3,4,6,7,8,8,8,8

Each day, a 0 becomes a 6 and adds a new 8 to the end of the list, while each other number decreases by 1 if it was present at the start of the day.

In this example, after 18 days, there are a total of 26 fish.

Question 1 (easy): How many lanternfish would there be after 80 days?

Question 2 (harder): How many lanternfish would there be after 400 days?

Solution:

The provided code below is an implementation of a simulation to count the number of lanternfish based on a set of rules that govern their behavior over a certain number of days.

Let's break down the code step by step:

1. from collections import deque`: This line imports the `deque` class from the `collections` module. A deque (short for "double-ended queue") is a data structure that allows efficient adding and removing of elements from both ends.
2. def count_fish(days, fish):`: This defines a function called `count_fish` that takes two arguments: `days` (the number of days to simulate) and `fish` (a list of integers representing the countdown timers of fish).
3. bins = deque([0] * 9)`: Here, a deque called `bins` is initialized with 9 positions, each containing the value 0. These positions represent countdown timers from 0 to 8.
4. for f in fish:`: This loop iterates over each integer in the `fish` list.
5. bins[f] += 1`: For each integer in the `fish` list, the corresponding position in the `bins` deque is incremented by 1. This is essentially counting how many fish have countdown timers equal to each value from 0 to 8.
6. for day in range(days):`: This loop iterates over each day in the range from 0 to `days - 1`.
7. bins[7] += bins[0]`: On each day, the value at position 7 in the `bins` deque (representing fish with a countdown timer of 7) is increased by the number of fish with a countdown timer of 0.
8. bins.rotate(-1)`: After updating the counts for each day, the `bins` deque is rotated to the left by one position. This effectively decrements the countdown timers for all fish.
9. return sum(bins)`: Finally, the sum of all the values in the `bins` deque is returned, which represents the total number of lanternfish after simulating for the specified number of days.

The provided data `[3, 4, 3, 1, 2]` represents the initial countdown timers of the fish.

The subsequent lines of code execute the `count_fish` function for different numbers of days (80 and 400) using the provided data and print out the results.

In essence, this code simulates the behavior of lanternfish over a certain number of days, considering their countdown timers and the specific rules outlined in the code, and calculates the total number of lanternfish at the end of the simulation.

lanternfish would there be after 80 days 5934
lanternfish would there be after 400 days 7563375241248839