Advent-of-Code/2021/6/main2.py

36 lines
945 B
Python
Raw Permalink Normal View History

2021-12-07 14:50:37 -05:00
# "constants":
2021-12-07 15:01:25 -05:00
total_fish = 0
2021-12-07 14:50:37 -05:00
# intialize shared data:
fish_reproducing_on_day = []
# functions:
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]
2021-12-07 15:01:25 -05:00
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
2021-12-07 14:50:37 -05:00
#### MAIN EXECUTION ####
fish = []
with open('data', 'r') as fp:
fish = fp.readline().strip().split(',')
2021-12-07 15:01:25 -05:00
total_fish = len(fish)
2021-12-07 14:50:37 -05:00
2021-12-07 15:01:25 -05:00
print(Simulate(80))
print(Simulate(256))