{ "cells": [ { "cell_type": "code", "execution_count": 34, "id": "charged-commonwealth", "metadata": {}, "outputs": [], "source": [ "import statistics, math\n", "\n", "def fuel(positions, rendezvous, consumption):\n", " return sum(map(lambda x: consumption(abs(x-rendezvous)),\n", " positions))\n", "\n", "def least_fuel(positions, rendezvous, consumption):\n", " r = math.floor(rendezvous(positions)) \n", " return fuel(positions, r, consumption)" ] }, { "cell_type": "code", "execution_count": 35, "id": "divine-andorra", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "352254" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import import_ipynb\n", "import helper\n", "f = helper.read_file('2021_7.txt')\n", "\n", "positions = list(map(int, f.rstrip().split(',')))\n", "least_fuel(positions, \n", " statistics.median, \n", " consumption=lambda x: x)" ] }, { "cell_type": "code", "execution_count": 36, "id": "entertaining-wichita", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "99053143.0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "least_fuel(positions,\n", " statistics.mean, \n", " consumption=lambda x: x*(x+1)/2)" ] } ], "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": "The Treachery of Whales" }, "nbformat": 4, "nbformat_minor": 5 }