{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def counting_sheep(n):\n",
    "    not_seen = [0,1,2,3,4,5,6,7,8,9]\n",
    "    N = n\n",
    "    \n",
    "    if n==0:\n",
    "        return \"INSOMNIA\"\n",
    "    \n",
    "    while True:\n",
    "        digits = list(map(int, str(N)))\n",
    "        \n",
    "        not_seen = [x for x in not_seen if x not in digits]\n",
    "            \n",
    "        if len(not_seen) == 0:\n",
    "            break\n",
    "        else:\n",
    "            N += n\n",
    "            #if N > max_N:\n",
    "            #    return \"INSOMNIA\"\n",
    "    return N"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lines = open('./A-small-attempt2.in').read().splitlines()\n",
    "results = []\n",
    "for line in lines[1:]:\n",
    "    n = int(line)\n",
    "    results.append(counting_sheep(n))\n",
    "\n",
    "fout = open('./out.txt',\"w\")\n",
    "for i in range(len(results)):\n",
    "    fout.write(\"Case #%s: %s \\n\" % (str(i+1),str(results[i])))\n",
    "fout.close()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
