2021 day 6

This commit is contained in:
a 2021-12-06 20:54:30 -06:00
parent 9b8b223909
commit 5f27175083
4 changed files with 53 additions and 0 deletions

30
2021/06/06.py Normal file
View file

@ -0,0 +1,30 @@
with open("sample.txt") as f:
fish = [int(n) for n in f.read().split(',')]
def advance(fish):
tomorrow = []
spawn = 0
for f in fish:
if f == 0:
tomorrow.append(6)
spawn += 1
else:
tomorrow.append(f-1)
for i in range(spawn):
tomorrow.append(8)
return tomorrow
def part1(fish):
for i in range(80):
fish = advance(fish)
return len(fish)
print(part1(fish))
## TOO LONG
# def part2(fish):
# for i in range(256):
# fish = advance(fish)
# return len(fish)
# print(part2(fish))

21
2021/06/06_refactored.py Normal file
View file

@ -0,0 +1,21 @@
with open("sample.txt") as f:
fish = [int(n) for n in f.read().split(',')]
# note that we don't actually care about the timers themselves,
# we just care about the total count after x days.
#
# technically we also care about the timer values,
# since this directly influences the total count per daily cycle.
from collections import Counter
def advance(counter, days):
for day in range(days):
counter[(day+7)%9] += counter[day%9] # every 7 days, a new fish spawns taking 9 days.
return sum(counter.values())
c = Counter(fish)
part1 = advance(c,80)
c = Counter(fish)
part2 = advance(c,256)
print(part1, part2)

1
2021/06/input.txt Normal file
View file

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

1
2021/06/sample.txt Normal file
View file

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