mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2024-11-17 02:23:23 +01:00
6
This commit is contained in:
parent
8b3fe11f20
commit
15aa1f4e18
11
2021/06/README.md
Normal file
11
2021/06/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# 06
|
||||
|
||||
The naive way to do it would be to keep a list. A way more performant ways way to do this is just having an array with 9 entries (0-8) which count the states of the fish. Then you actually just rotate the array each day and add the `0` entry to the `6` additionally.
|
||||
|
||||
<details>
|
||||
<summary>Solutions</summary>
|
||||
<ol>
|
||||
<li>352195</li>
|
||||
<li>1600306001288</li>
|
||||
</ol>
|
||||
</details>
|
2
2021/06/input.txt
Normal file
2
2021/06/input.txt
Normal file
@ -0,0 +1,2 @@
|
||||
4,1,1,1,5,1,3,1,5,3,4,3,3,1,3,3,1,5,3,2,4,4,3,4,1,4,2,2,1,3,5,1,1,3,2,5,1,1,4,2,5,4,3,2,5,3,3,4,5,4,3,5,4,2,5,5,2,2,2,3,5,5,4,2,1,1,5,1,4,3,2,2,1,2,1,5,3,3,3,5,1,5,4,2,2,2,1,4,2,5,2,3,3,2,3,4,4,1,4,4,3,1,1,1,1,1,4,4,5,4,2,5,1,5,4,4,5,2,3,5,4,1,4,5,2,1,1,2,5,4,5,5,1,1,1,1,1,4,5,3,1,3,4,3,3,1,5,4,2,1,4,4,4,1,1,3,1,3,5,3,1,4,5,3,5,1,1,2,2,4,4,1,4,1,3,1,1,3,1,3,3,5,4,2,1,1,2,1,2,3,3,5,4,1,1,2,1,2,5,3,1,5,4,3,1,5,2,3,4,4,3,1,1,1,2,1,1,2,1,5,4,2,2,1,4,3,1,1,1,1,3,1,5,2,4,1,3,2,3,4,3,4,2,1,2,1,2,4,2,1,5,2,2,5,5,1,1,2,3,1,1,1,3,5,1,3,5,1,3,3,2,4,5,5,3,1,4,1,5,2,4,5,5,5,2,4,2,2,5,2,4,1,3,2,1,1,4,4,1,5
|
||||
|
66
2021/06/python/main.py
Normal file
66
2021/06/python/main.py
Normal file
@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from os.path import join, dirname
|
||||
from typing import Dict, List
|
||||
|
||||
# Day 06
|
||||
|
||||
# Common
|
||||
|
||||
|
||||
def read_input(filename):
|
||||
data = join(dirname(__file__), '..', filename)
|
||||
with open(data) as f:
|
||||
return f.read().strip()
|
||||
|
||||
|
||||
test = read_input('test.txt')
|
||||
data = read_input('input.txt')
|
||||
|
||||
|
||||
class School:
|
||||
def __init__(self, fishes: List[int]) -> None:
|
||||
self.fishes = [0] * 9
|
||||
for fish in fishes:
|
||||
self.fishes[fish] += 1
|
||||
|
||||
@staticmethod
|
||||
def parse(raw: str):
|
||||
return School([int(x) for x in raw.split(',')])
|
||||
|
||||
def __len__(self) -> int:
|
||||
return sum(self.fishes)
|
||||
|
||||
def tick(self):
|
||||
born, *rest = self.fishes.copy()
|
||||
self.fishes = rest + [born]
|
||||
self.fishes[6] += born
|
||||
|
||||
def simulate(self, days: int):
|
||||
for _ in range(days):
|
||||
self.tick()
|
||||
|
||||
|
||||
# 1
|
||||
print('1.')
|
||||
|
||||
school = School.parse(test)
|
||||
school.simulate(18)
|
||||
print(len(school))
|
||||
school.simulate(80)
|
||||
print(f'Test: {len(school)}')
|
||||
|
||||
school = School.parse(data)
|
||||
school.simulate(80)
|
||||
print(f'Data: {len(school)}')
|
||||
|
||||
# 2
|
||||
print('\n2.')
|
||||
|
||||
school = School.parse(test)
|
||||
school.simulate(256)
|
||||
print(f'Test: {len(school)}')
|
||||
|
||||
school = School.parse(data)
|
||||
school.simulate(256)
|
||||
print(f'Data: {len(school)}')
|
1
2021/06/test.txt
Normal file
1
2021/06/test.txt
Normal file
@ -0,0 +1 @@
|
||||
3,4,3,1,2
|
Loading…
Reference in New Issue
Block a user