Browse Source

Day 6

master
Noëlle 3 years ago
parent
commit
3dbd119ea1
No known key found for this signature in database
2 changed files with 75 additions and 0 deletions
  1. 36
    0
      aoc6-1.py
  2. 39
    0
      aoc6-2.py

+ 36
- 0
aoc6-1.py View File

@@ -0,0 +1,36 @@
class Lanternfish:
def __init__(self, count=None):
self.count = 8 if count == None else count

def age(self):
self.count -= 1
if self.count == -1:
self.count = 6
return True
return False

def main():
num_days = 80

with open("aoc6-1.txt", "r") as file:
fish_ages = [int(age) for age in file.readlines()[0].split(",")]
fishes = []
for age in fish_ages:
fishes.append(Lanternfish(age))

fish_benchmark, fish_increment = 10000, 10000
for day in range(num_days):
num_fish = len(fishes)
if num_fish > fish_benchmark:
print(f"Reached {num_fish} lanternfish on day {day}.")
fish_benchmark += fish_increment
for i in range(num_fish):
if fishes[i].age():
fishes.append(Lanternfish())

print(len(fishes))
return len(fishes)


if __name__ == "__main__":
main()

+ 39
- 0
aoc6-2.py View File

@@ -0,0 +1,39 @@
class LanternfishSchool:
def __init__(self, counts, size_inc=100000):
self.time_left = {}
self.time_left["ttr"] = 0
for i in range(9):
self.time_left[i] = counts.count(i)
self.size_inc = size_inc
self.size_base = size_inc

def age(self):
self.time_left["ttr"] = self.time_left[0]
for i in range(1,9):
self.time_left[i-1] = self.time_left[i]
self.time_left[8] = self.time_left["ttr"]
self.time_left[6] += self.time_left["ttr"]
self.time_left["ttr"] = 0
if self.school_size() >= self.size_base:
print(f"School size now {self.school_size()}.")

def school_size(self):
total = 0
for _,v in self.time_left.items():
total += v
return total

def main():
# The old version gets untenable above about 100 days.
# Trying a different approach.
num_days = 256
with open("aoc6-1.txt", "r") as file:
fish_ages = [int(age) for age in file.readlines()[0].split(",")]

lfs = LanternfishSchool(fish_ages)
for _ in range(num_days):
lfs.age()
print(lfs.school_size())

if __name__ == "__main__":
main()

Loading…
Cancel
Save