{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "Counting Sheep"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%latex\n",
    "Counting Sheep\n"
   ]
  },
  {
   "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": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lines = open('./A-large.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_large.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": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "Revenge of the Pancakes"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%latex\n",
    "Revenge of the Pancakes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "def pancakes(stack):\n",
    "    stack = list(stack)\n",
    "    count = 0\n",
    "    for i in range(len(stack)-1,-1,-1):\n",
    "        if stack[i] == \"+\":\n",
    "            continue\n",
    "        else:\n",
    "            \"\"\"\n",
    "            if stack[0] == \"+\" and i != 0:\n",
    "                stack[0] = \"-\"\n",
    "                count += 1\n",
    "            \"\"\"\n",
    "            if stack[0] == \"+\" and i != 0:\n",
    "                for j in range(i-1,-1,-1):\n",
    "                    if stack[j] == \"+\":\n",
    "                        window = ['-' if x == '+' else '+' for x in reversed(stack[0:j+1])]\n",
    "                        for x in range(len(window)):\n",
    "                            stack[x] = window[x]\n",
    "                        count += 1\n",
    "                        break\n",
    "            \n",
    "            window = ['-' if x == '+' else '+' for x in reversed(stack[0:i+1])]\n",
    "            for x in range(len(window)):\n",
    "                stack[x] = window[x]\n",
    "            count += 1\n",
    "    \n",
    "    return count\n",
    "    \n",
    "print(pancakes(\"+---+--\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "2\n",
      "0\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "print(pancakes(\"-\"))\n",
    "print(pancakes(\"-+\"))\n",
    "print(pancakes(\"+-\"))\n",
    "print(pancakes(\"+++\"))\n",
    "print(pancakes(\"--+-\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lines = open('./B-small-attempt0.in').read().splitlines()\n",
    "results = []\n",
    "for line in lines[1:]:\n",
    "    results.append(pancakes(line.strip()))\n",
    "\n",
    "fout = open('./out_B_small.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()"
   ]
  },
  {
   "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
}
