Created
December 9, 2021 09:36
-
-
Save huuhoa/44cc22f98b3372cebf3f015aa6e9d40c to your computer and use it in GitHub Desktop.
AoC 2021 - day 6 part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def advance_a_day(numbers): | |
counter = 0 | |
for i in range(len(numbers)): | |
n = numbers[i] - 1 | |
if n == -1: | |
n = 6 | |
counter += 1 | |
numbers[i] = n | |
return counter | |
global_cache = {i:1 for i in range(9)} | |
def mutate(days): | |
fishes = [8] | |
total_fishes = 0 | |
for d in range(days): | |
new_fishes = advance_a_day(fishes) | |
if new_fishes > 0: | |
total_fishes += precalculate(days-d-1) * new_fishes | |
global_cache[days] = len(fishes) + total_fishes | |
def precalculate(days): | |
if days not in global_cache: | |
mutate(days) | |
return global_cache[days] | |
def main(file_path): | |
numbers = read_data(file_path) | |
fishes = [int(n) for n in numbers] | |
total_fishes = 0 | |
max_days = 256 | |
for d in range(max_days): | |
new_fishes = advance_a_day(fishes) | |
total_fishes += precalculate(max_days-d-1) * new_fishes | |
print(total_fishes + len(fishes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment