09dce0219c799687c3e69b4f0667f43df2cd98d4
[advent-of-code-17.git] / src / advent19 / advent19.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}\n",
11 "{-# LANGUAGE OverloadedStrings #-}\n",
12 "{-# LANGUAGE TypeFamilies #-}"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 2,
18 "metadata": {},
19 "outputs": [],
20 "source": [
21 "import Data.List\n",
22 "import Data.Char"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 3,
28 "metadata": {},
29 "outputs": [
30 {
31 "data": {
32 "text/plain": [
33 "[\" | \",\" | +--+ \",\" A | C \",\" F---|----E|--+ \",\" | | | D \",\" +B-+ +--+ \",\" \"]"
34 ]
35 },
36 "metadata": {},
37 "output_type": "display_data"
38 }
39 ],
40 "source": [
41 "sampleText <- readFile \"sample-maze.txt\"\n",
42 "sample = lines sampleText\n",
43 "print sample"
44 ]
45 },
46 {
47 "cell_type": "code",
48 "execution_count": 4,
49 "metadata": {},
50 "outputs": [],
51 "source": [
52 "type Maze = [String]"
53 ]
54 },
55 {
56 "cell_type": "code",
57 "execution_count": 5,
58 "metadata": {},
59 "outputs": [],
60 "source": [
61 "data Direction = Up | Down | Left | Right deriving (Show, Eq)"
62 ]
63 },
64 {
65 "cell_type": "code",
66 "execution_count": 6,
67 "metadata": {},
68 "outputs": [],
69 "source": [
70 "data Progress = Progress { row :: Int\n",
71 " , column :: Int\n",
72 " , direction :: Direction\n",
73 " , letters :: String\n",
74 " , stepCount :: Int\n",
75 " } deriving (Show, Eq)"
76 ]
77 },
78 {
79 "cell_type": "code",
80 "execution_count": 7,
81 "metadata": {},
82 "outputs": [],
83 "source": [
84 "startProgress :: Maze -> Progress\n",
85 "startProgress maze = Progress {row = 0, column = startCol, direction = Down, letters = \"\", stepCount = 0}\n",
86 " where topRow = maze!!0\n",
87 " startCol = head $ elemIndices '|' topRow"
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": 8,
93 "metadata": {},
94 "outputs": [],
95 "source": [
96 "delta :: Direction -> (Int, Int)\n",
97 "delta Up = (-1, 0)\n",
98 "delta Down = ( 1, 0)\n",
99 "delta Left = ( 0, -1)\n",
100 "delta Right = ( 0, 1)"
101 ]
102 },
103 {
104 "cell_type": "code",
105 "execution_count": 9,
106 "metadata": {},
107 "outputs": [],
108 "source": [
109 "isContinuation '|' = True\n",
110 "isContinuation '-' = True\n",
111 "isContinuation _ = False\n",
112 "\n",
113 "isJunction '+' = True\n",
114 "isJunction _ = False "
115 ]
116 },
117 {
118 "cell_type": "code",
119 "execution_count": 10,
120 "metadata": {},
121 "outputs": [],
122 "source": [
123 "location :: Maze -> Int -> Int -> Char\n",
124 "location maze r c = (maze!!r)!!c"
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": 11,
130 "metadata": {},
131 "outputs": [],
132 "source": [
133 "newDirection :: Maze -> Progress -> Direction\n",
134 "newDirection maze progress = \n",
135 " if d == Up || d == Down \n",
136 " then if isSpace leftChar then Right else Left\n",
137 " else if isSpace upChar then Down else Up\n",
138 " where d = direction progress\n",
139 " r = row progress\n",
140 " c = column progress\n",
141 " upChar = location maze (r - 1) c\n",
142 "-- downChar = location maze (r + 1) c\n",
143 " leftChar = location maze r (c - 1)\n",
144 "-- rightChar = location maze r (c + 1)\n",
145 " "
146 ]
147 },
148 {
149 "cell_type": "code",
150 "execution_count": 12,
151 "metadata": {},
152 "outputs": [],
153 "source": [
154 "step :: Maze -> Progress -> Progress\n",
155 "step maze progress = progress {row = r', column = c', direction = d', letters = l', stepCount = sc'}\n",
156 " where r = row progress\n",
157 " c = column progress\n",
158 " thisChar = location maze r c\n",
159 " l' = if isAlpha thisChar then (letters progress) ++ [thisChar] else letters progress\n",
160 " d' = if isJunction thisChar then newDirection maze progress else direction progress \n",
161 " (dr, dc) = delta d'\n",
162 " r' = r + dr\n",
163 " c' = c + dc\n",
164 " sc' = stepCount progress + 1"
165 ]
166 },
167 {
168 "cell_type": "code",
169 "execution_count": 13,
170 "metadata": {},
171 "outputs": [],
172 "source": [
173 "\n",
174 "isFinished :: Maze -> Progress -> Bool\n",
175 "isFinished maze progress = isSpace $ location maze (row progress) (column progress)"
176 ]
177 },
178 {
179 "cell_type": "code",
180 "execution_count": 14,
181 "metadata": {},
182 "outputs": [],
183 "source": [
184 "navigate' maze progress = \n",
185 " if isFinished maze progress \n",
186 " then progress\n",
187 " else navigate' maze (step maze progress)"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": 15,
193 "metadata": {},
194 "outputs": [],
195 "source": [
196 "navigate :: Maze -> Progress\n",
197 "navigate maze = navigate' maze progress\n",
198 " where progress = startProgress maze"
199 ]
200 },
201 {
202 "cell_type": "code",
203 "execution_count": 16,
204 "metadata": {},
205 "outputs": [
206 {
207 "data": {
208 "text/plain": [
209 "Progress {row = 3, column = 0, direction = Left, letters = \"ABCDEF\", stepCount = 38}"
210 ]
211 },
212 "metadata": {},
213 "output_type": "display_data"
214 }
215 ],
216 "source": [
217 "navigate sample"
218 ]
219 },
220 {
221 "cell_type": "code",
222 "execution_count": 17,
223 "metadata": {},
224 "outputs": [
225 {
226 "data": {
227 "text/plain": [
228 "'+'"
229 ]
230 },
231 "metadata": {},
232 "output_type": "display_data"
233 }
234 ],
235 "source": [
236 "sample!!5!!8"
237 ]
238 },
239 {
240 "cell_type": "code",
241 "execution_count": 18,
242 "metadata": {},
243 "outputs": [
244 {
245 "data": {
246 "text/plain": [
247 "False"
248 ]
249 },
250 "metadata": {},
251 "output_type": "display_data"
252 }
253 ],
254 "source": [
255 "isJunction '|'"
256 ]
257 },
258 {
259 "cell_type": "code",
260 "execution_count": 19,
261 "metadata": {},
262 "outputs": [
263 {
264 "data": {
265 "text/plain": [
266 "Progress {row = 5, column = 8, direction = Right, letters = \"\", stepCount = 0}"
267 ]
268 },
269 "metadata": {},
270 "output_type": "display_data"
271 }
272 ],
273 "source": [
274 "pt = (startProgress sample) {row = 5, column = 8, direction = Right}\n",
275 "pt"
276 ]
277 },
278 {
279 "cell_type": "code",
280 "execution_count": 20,
281 "metadata": {},
282 "outputs": [
283 {
284 "data": {
285 "text/plain": [
286 "Up"
287 ]
288 },
289 "metadata": {},
290 "output_type": "display_data"
291 }
292 ],
293 "source": [
294 "newDirection sample pt"
295 ]
296 },
297 {
298 "cell_type": "code",
299 "execution_count": 21,
300 "metadata": {},
301 "outputs": [
302 {
303 "data": {
304 "text/plain": [
305 "Progress {row = 4, column = 8, direction = Up, letters = \"\", stepCount = 1}"
306 ]
307 },
308 "metadata": {},
309 "output_type": "display_data"
310 }
311 ],
312 "source": [
313 "step sample pt"
314 ]
315 },
316 {
317 "cell_type": "code",
318 "execution_count": 25,
319 "metadata": {},
320 "outputs": [],
321 "source": [
322 "part1 = letters "
323 ]
324 },
325 {
326 "cell_type": "code",
327 "execution_count": 26,
328 "metadata": {},
329 "outputs": [],
330 "source": [
331 "part2 = stepCount"
332 ]
333 },
334 {
335 "cell_type": "code",
336 "execution_count": 27,
337 "metadata": {},
338 "outputs": [],
339 "source": [
340 "main :: IO ()\n",
341 "main = do \n",
342 " text <- readFile \"../../data/advent19.txt\"\n",
343 " let maze = lines text\n",
344 " let progress = navigate maze\n",
345 " print $ part1 progress\n",
346 " print $ part2 progress"
347 ]
348 },
349 {
350 "cell_type": "code",
351 "execution_count": 28,
352 "metadata": {},
353 "outputs": [
354 {
355 "data": {
356 "text/plain": [
357 "\"XYFDJNRCQA\"\n",
358 "17450"
359 ]
360 },
361 "metadata": {},
362 "output_type": "display_data"
363 }
364 ],
365 "source": [
366 "main"
367 ]
368 },
369 {
370 "cell_type": "code",
371 "execution_count": null,
372 "metadata": {},
373 "outputs": [],
374 "source": [
375 "navigate "
376 ]
377 }
378 ],
379 "metadata": {
380 "kernelspec": {
381 "display_name": "Haskell",
382 "language": "haskell",
383 "name": "haskell"
384 },
385 "language_info": {
386 "codemirror_mode": "ihaskell",
387 "file_extension": ".hs",
388 "name": "haskell",
389 "version": "8.0.2"
390 }
391 },
392 "nbformat": 4,
393 "nbformat_minor": 2
394 }