For context I was solving the day 6 problem from the 2021 Advent of Code, and wanted to try using numpy arrays over python lists since from my current understanding they should be faster. But ran into an issue, my solution prints the correct answer but takes ages to finish computing as the number_of_days_to_cycle_through variable scales.
I wanted help in understanding why the incredibly long scaling was occurring, and how to notice/prevent that mistake in my code going forward?(the lantern_fish_array is a numpy array of int64)
def iterate_through_one_day(lantern_fish_array):
iterator = 0
copy_of_lantern_fish_array = lantern_fish_array.copy()
for fish in copy_of_lantern_fish_array:
if fish == 0:
lantern_fish_array = np.append(lantern_fish_array, 8)
lantern_fish_array[iterator] = 6
else:
lantern_fish_array[iterator] -= 1
iterator += 1
del copy_of_lantern_fish_array
return new_lantern_fish_array
def solve_part_1(lantern_fish_array):
num_of_days_to_cycle_through = 256
while num_of_days_to_cycle_through != 0:
lantern_fish_array = iterate_through_one_day(lantern_fish_array)
num_of_days_to_cycle_through -= 1
return lantern_fish_array.size