From dbaab6e787381b0f6078475fe3c28ba279a977bb Mon Sep 17 00:00:00 2001 From: David Vereb Date: Tue, 7 Dec 2021 15:01:25 -0500 Subject: [PATCH] Cleaned up code for part two of day 6. --- 2021/6/main2.py | 56 ++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 38 deletions(-) diff --git a/2021/6/main2.py b/2021/6/main2.py index 4d97f19..71c143d 100644 --- a/2021/6/main2.py +++ b/2021/6/main2.py @@ -1,55 +1,35 @@ # "constants": -DAYS = 256 +total_fish = 0 # intialize shared data: fish_reproducing_on_day = [] -for i in range(0, DAYS + 9): - fish_reproducing_on_day.append(0) # functions: -def SimulateFish(days_until_next_fish, total_days_left): - # # DEBUG(dev): - # print('SimulateFish(', days_until_next_fish, ',', total_days_left, ')') - - # If we're past the final date, don't count any more: - if(total_days_left - days_until_next_fish <= 0): - # add 1 for THIS fish - return 1 - - total = 0 - # Add up all the totals of the fish this one is going to spawn - for i in range(total_days_left - days_until_next_fish, 0, -7): - total += SimulateFish(9, i) - # Add this fish, as well - total += 1 - - # Return grand total from this fish (INCLUDING this fish) - return total - def SimulateFishByDay(current_day): fish_reproducing_on_day[current_day + 7] += fish_reproducing_on_day[current_day] fish_reproducing_on_day[current_day + 9] += fish_reproducing_on_day[current_day] return fish_reproducing_on_day[current_day] +def Simulate(days): + # reset data in case of multiple simulations: + fish_reproducing_on_day.clear() + for i in range(0, days + 9): + fish_reproducing_on_day.append(0) + + for i in range(0, len(fish)): + fish_reproducing_on_day[int(fish[i])] += 1 + + grand_total = total_fish # starting fish count + for i in range(0, days): + grand_total += SimulateFishByDay(i) + return grand_total + #### MAIN EXECUTION #### fish = [] with open('data', 'r') as fp: fish = fp.readline().strip().split(',') -grand_total_1 = 0 -for i in range(0, len(fish)): - grand_total_1 += SimulateFish(int(fish[i]), 80) +total_fish = len(fish) -print(grand_total_1) - - - -for i in range(0, len(fish)): - fish_reproducing_on_day[int(fish[i])] += 1 - -grand_total_2 = 300 # starting fish count -for i in range(0, DAYS): - print(fish_reproducing_on_day) - grand_total_2 += SimulateFishByDay(i) -print(grand_total_2) - +print(Simulate(80)) +print(Simulate(256))