Easier math for part 2.

This commit is contained in:
David Vereb 2021-12-07 14:50:37 -05:00
parent ff4ebd9f66
commit 35da01d66a

55
2021/6/main2.py Normal file
View File

@ -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)