Cleaned up code for part two of day 6.

This commit is contained in:
David Vereb 2021-12-07 15:01:25 -05:00
parent 35da01d66a
commit dbaab6e787

View File

@ -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)
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)
total_fish = len(fish)
print(Simulate(80))
print(Simulate(256))