{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def simpler(P):\n",
    "    prev = None\n",
    "    for element in P:\n",
    "        if prev != element:\n",
    "            yield element\n",
    "        prev = element\n",
    "def simple(P):\n",
    "    return \"\".join(list(simpler(P)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "memo = {\"+\":0, \"-\":1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def pancake(P):\n",
    "    p = simple(P)\n",
    "    if p in memo:\n",
    "        return memo[p]\n",
    "    else:\n",
    "        p = simpler(manuver(p))\n",
    "        memo[p] = pancake(p) + 1\n",
    "        return memo[p]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'-+-+-'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simple(\"--+--++-\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def manuver(P):\n",
    "    if P[0]==\"-\":\n",
    "        return \"+\" + P[1:]\n",
    "    else:\n",
    "        return \"-+\" + P[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'++-'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pancake()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'+': 0, '++': 0, '++-': 0, '-': 1, '-+': 0}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "memo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Case #1: 1\n",
      "\n",
      "Case #2: 1\n",
      "\n",
      "Case #3: 2\n",
      "\n",
      "Case #4: 0\n",
      "\n",
      "Case #5: 3\n",
      "\n"
     ]
    }
   ],
   "source": [
    "file_in='pancake_0.in'\n",
    "lines = open(file_in, \"r\").readlines()\n",
    "out = open(\"pancake_0.out\", \"w\")\n",
    "for x, y in enumerate(lines):\n",
    "    if x==0:\n",
    "        continue\n",
    "    out.write(\"Case #%s: %s\\n\" % (x,pancake(y.strip())))\n",
    "    print(\"Case #%s: %s\\n\" % (x,pancake(y.strip())))\n",
    "out.close()"
   ]
  }
 ],
 "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.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
