From 74a9f60b122ffcccfa21ffc461abd4dbc8f7a566 Mon Sep 17 00:00:00 2001 From: Roland Thomas Date: Tue, 3 Oct 2023 14:19:08 -0400 Subject: [PATCH] Add 2021/day6 --- 2021/day6 | 1 + 2021/day6.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2021/day6.sample | 1 + 3 files changed, 43 insertions(+) create mode 100644 2021/day6 create mode 100755 2021/day6.py create mode 100644 2021/day6.sample diff --git a/2021/day6 b/2021/day6 new file mode 100644 index 0000000..66ede3c --- /dev/null +++ b/2021/day6 @@ -0,0 +1 @@ +3,5,2,5,4,3,2,2,3,5,2,3,2,2,2,2,3,5,3,5,5,2,2,3,4,2,3,5,5,3,3,5,2,4,5,4,3,5,3,2,5,4,1,1,1,5,1,4,1,4,3,5,2,3,2,2,2,5,2,1,2,2,2,2,3,4,5,2,5,4,1,3,1,5,5,5,3,5,3,1,5,4,2,5,3,3,5,5,5,3,2,2,1,1,3,2,1,2,2,4,3,4,1,3,4,1,2,2,4,1,3,1,4,3,3,1,2,3,1,3,4,1,1,2,5,1,2,1,2,4,1,3,2,1,1,2,4,3,5,1,3,2,1,3,2,3,4,5,5,4,1,3,4,1,2,3,5,2,3,5,2,1,1,5,5,4,4,4,5,3,3,2,5,4,4,1,5,1,5,5,5,2,2,1,2,4,5,1,2,1,4,5,4,2,4,3,2,5,2,2,1,4,3,5,4,2,1,1,5,1,4,5,1,2,5,5,1,4,1,1,4,5,2,5,3,1,4,5,2,1,3,1,3,3,5,5,1,4,1,3,2,2,3,5,4,3,2,5,1,1,1,2,2,5,3,4,2,1,3,2,5,3,2,2,3,5,2,1,4,5,4,4,5,5,3,3,5,4,5,5,4,3,5,3,5,3,1,3,2,2,1,4,4,5,2,2,4,2,1,4 diff --git a/2021/day6.py b/2021/day6.py new file mode 100755 index 0000000..50c8333 --- /dev/null +++ b/2021/day6.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +from sys import argv +from collections import Counter, deque + +def attempt_one(line, iterations): + fishes = [int(fish) for fish in line if fish.isdigit()] + + for days in range(iterations): + number_zeroes = fishes.count(0) + fishes = [fish if fish else 7 for fish in fishes] + if number_zeroes: + fishes.extend(number_zeroes * [9]) + fishes = [fish-1 for fish in fishes] + return len(fishes) + + +def lanternfish(line, iterations): + fishes = Counter([int(fish) for fish in line if fish.isdigit()]) + fishes_list = [0] * 9 + for fish in fishes: + fishes_list[fish] = fishes[fish] + + fishes_que = deque(fishes_list) + + for days in range(iterations): + number_zeroes = fishes_que.popleft() + fishes_que.append(number_zeroes) + fishes_que[6] += number_zeroes + return sum(fishes_que) + + + +def main(): + with open("day6", "r") as file: + line = file.readline() + + print(lanternfish(line, 80)) + print(lanternfish(line, 256)) + +if __name__ == "__main__": + main() diff --git a/2021/day6.sample b/2021/day6.sample new file mode 100644 index 0000000..55129f1 --- /dev/null +++ b/2021/day6.sample @@ -0,0 +1 @@ +3,4,3,1,2