Task 1
[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"
111 ]
112 },
113 {
114 "cell_type": "code",
115 "execution_count": 7,
116 "metadata": {},
117 "outputs": [],
118 "source": [
119 "def move(mower, distance):\n",
120 " if mower['d'] == 0:\n",
121 " mower['y'] += distance\n",
122 " elif mower['d'] == 90:\n",
123 " mower['x'] += distance\n",
124 " elif mower['d'] == 180:\n",
125 " mower['y'] -= distance\n",
126 " elif mower['d'] == 270:\n",
127 " mower['x'] -= distance\n",
128 " else:\n",
129 " raise ValueError\n",
130 " return mower"
131 ]
132 },
133 {
134 "cell_type": "code",
135 "execution_count": 12,
136 "metadata": {},
137 "outputs": [],
138 "source": [
139 "def init_mowmaster():\n",
140 " return {'x': 0, 'y': 0, 'd': 90}"
141 ]
142 },
143 {
144 "cell_type": "code",
145 "execution_count": 16,
146 "metadata": {},
147 "outputs": [],
148 "source": [
149 "def mowmaster_distance(mw):\n",
150 " return abs(mw['x']) + abs(mw['y'])"
151 ]
152 },
153 {
154 "cell_type": "code",
155 "execution_count": 13,
156 "metadata": {},
157 "outputs": [],
158 "source": [
159 "def execute(mowmaster, instructions, debug=False):\n",
160 " for instruction in instructions:\n",
161 " if instruction == 'C':\n",
162 " mowmaster['d'] = (mowmaster['d'] + 90) % 360\n",
163 " elif instruction == 'A':\n",
164 " mowmaster['d'] = (mowmaster['d'] - 90) % 360\n",
165 " elif instruction.startswith('F'):\n",
166 " mowmaster = move(mowmaster, int(instruction[1:]))\n",
167 " if debug: \n",
168 " print(instruction, mowmaster)\n",
169 " return mowmaster"
170 ]
171 },
172 {
173 "cell_type": "code",
174 "execution_count": 18,
175 "metadata": {},
176 "outputs": [
177 {
178 "data": {
179 "text/plain": [
180 "337"
181 ]
182 },
183 "execution_count": 18,
184 "metadata": {},
185 "output_type": "execute_result"
186 }
187 ],
188 "source": [
189 "mw = init_mowmaster()\n",
190 "execute(mw, instructions)\n",
191 "mowmaster_distance(mw)"
192 ]
193 },
194 {
195 "cell_type": "code",
196 "execution_count": null,
197 "metadata": {},
198 "outputs": [],
199 "source": []
200 }
201 ],
202 "metadata": {
203 "kernelspec": {
204 "display_name": "Python 3",
205 "language": "python",
206 "name": "python3"
207 },
208 "language_info": {
209 "codemirror_mode": {
210 "name": "ipython",
211 "version": 3
212 },
213 "file_extension": ".py",
214 "mimetype": "text/x-python",
215 "name": "python",
216 "nbconvert_exporter": "python",
217 "pygments_lexer": "ipython3",
218 "version": "3.6.5"
219 }
220 },
221 "nbformat": 4,
222 "nbformat_minor": 2
223 }