{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%load_ext line_profiler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from wordle_game import wordle, solver, helper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cProfile, pstats\n",
    "from multiprocessing import Pool\n",
    "from functools import partial\n",
    "\n",
    "import numpy as np\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "wordlist = wordle.WordList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('aback', 'aahed')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wordlist.answers[0], wordlist.acceptable_guesses[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 0, 0, 0])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wordle.generate_state(wordlist.answers[0], wordlist.acceptable_guesses[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "helper.generate_state(wordlist.answers[0], wordlist.acceptable_guesses[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Profiling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.73 µs ± 151 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit wordle.generate_state(wordlist.answers[0], wordlist.acceptable_guesses[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "599 ns ± 1.23 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit helper.generate_state(wordlist.answers[0], wordlist.acceptable_guesses[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "%lprun -f wordle.generate_state wordle.generate_state(wordlist.answers[0], wordlist.acceptable_guesses[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def profiler():\n",
    "    for i in range(100000):\n",
    "        helper.generate_state(wordlist.answers[0], wordlist.acceptable_guesses[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "cProfile.runctx(\"profiler()\", globals(), locals(), \"Profile.prof\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mon Jan 31 21:11:30 2022    Profile.prof\n",
      "\n",
      "         1400004 function calls in 0.304 seconds\n",
      "\n",
      "   Ordered by: cumulative time\n",
      "\n",
      "   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n",
      "        1    0.000    0.000    0.304    0.304 {built-in method builtins.exec}\n",
      "        1    0.000    0.000    0.304    0.304 <string>:1(<module>)\n",
      "        1    0.024    0.024    0.304    0.304 858601308.py:1(profiler)\n",
      "   100000    0.020    0.000    0.279    0.000 {built-in method wordle_game.helper.generate_state}\n",
      "   100000    0.017    0.000    0.259    0.000 helper.pyx:35(generate_state (wrapper))\n",
      "   100000    0.179    0.000    0.242    0.000 helper.pyx:35(generate_state)\n",
      "  1100000    0.063    0.000    0.063    0.000 helper.pyx:32(map_char)\n",
      "        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<pstats.Stats at 0x7f939dd424f0>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pstats.Stats(\"Profile.prof\")\n",
    "s.strip_dirs().sort_stats(\"cumtime\").print_stats()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "game = wordle.WordleGame(target='perky')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "solve = solver.MaxEntropy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[40m\u001b[37mS\u001b[0m\u001b[40m\u001b[37mO\u001b[0m\u001b[40m\u001b[37mA\u001b[0m\u001b[43m\u001b[37mR\u001b[0m\u001b[43m\u001b[37mE\u001b[0m\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'direr'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game.add_state(\"soare\",\"BBBYY\")\n",
    "print(game)\n",
    "solve.guess(game)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "%lprun -f solve.guess solve.guess(game)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[40m\u001b[37mS\u001b[0m\u001b[40m\u001b[37mO\u001b[0m\u001b[40m\u001b[37mA\u001b[0m\u001b[43m\u001b[37mR\u001b[0m\u001b[43m\u001b[37mE\u001b[0m\n",
      "\u001b[40m\u001b[37mT\u001b[0m\u001b[40m\u001b[37mH\u001b[0m\u001b[42m\u001b[37mR\u001b[0m\u001b[43m\u001b[37mE\u001b[0m\u001b[40m\u001b[37mW\u001b[0m\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'dampy'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game.add_state(\"threw\",\"BBGYB\")\n",
    "print(game)\n",
    "solve.guess(game)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[40m\u001b[37mS\u001b[0m\u001b[40m\u001b[37mO\u001b[0m\u001b[40m\u001b[37mA\u001b[0m\u001b[43m\u001b[37mR\u001b[0m\u001b[43m\u001b[37mE\u001b[0m\n",
      "\u001b[40m\u001b[37mT\u001b[0m\u001b[40m\u001b[37mH\u001b[0m\u001b[42m\u001b[37mR\u001b[0m\u001b[43m\u001b[37mE\u001b[0m\u001b[40m\u001b[37mW\u001b[0m\n",
      "\u001b[40m\u001b[37mN\u001b[0m\u001b[42m\u001b[37mE\u001b[0m\u001b[42m\u001b[37mR\u001b[0m\u001b[40m\u001b[37mD\u001b[0m\u001b[42m\u001b[37mY\u001b[0m\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'abamp'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game.add_state(\"nerdy\",\"BGGBG\")\n",
    "print(game)\n",
    "solve.guess(game)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[40m\u001b[37mS\u001b[0m\u001b[40m\u001b[37mO\u001b[0m\u001b[40m\u001b[37mA\u001b[0m\u001b[43m\u001b[37mR\u001b[0m\u001b[43m\u001b[37mE\u001b[0m\n",
      "\u001b[40m\u001b[37mT\u001b[0m\u001b[40m\u001b[37mH\u001b[0m\u001b[42m\u001b[37mR\u001b[0m\u001b[43m\u001b[37mE\u001b[0m\u001b[40m\u001b[37mW\u001b[0m\n",
      "\u001b[40m\u001b[37mN\u001b[0m\u001b[42m\u001b[37mE\u001b[0m\u001b[42m\u001b[37mR\u001b[0m\u001b[40m\u001b[37mD\u001b[0m\u001b[42m\u001b[37mY\u001b[0m\n",
      "\u001b[40m\u001b[37mM\u001b[0m\u001b[42m\u001b[37mE\u001b[0m\u001b[42m\u001b[37mR\u001b[0m\u001b[40m\u001b[37mC\u001b[0m\u001b[42m\u001b[37mY\u001b[0m\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'jerky'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game.add_state(\"mercy\",\"BGGBG\")\n",
    "print(game)\n",
    "solve.guess(game)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jerky', 'perky']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solver.filter_answers(game)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simulate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 2315/2315 [04:28<00:00,  8.61it/s]\n"
     ]
    }
   ],
   "source": [
    "solutions = solve.simulate()\n",
    "np.save('../data/maxentropy_solutions.npy', solutions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "solutions = np.load('../data/maxentropy_solutions.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  -1   12]\n",
      " [   2   31]\n",
      " [   3 1085]\n",
      " [   4 1104]\n",
      " [   5   82]\n",
      " [   6    1]]\n"
     ]
    }
   ],
   "source": [
    "unique, counts = np.unique(solutions, return_counts=True)\n",
    "print(np.asarray((unique, counts)).T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.538428137212332"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Average number of guesses\n",
    "np.sum(unique[1:]*counts[1:])/counts[1:].sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "cpu = os.cpu_count() - 1\n",
    "cpu = cpu if cpu > 0 else cpu\n",
    "\n",
    "\n",
    "results = []\n",
    "\n",
    "for target in tqdm(wordlist.answers):\n",
    "    with Pool(os.cpu_count() - 1) as pool:\n",
    "        results.extend(pool.map(partial(wordle_game.helper.generate_state, target), wordlist.acceptable_guesses))\n",
    "        pool.close()\n",
    "        pool.join()\n",
    "        \n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
