From 35da01d66aba379b8659c4f45114865689fa3e48 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Tue, 7 Dec 2021 14:50:37 -0500 Subject: [PATCH] Easier math for part 2. --- 2021/6/main2.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2021/6/main2.py diff --git a/2021/6/main2.py b/2021/6/main2.py new file mode 100644 index 0000000..4d97f19 --- /dev/null +++ b/2021/6/main2.py @@ -0,0 +1,55 @@ +# "constants": +DAYS = 256 + +# 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] + +#### 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) + +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) +