{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3295539.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "dat = pd.read_csv(\"day1.txt\", squeeze = True, names = \"x\")\n",
    "fuel_needed = sum(np.floor(dat / 3) - 2)\n",
    "fuel_needed\n",
    "# It works with the sample input??\n",
    "# fuel_needed = np.floor(100756/3) - 2\n",
    "# fuel_needed\n",
    "# fuel_needed = np.floor(1969/3) - 2\n",
    "# fuel_needed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "to acct for was:\n",
      "1098511.0\n",
      "now total fuel needed is:\n",
      "4394050.0\n",
      "to acct for was:\n",
      "366168.0\n",
      "now total fuel needed is:\n",
      "4760218.0\n",
      "to acct for was:\n",
      "122054.0\n",
      "now total fuel needed is:\n",
      "4882272.0\n",
      "to acct for was:\n",
      "40682.0\n",
      "now total fuel needed is:\n",
      "4922954.0\n",
      "to acct for was:\n",
      "13558.0\n",
      "now total fuel needed is:\n",
      "4936512.0\n",
      "to acct for was:\n",
      "4517.0\n",
      "now total fuel needed is:\n",
      "4941029.0\n",
      "to acct for was:\n",
      "1503.0\n",
      "now total fuel needed is:\n",
      "4942532.0\n",
      "to acct for was:\n",
      "499.0\n",
      "now total fuel needed is:\n",
      "4943031.0\n",
      "to acct for was:\n",
      "164.0\n",
      "now total fuel needed is:\n",
      "4943195.0\n",
      "to acct for was:\n",
      "52.0\n",
      "now total fuel needed is:\n",
      "4943247.0\n",
      "to acct for was:\n",
      "15.0\n",
      "now total fuel needed is:\n",
      "4943262.0\n",
      "to acct for was:\n",
      "3.0\n",
      "now total fuel needed is:\n",
      "4943265.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "4940441.0"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "to_acct_for = np.floor(fuel_needed / 3) - 2\n",
    "while to_acct_for > 0:\n",
    "    print(\"to acct for was:\")\n",
    "    print(to_acct_for)\n",
    "    fuel_needed = fuel_needed + to_acct_for\n",
    "    print(\"now total fuel needed is:\")\n",
    "    print(fuel_needed)\n",
    "    to_acct_for = max(np.floor(to_acct_for / 3) - 2, 0)\n",
    "\n",
    "\n",
    "# fuel_needed\n",
    "# to_acct_for\n",
    "\n",
    "# too high: 4943265\n",
    "# too low:  3844754 - was double-shrinking the first one\n",
    "# too high: 4943262\n",
    "\n",
    "# Argh that last sentence in the instructions!  I bet it's a rounding subtotals error caused by that instruction.\n",
    "\n",
    "# Part 2\n",
    "\n",
    "def real_fuel (module_weight):\n",
    "    \"return the actual recursive fuel needed for a nominal weight\"\n",
    "    fuel_needed = np.floor(module_weight / 3) - 2\n",
    "    to_acct_for = np.floor(fuel_needed / 3) - 2\n",
    "    while to_acct_for > 0:\n",
    "        fuel_needed = fuel_needed + to_acct_for\n",
    "        to_acct_for = max(np.floor(to_acct_for / 3) - 2, 0)\n",
    "    return fuel_needed\n",
    "\n",
    "sum(dat.apply(real_fuel))\n",
    "\n",
    "# for(item in dat):\n",
    " #   real_fuel(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1098511.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
