Tweaked python task 7
[summerofcode2018soln.git] / src / task7 / task7.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 25,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "def enflip(items, flips, burnt=False, debug=False):\n",
10 " if debug: i0 = items\n",
11 " for flip in flips:\n",
12 " if burnt:\n",
13 " items = [-i for i in reversed(items[:flip])] + items[flip:]\n",
14 " else:\n",
15 " items = [i for i in reversed(items[:flip])] + items[flip:]\n",
16 " if debug: print('{} -{}-> {}'.format(i0, flip, items))\n",
17 " if debug: i0 = items\n",
18 " return items"
19 ]
20 },
21 {
22 "cell_type": "code",
23 "execution_count": 26,
24 "metadata": {},
25 "outputs": [],
26 "source": [
27 "def burnt_sorted(pancakes):\n",
28 " return all(p > 0 for p in pancakes) and pancakes == sorted(pancakes)\n",
29 "\n",
30 "def unburnt_sorted(pancakes):\n",
31 " simple_pancakes = [abs(p) for p in pancakes]\n",
32 " return simple_pancakes == sorted(simple_pancakes)"
33 ]
34 },
35 {
36 "cell_type": "code",
37 "execution_count": 27,
38 "metadata": {},
39 "outputs": [],
40 "source": [
41 "flips_t = [l.strip().split(': ') for l in open('../../data/07-flips.txt')]"
42 ]
43 },
44 {
45 "cell_type": "code",
46 "execution_count": 28,
47 "metadata": {},
48 "outputs": [],
49 "source": [
50 "stack = [[int(p) for p in l.split()] for c, l in flips_t if c == 'burgers'][0]\n",
51 "flips = {int(c): [int(p) for p in l.split()] for c, l in flips_t if c != 'burgers'}"
52 ]
53 },
54 {
55 "cell_type": "code",
56 "execution_count": 29,
57 "metadata": {},
58 "outputs": [
59 {
60 "data": {
61 "text/plain": [
62 "69"
63 ]
64 },
65 "execution_count": 29,
66 "metadata": {},
67 "output_type": "execute_result"
68 }
69 ],
70 "source": [
71 "sum(1 for f in flips.values()\n",
72 " if unburnt_sorted(enflip(stack, f)))"
73 ]
74 },
75 {
76 "cell_type": "code",
77 "execution_count": 30,
78 "metadata": {},
79 "outputs": [
80 {
81 "data": {
82 "text/plain": [
83 "92"
84 ]
85 },
86 "execution_count": 30,
87 "metadata": {},
88 "output_type": "execute_result"
89 }
90 ],
91 "source": [
92 "[f for f in flips\n",
93 " if burnt_sorted(enflip(stack, flips[f], burnt=True))][0]"
94 ]
95 },
96 {
97 "cell_type": "code",
98 "execution_count": null,
99 "metadata": {},
100 "outputs": [],
101 "source": []
102 }
103 ],
104 "metadata": {
105 "kernelspec": {
106 "display_name": "Python 3",
107 "language": "python",
108 "name": "python3"
109 },
110 "language_info": {
111 "codemirror_mode": {
112 "name": "ipython",
113 "version": 3
114 },
115 "file_extension": ".py",
116 "mimetype": "text/x-python",
117 "name": "python",
118 "nbconvert_exporter": "python",
119 "pygments_lexer": "ipython3",
120 "version": "3.6.6"
121 }
122 },
123 "nbformat": 4,
124 "nbformat_minor": 2
125 }