選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

aoc6-2.py 1.2KB

3年前
123456789101112131415161718192021222324252627282930313233343536373839
  1. class LanternfishSchool:
  2. def __init__(self, counts, size_inc=100000):
  3. self.time_left = {}
  4. self.time_left["ttr"] = 0
  5. for i in range(9):
  6. self.time_left[i] = counts.count(i)
  7. self.size_inc = size_inc
  8. self.size_base = size_inc
  9. def age(self):
  10. self.time_left["ttr"] = self.time_left[0]
  11. for i in range(1,9):
  12. self.time_left[i-1] = self.time_left[i]
  13. self.time_left[8] = self.time_left["ttr"]
  14. self.time_left[6] += self.time_left["ttr"]
  15. self.time_left["ttr"] = 0
  16. if self.school_size() >= self.size_base:
  17. print(f"School size now {self.school_size()}.")
  18. def school_size(self):
  19. total = 0
  20. for _,v in self.time_left.items():
  21. total += v
  22. return total
  23. def main():
  24. # The old version gets untenable above about 100 days.
  25. # Trying a different approach.
  26. num_days = 256
  27. with open("aoc6-1.txt", "r") as file:
  28. fish_ages = [int(age) for age in file.readlines()[0].split(",")]
  29. lfs = LanternfishSchool(fish_ages)
  30. for _ in range(num_days):
  31. lfs.age()
  32. print(lfs.school_size())
  33. if __name__ == "__main__":
  34. main()