{ "cells": [ { "cell_type": "code", "execution_count": 67, "id": "instant-horizontal", "metadata": {}, "outputs": [], "source": [ "def next_generation(fs):\n", " fs = {k-1:v for k, v in fs.items()}\n", " fs[6] += fs[-1]\n", " fs[8] = fs[-1]\n", " fs[-1] = 0\n", " return fs\n", "\n", "def nth_generation(fs, n):\n", " for _ in range(n):\n", " fs = next_generation(fs)\n", " return fs\n", "\n", "def fish(l):\n", " fs = {i:0 for i in range(9)}\n", " for d in l:\n", " fs[d] = fs[d] + 1\n", " return fs" ] }, { "cell_type": "code", "execution_count": 71, "id": "ranking-consideration", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5934" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs = fish([3,4,3,1,2])\n", "fs = nth_generation(fs, 80)\n", "sum(fs.values())" ] }, { "cell_type": "code", "execution_count": 74, "id": "neural-print", "metadata": {}, "outputs": [], "source": [ "import import_ipynb\n", "import helper\n", "s = helper.read_file('2021_6.txt')" ] }, { "cell_type": "code", "execution_count": 75, "id": "searching-heading", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "372300" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs = map(int, s.rstrip().split(','))\n", "fs = fish(fs)\n", "fs = nth_generation(fs, 80)\n", "sum(fs.values())" ] }, { "cell_type": "code", "execution_count": 76, "id": "hazardous-congo", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1675781200288" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs = map(int, s.rstrip().split(','))\n", "fs = fish(fs)\n", "fs = nth_generation(fs, 256)\n", "sum(fs.values())" ] }, { "cell_type": "code", "execution_count": null, "id": "civilian-lancaster", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" }, "title": "Lanternfish" }, "nbformat": 4, "nbformat_minor": 5 }