[2021] day 6 solved part 1 naively

This commit is contained in:
Maciej Jur 2021-12-06 19:21:32 +01:00
parent 51705d963f
commit d9182cbff4
2 changed files with 31 additions and 0 deletions

1
2021/.input/day06 Normal file
View file

@ -0,0 +1 @@
3,4,1,1,5,1,3,1,1,3,5,1,1,5,3,2,4,2,2,2,1,1,1,1,5,1,1,1,1,1,3,1,1,5,4,1,1,1,4,1,1,1,1,2,3,2,5,1,5,1,2,1,1,1,4,1,1,1,1,3,1,1,3,1,1,1,1,1,1,2,3,4,2,1,3,1,1,2,1,1,2,1,5,2,1,1,1,1,1,1,4,1,1,1,1,5,1,4,1,1,1,3,3,1,3,1,3,1,4,1,1,1,1,1,4,5,1,1,3,2,2,5,5,4,3,1,2,1,1,1,4,1,3,4,1,1,1,1,2,1,1,3,2,1,1,1,1,1,4,1,1,1,4,4,5,2,1,1,1,1,1,2,4,2,1,1,1,2,1,1,2,1,5,1,5,2,5,5,1,1,3,1,4,1,1,1,1,1,1,1,4,1,1,4,1,1,1,1,1,2,1,2,1,1,1,5,1,1,3,5,1,1,5,5,3,5,3,4,1,1,1,3,1,1,3,1,1,1,1,1,1,5,1,3,1,5,1,1,4,1,3,1,1,1,2,1,1,1,2,1,5,1,1,1,1,4,1,3,2,3,4,1,3,5,3,4,1,4,4,4,1,3,2,4,1,4,1,1,2,1,3,1,5,5,1,5,1,1,1,5,2,1,2,3,1,4,3,3,4,3

30
2021/Python/day06.py Normal file
View file

@ -0,0 +1,30 @@
class LanternFish:
def __init__(self, cycle: int, current: int = None):
self.cycle = cycle
self.current = current or cycle
self.children = []
def advance(self) -> None:
self.current -= 1
[child.advance() for child in self.children]
if self.current == -1:
self.current = self.cycle
self.children.append(LanternFish(cycle=6, current=8))
def count(self) -> int:
return 1 + sum([child.count() for child in self.children])
def load() -> list[int]:
with open('../.input/day06') as f:
return [int(line) for line in f.read().split(",")]
def solve1() -> int:
fishes = [LanternFish(cycle=6, current=init) for init in load()]
[[fish.advance() for fish in fishes] for _ in range(100)]
return sum([fish.count() for fish in fishes])
if __name__ == "__main__":
print(solve1())