{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bleatrix Trotter the sheep has devised a strategy that helps her fall asleep faster. First, she picks a number N. Then she starts naming N, 2 × N, 3 × N, and so on. Whenever she names a number, she thinks about all of the digits in that number. She keeps track of which digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) she has seen at least once so far as part of any number she has named. Once she has seen each of the ten digits at least once, she will fall asleep.\n",
    "\n",
    "Bleatrix must start with N and must always name (i + 1) × N directly after i × N. For example, suppose that Bleatrix picks N = 1692. She would count as follows:\n",
    "\n",
    "N = 1692. Now she has seen the digits 1, 2, 6, and 9.\n",
    "2N = 3384. Now she has seen the digits 1, 2, 3, 4, 6, 8, and 9.\n",
    "3N = 5076. Now she has seen all ten digits, and falls asleep.\n",
    "What is the last number that she will name before falling asleep? If she will count forever, print INSOMNIA instead.\n",
    "\n",
    "Input\n",
    "\n",
    "The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with a single integer N, the number Bleatrix has chosen.\n",
    "\n",
    "Output\n",
    "\n",
    "For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the last number that Bleatrix will name before falling asleep, according to the rules described in the statement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "## Test Data Set\n",
    "s = '5\\n0\\n1\\n2\\n11\\n1692'\n",
    "\n",
    "with open('test_sleep.txt','w') as out:\n",
    "    out.write(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sleep_set = set(['0','1','2','3','4','5','6','7','8','9'])\n",
    "\n",
    "class counting_numbers():\n",
    "    def __init__(self, N):\n",
    "        self.N = N\n",
    "        self.increment = 1\n",
    "        self.seen = set()\n",
    "        self.check_insomnia()\n",
    "        \n",
    "    def check_insomnia(self):\n",
    "        if self.N == 0:\n",
    "            raise(Exception)\n",
    "            \n",
    "    def increment_number(self):\n",
    "        x = self.N*self.increment\n",
    "        self.inspect_number(x)\n",
    "        self.increment+=1\n",
    "        \n",
    "    def inspect_number(self, x):\n",
    "        for i in str(x):\n",
    "            self.seen.add(i)\n",
    "        self._check_set()\n",
    "        \n",
    "    def _check_set(self):\n",
    "        if len(self.seen.intersection(sleep_set)) >= 10:\n",
    "            raise(Exception)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "last_number = []\n",
    "with open('A-large.in') as fid:\n",
    "    n = fid.readline()\n",
    "    for N in fid.readlines():\n",
    "        try:\n",
    "            c = counting_numbers(int(N))\n",
    "            while True:\n",
    "                c.increment_number()\n",
    "        except:\n",
    "            if int(N) == 0:\n",
    "                last_number.append('INSOMNIA')\n",
    "            else:\n",
    "                last_number.append(c.increment*c.N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Case #1: INSOMNIA\n",
      "Case #2: 10\n",
      "Case #3: 90\n",
      "Case #4: 110\n",
      "Case #5: 5076\n",
      "Case #6: 1651640\n",
      "Case #7: 900\n",
      "Case #8: 96\n",
      "Case #9: 2361552\n",
      "Case #10: 1064790\n",
      "Case #11: 3904588\n",
      "Case #12: 5478\n",
      "Case #13: 1054396\n",
      "Case #14: 4646750\n",
      "Case #15: 274436\n",
      "Case #16: 2822696\n",
      "Case #17: 2143518\n",
      "Case #18: 619885\n",
      "Case #19: 1905380\n",
      "Case #20: 971300\n",
      "Case #21: 9999910\n",
      "Case #22: 2577384\n",
      "Case #23: 3310923\n",
      "Case #24: 2531620\n",
      "Case #25: 1182759\n",
      "Case #26: 90\n",
      "Case #27: 1249611\n",
      "Case #28: 3878460\n",
      "Case #29: 2307320\n",
      "Case #30: 3589275\n",
      "Case #31: 918\n",
      "Case #32: 9999930\n",
      "Case #33: 1402725\n",
      "Case #34: 2743510\n",
      "Case #35: 900\n",
      "Case #36: 1146260\n",
      "Case #37: 3234944\n",
      "Case #38: 2356\n",
      "Case #39: 3196085\n",
      "Case #40: 6069819\n",
      "Case #41: 7999984\n",
      "Case #42: 90\n",
      "Case #43: 700952\n",
      "Case #44: 3005455\n",
      "Case #45: 2897420\n",
      "Case #46: 9000000\n",
      "Case #47: 406077\n",
      "Case #48: 5999952\n",
      "Case #49: 90\n",
      "Case #50: 9999970\n",
      "Case #51: 6999965\n",
      "Case #52: 4670346\n",
      "Case #53: 3249990\n",
      "Case #54: 2032929\n",
      "Case #55: 9999990\n",
      "Case #56: 154900\n",
      "Case #57: 3169616\n",
      "Case #58: 829072\n",
      "Case #59: 5999976\n",
      "Case #60: 726025\n",
      "Case #61: 1692114\n",
      "Case #62: 743095\n",
      "Case #63: 2927070\n",
      "Case #64: 1231310\n",
      "Case #65: 2827860\n",
      "Case #66: 3221792\n",
      "Case #67: 1252435\n",
      "Case #68: 903572\n",
      "Case #69: 920\n",
      "Case #70: 213696\n",
      "Case #71: 1558920\n",
      "Case #72: 2459590\n",
      "Case #73: 2015439\n",
      "Case #74: 9000\n",
      "Case #75: 9000\n",
      "Case #76: 3456560\n",
      "Case #77: 1249500\n",
      "Case #78: 92\n",
      "Case #79: 3145660\n",
      "Case #80: 3923210\n",
      "Case #81: 6212101\n",
      "Case #82: 195687\n",
      "Case #83: 30\n",
      "Case #84: 70\n",
      "Case #85: 1078004\n",
      "Case #86: 97090\n",
      "Case #87: 5999964\n",
      "Case #88: 5767872\n",
      "Case #89: 3670900\n",
      "Case #90: 476119\n",
      "Case #91: 2208692\n",
      "Case #92: 1746759\n",
      "Case #93: 4054870\n",
      "Case #94: 900000\n",
      "Case #95: 3893925\n",
      "Case #96: 90000\n",
      "Case #97: 1744256\n",
      "Case #98: 90\n",
      "Case #99: 606970\n",
      "Case #100: 9000000\n"
     ]
    }
   ],
   "source": [
    "with open('sleep_output.txt','w') as out:\n",
    "    for i, j in enumerate(last_number):\n",
    "        print 'Case #{}: {}'.format(i+1, j)\n",
    "        out.write('Case #{}: {}\\n'.format(i+1, j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "las"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
