{ "cells": [ { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def enflip(items, flips, burnt=False, debug=False):\n", " if debug: i0 = items\n", " for flip in flips:\n", " if burnt:\n", " items = [-i for i in reversed(items[:flip])] + items[flip:]\n", " else:\n", " items = [i for i in reversed(items[:flip])] + items[flip:]\n", " if debug: print('{} -{}-> {}'.format(i0, flip, items))\n", " if debug: i0 = items\n", " return items" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "def burnt_sorted(pancakes):\n", " return all(p > 0 for p in pancakes) and pancakes == sorted(pancakes)\n", "\n", "def unburnt_sorted(pancakes):\n", " simple_pancakes = [abs(p) for p in pancakes]\n", " return simple_pancakes == sorted(simple_pancakes)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "flips_t = [l.strip().split(': ') for l in open('../../data/07-flips.txt')]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "stack = [[int(p) for p in l.split()] for c, l in flips_t if c == 'burgers'][0]\n", "flips = {int(c): [int(p) for p in l.split()] for c, l in flips_t if c != 'burgers'}" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "69" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(1 for f in flips.values()\n", " if unburnt_sorted(enflip(stack, f)))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "92" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[f for f in flips\n", " if burnt_sorted(enflip(stack, flips[f], burnt=True))][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }