diff --git a/2021/06/06.py b/2021/06/06.py new file mode 100644 index 0000000..f3cc860 --- /dev/null +++ b/2021/06/06.py @@ -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)) \ No newline at end of file diff --git a/2021/06/06_refactored.py b/2021/06/06_refactored.py new file mode 100644 index 0000000..b92c53c --- /dev/null +++ b/2021/06/06_refactored.py @@ -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) \ No newline at end of file diff --git a/2021/06/input.txt b/2021/06/input.txt new file mode 100644 index 0000000..8a11000 --- /dev/null +++ b/2021/06/input.txt @@ -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 \ No newline at end of file diff --git a/2021/06/sample.txt b/2021/06/sample.txt new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/2021/06/sample.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file