a5632bb9b9a038d9720a2c98a02e8bc88e196870
[summerofcode2018soln.git] / src / task1 / task1.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "instructions = [l.strip() for l in open('../../data/01-mowmaster.txt')]"
10 ]
11 },
12 {
13 "cell_type": "markdown",
14 "metadata": {},
15 "source": [
16 "# Part 1"
17 ]
18 },
19 {
20 "cell_type": "code",
21 "execution_count": 3,
22 "metadata": {},
23 "outputs": [
24 {
25 "data": {
26 "text/plain": [
27 "1136"
28 ]
29 },
30 "execution_count": 3,
31 "metadata": {},
32 "output_type": "execute_result"
33 }
34 ],
35 "source": [
36 "sum(1 for i in instructions if not i.startswith('#'))"
37 ]
38 },
39 {
40 "cell_type": "markdown",
41 "metadata": {},
42 "source": [
43 "Or, as a one-liner:"
44 ]
45 },
46 {
47 "cell_type": "code",
48 "execution_count": 4,
49 "metadata": {},
50 "outputs": [
51 {
52 "data": {
53 "text/plain": [
54 "1136"
55 ]
56 },
57 "execution_count": 4,
58 "metadata": {},
59 "output_type": "execute_result"
60 }
61 ],
62 "source": [
63 "sum(1 for i in open('../../data/01-mowmaster.txt') if not i.startswith('#'))"
64 ]
65 },
66 {
67 "cell_type": "code",
68 "execution_count": 5,
69 "metadata": {},
70 "outputs": [
71 {
72 "data": {
73 "text/plain": [
74 "395"
75 ]
76 },
77 "execution_count": 5,
78 "metadata": {},
79 "output_type": "execute_result"
80 }
81 ],
82 "source": [
83 "sum(1 for i in instructions if i.startswith('#'))"
84 ]
85 },
86 {
87 "cell_type": "code",
88 "execution_count": 6,
89 "metadata": {},
90 "outputs": [
91 {
92 "data": {
93 "text/plain": [
94 "1531"
95 ]
96 },
97 "execution_count": 6,
98 "metadata": {},
99 "output_type": "execute_result"
100 }
101 ],
102 "source": [
103 "len(instructions)"
104 ]
105 },
106 {
107 "cell_type": "markdown",
108 "metadata": {},
109 "source": [
110 "# Part 2\n",
111 "\n",
112 "I have the `mower` as an \"object\" storing its state. (In this case, the mower is a `dict` and the state is just its location and direction.) As each instruction is executed, the mower is updated."
113 ]
114 },
115 {
116 "cell_type": "markdown",
117 "metadata": {},
118 "source": [
119 "An initial mower. The initial location and direction don't matter in this case, so I choose a location that's easy and an arbitrary location."
120 ]
121 },
122 {
123 "cell_type": "code",
124 "execution_count": 12,
125 "metadata": {},
126 "outputs": [],
127 "source": [
128 "def init_mowmaster():\n",
129 " return {'x': 0, 'y': 0, 'd': 90}"
130 ]
131 },
132 {
133 "cell_type": "markdown",
134 "metadata": {},
135 "source": [
136 "Execute the instructions. If it starts `C` or `A`, turn; if it starts `F`, move forward. Ignore all other instructions."
137 ]
138 },
139 {
140 "cell_type": "code",
141 "execution_count": 13,
142 "metadata": {},
143 "outputs": [],
144 "source": [
145 "def execute(mowmaster, instructions, debug=False):\n",
146 " for instruction in instructions:\n",
147 " if instruction == 'C':\n",
148 " mowmaster['d'] = (mowmaster['d'] + 90) % 360 # Use the modul\n",
149 " elif instruction == 'A':\n",
150 " mowmaster['d'] = (mowmaster['d'] - 90) % 360\n",
151 " elif instruction.startswith('F'):\n",
152 " mowmaster = move(mowmaster, int(instruction[1:]))\n",
153 " if debug: \n",
154 " print(instruction, mowmaster)\n",
155 " return mowmaster"
156 ]
157 },
158 {
159 "cell_type": "markdown",
160 "metadata": {},
161 "source": [
162 "The "
163 ]
164 },
165 {
166 "cell_type": "code",
167 "execution_count": 7,
168 "metadata": {},
169 "outputs": [],
170 "source": [
171 "def move(mower, distance):\n",
172 " if mower['d'] == 0:\n",
173 " mower['y'] += distance\n",
174 " elif mower['d'] == 90:\n",
175 " mower['x'] += distance\n",
176 " elif mower['d'] == 180:\n",
177 " mower['y'] -= distance\n",
178 " elif mower['d'] == 270:\n",
179 " mower['x'] -= distance\n",
180 " else:\n",
181 " raise ValueError\n",
182 " return mower"
183 ]
184 },
185 {
186 "cell_type": "code",
187 "execution_count": 16,
188 "metadata": {},
189 "outputs": [],
190 "source": [
191 "def mowmaster_distance(mw):\n",
192 " return abs(mw['x']) + abs(mw['y'])"
193 ]
194 },
195 {
196 "cell_type": "code",
197 "execution_count": 18,
198 "metadata": {},
199 "outputs": [
200 {
201 "data": {
202 "text/plain": [
203 "337"
204 ]
205 },
206 "execution_count": 18,
207 "metadata": {},
208 "output_type": "execute_result"
209 }
210 ],
211 "source": [
212 "mw = init_mowmaster()\n",
213 "execute(mw, instructions)\n",
214 "mowmaster_distance(mw)"
215 ]
216 },
217 {
218 "cell_type": "code",
219 "execution_count": null,
220 "metadata": {},
221 "outputs": [],
222 "source": []
223 }
224 ],
225 "metadata": {
226 "kernelspec": {
227 "display_name": "Python 3",
228 "language": "python",
229 "name": "python3"
230 },
231 "language_info": {
232 "codemirror_mode": {
233 "name": "ipython",
234 "version": 3
235 },
236 "file_extension": ".py",
237 "mimetype": "text/x-python",
238 "name": "python",
239 "nbconvert_exporter": "python",
240 "pygments_lexer": "ipython3",
241 "version": "3.6.5"
242 }
243 },
244 "nbformat": 4,
245 "nbformat_minor": 2
246 }