--- /dev/null
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "import random"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['^', '^', '^', 'v', 'v', 'v', '=']"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "instructions = list('^^^vvv=')\n",
+ "instructions"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 59,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def build_sequence(instructions, length=100):\n",
+ " non_exits = [i for i in instructions if i != '=']\n",
+ " seq = [random.choice(non_exits)]\n",
+ " last_was_exit = False\n",
+ " for _ in range(length-2):\n",
+ " if last_was_exit:\n",
+ " seq.append(random.choice(non_exits))\n",
+ " last_was_exit = False\n",
+ " else:\n",
+ " i = random.choice(instructions)\n",
+ " last_was_exit = (i == '=')\n",
+ " seq.append(i)\n",
+ " seq.append(instructions[-1])\n",
+ " return ''.join(seq)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 62,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'v=^^^vv^=='"
+ ]
+ },
+ "execution_count": 62,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "build_sequence(instructions, 10)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 66,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'vvv^v^^^^v^v=^vvv=v^vvv^=v^^v=v^^^^vv^=v^vvv^v^^vv^^vvvvv^^vv^v=^v=v^^^vvv^v^vv=v^vv^^=^vv^^vvvv=^^='"
+ ]
+ },
+ "execution_count": 66,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "seq = build_sequence(instructions)\n",
+ "seq"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def value(instr):\n",
+ " if instr == '^':\n",
+ " return 1\n",
+ " elif instr == 'v':\n",
+ " return -1\n",
+ " else:\n",
+ " return 0 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def final(sequence):\n",
+ " return sum(value(i) for i in sequence)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "-1"
+ ]
+ },
+ "execution_count": 22,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "final(seq)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def running(sequence):\n",
+ " current = 0\n",
+ " currents = []\n",
+ " exits = []\n",
+ " for i in sequence:\n",
+ " if value(i) == 0:\n",
+ " exits.append(current)\n",
+ " else:\n",
+ " current += value(i)\n",
+ " currents.append(current)\n",
+ " return currents, exits"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "([1,\n",
+ " 2,\n",
+ " 3,\n",
+ " 2,\n",
+ " 3,\n",
+ " 4,\n",
+ " 5,\n",
+ " 4,\n",
+ " 5,\n",
+ " 4,\n",
+ " 5,\n",
+ " 4,\n",
+ " 3,\n",
+ " 4,\n",
+ " 3,\n",
+ " 2,\n",
+ " 3,\n",
+ " 4,\n",
+ " 3,\n",
+ " 2,\n",
+ " 3,\n",
+ " 4,\n",
+ " 5,\n",
+ " 6,\n",
+ " 5,\n",
+ " 4,\n",
+ " 3,\n",
+ " 4,\n",
+ " 3,\n",
+ " 2,\n",
+ " 1,\n",
+ " 2,\n",
+ " 3,\n",
+ " 4,\n",
+ " 3,\n",
+ " 4,\n",
+ " 3,\n",
+ " 2,\n",
+ " 1,\n",
+ " 2,\n",
+ " 1,\n",
+ " 2,\n",
+ " 3,\n",
+ " 2,\n",
+ " 3,\n",
+ " 4,\n",
+ " 3,\n",
+ " 2,\n",
+ " 1,\n",
+ " 0,\n",
+ " -1,\n",
+ " -2,\n",
+ " -3,\n",
+ " -4,\n",
+ " -3,\n",
+ " -2,\n",
+ " -1,\n",
+ " -2,\n",
+ " -3,\n",
+ " -4,\n",
+ " -3,\n",
+ " -2,\n",
+ " -1,\n",
+ " -2,\n",
+ " -3,\n",
+ " -2,\n",
+ " -3,\n",
+ " -4,\n",
+ " -5,\n",
+ " -4,\n",
+ " -3,\n",
+ " -4,\n",
+ " -5,\n",
+ " -4,\n",
+ " -5,\n",
+ " -4,\n",
+ " -3,\n",
+ " -2,\n",
+ " -1,\n",
+ " 0,\n",
+ " -1,\n",
+ " 0,\n",
+ " -1,\n",
+ " -2,\n",
+ " -3,\n",
+ " -2,\n",
+ " -1,\n",
+ " 0,\n",
+ " -1],\n",
+ " [5, 4, 2, 1, -2, -2, -3, -5, -5, -1, -1])"
+ ]
+ },
+ "execution_count": 24,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "running(seq)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "^vvv^^v^=vv=v^v=^vvvv^=^vv^vvv^=v=v^^v=^^v=v^=v^^vv^^==v^^v^v=vvv^^^^=vvv^^v=v=vv^^^^v^v=vv^v^=^^^^v=v=v^^^vv^vv^v^^vvv^v==^vv=^vv^vv^^vv=^=v=vv^vv^=v^^^^vvvvv=vv^^^v=^=^=vv^v=v^^^^vv=^^^^vv^^v=vv^^v^^v^vv^^==^v^=^^^^v=^v^v^v=vv^=vvv^v=^v^^^^vv^v^v^^v^v^v^^^vv^^v^^v=v^v^v^v^^v^vvvv^=^v=^^vvvv^^vv^vv^v=^v=^vvvvv^vv=vv^=v^=^^^^^=^vvvvv^=^v^=^^^v^^v^=v^^^vv^v^^vv^=^v^^^vvv=^v^^v^v^^vvvvv^vvv=^v=^^vv^^v^^vvvv=v^=v^vvvvvvv=^==^vv=v=vv=^^^^=vv^vvv^^v^vv^^^v^^v=v^^=^^^^vv^v==v^v^^v^^v^=^^^==v^^^vv^==vvvvvvv^=vv^=vv^^=v^^vvv^^^vvv^^vv=v=^^vvv^^^^v=^^vv^v^^=^v=v=^vv^^vvv^^^^vv^=v^^v^^v^v=v^^^^vvv^^=vv^=vv^^v^v^^v=v^^vv^=^v^^^=v^^v=v^vv^=^v^^v=vv^=^v^^^v^=v==v^vvvvv^vvvv^^v=^^vv^^v^^^^^=^v=^v^=^vv^v=^^^vvvv^^vv^^==^==^^^^v=v^vvvv=^v^=^vv^^v^^vvvvv=^v^^==^=^^v==v=vv^vv^^vvv=^vvvv=v=^vv=^v^^vv==vvv^^^=vv^^^vv=v=^vvv^^v^v=v^^v=^=v^vvvvvvvv===^=^vvv^vv^^vvvv^=^v^^^=^^^vvvvv^==v^^v=vvv^v=vv^vv^vv=vvv^vv^=^^^v^vv^vvv=v^vv^v^vvvvvv^^v^^^^vv^v=^=^v=v^^vvv^vv^^=^v^vv^v^=v=v^v^vvv=^^=^vv^vvv==^^v^^vvv^v^=\n",
+ "Final: -48\n",
+ "Highest 1, lowest -49, final -48.\n",
+ "Highest exit 0, lowest exit -49.\n"
+ ]
+ }
+ ],
+ "source": [
+ "seq = build_sequence(instructions, 1000)\n",
+ "print(seq)\n",
+ "print('Final: ', final(seq))\n",
+ "fs, es = running(seq)\n",
+ "print('Highest {}, lowest {}, final {}.'.format(max(fs), min(fs), fs[-1]))\n",
+ "print('Highest exit {}, lowest exit {}.'.format(max(es), min(es)))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 67,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Highest 216, lowest -6, final 209.\n",
+ "Highest exit 215, lowest exit -5.\n"
+ ]
+ }
+ ],
+ "source": [
+ "valid_seq = False\n",
+ "while not valid_seq:\n",
+ " seq = build_sequence(instructions, 10000)\n",
+ " fs, es = running(seq)\n",
+ " if min(fs) > -10 and fs[-1] > 0:\n",
+ " valid_seq = True\n",
+ "\n",
+ "open('02-lifts.txt', 'w').write(seq)\n",
+ "print('Highest {}, lowest {}, final {}.'.format(max(fs), min(fs), fs[-1]))\n",
+ "print('Highest exit {}, lowest exit {}.'.format(max(es), min(es)))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 80,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "('v^^^^^v=v=', ([-1, 0, 1, 2, 3, 4, 3, 2], [3, 2]))"
+ ]
+ },
+ "execution_count": 80,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "small_seq = build_sequence(instructions, 10)\n",
+ "small_seq, running(small_seq)"
+ ]
+ },
+ {
+ "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.6.1"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
--- /dev/null
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Puzzle 2: Lifts\n",
+ "\n",
+ "## Part 1: using the lifts\n",
+ "\n",
+ "You've chosen your holiday, now it's time to catch your flight. You arrive at Heathwick airport and have to find the departures floor. However, getting around Terminal $\\pi$ is a real pain. The instructions for how to use the lifts are rather complex.\n",
+ "\n",
+ "You start in the car park (the basement level, floor 0) and follow this instructions one step at a time. The instructions are each one character:\n",
+ "* `^` : go up one floor\n",
+ "* `v` : go down one floor\n",
+ "* `=` : open the doors, without moving floors.\n",
+ "\n",
+ "The input contains no other characters.\n",
+ "\n",
+ "If you follow the instructions and get out at the end, what floor do you end up on?\n",
+ "\n",
+ "Terminal $\\pi$ is large and there's no limit to the number of floors, both up and down.\n",
+ "\n",
+ "For instance, the sequence '^=' takes you to floor 1. \n",
+ "\n",
+ "The sequence `vv^=` takes you down two floors to the sub basement (floor -2), then up one floor, and you get out in the sub-basement on floor 0.\n",
+ "\n",
+ "The sequence `^^v=^=` would start on floor 0, go up two floors, then down one. The doors would then open on floor 1, but you'd stay in the lift. You'd then move up to floor 2, the doors open, and you get out on floor 2. \n",
+ "\n",
+ "The sequence `v^^^^^v=v=` would go down one floor (to floor -1), up five floors (to floor 4), down one floor (to floor 3), open the doors, go down another floor, and you'd finally get out at floor 2.\n",
+ "\n",
+ "Given the input in [02-lifts.txt](02-lifts.txt), where would you get out?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def value(instr):\n",
+ " if instr == '^':\n",
+ " return 1\n",
+ " elif instr == 'v':\n",
+ " return -1\n",
+ " else:\n",
+ " return 0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def final(sequence):\n",
+ " current = 0\n",
+ " for c in sequence:\n",
+ " current += value(c)\n",
+ " return current"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "209"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "with open('02-lifts.txt') as f:\n",
+ " instructions = f.read()\n",
+ " exit = final(instructions)\n",
+ "exit"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Smart-alec one line solution"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "209"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "sum(value(i) for i in open('02-lifts.txt').read())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Part 2: getting out\n",
+ "You can only leave the lift where the doors are open. What is the highest floor you could leave from?\n",
+ "\n",
+ "For instance, the sequence `^^v=^=` would allow you to exit on floors 1 and 2, so the highest floor you could leave from would be floor 2. \n",
+ "\n",
+ "The sequence `v^^^^^v=v=` would allow you to exit on floors 3 and 2, so the highest floor you could leave from would be floor 3 (even though the lift reaches floor 4)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def exits(sequence):\n",
+ " current = 0\n",
+ " exits = []\n",
+ " for i in sequence:\n",
+ " if value(i) == 0:\n",
+ " exits.append(current)\n",
+ " else:\n",
+ " current += value(i)\n",
+ " return exitsto"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "215"
+ ]
+ },
+ "execution_count": 13,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "max(exits(instructions))"
+ ]
+ },
+ {
+ "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.6.1"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}