Day 22
[advent-of-code-17.git] / src / advent22 / advent22a.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 #-}\n",
13 "{-# LANGUAGE BangPatterns #-}"
14 ]
15 },
16 {
17 "cell_type": "code",
18 "execution_count": 2,
19 "metadata": {},
20 "outputs": [],
21 "source": [
22 "import Prelude hiding (Left, Right)\n",
23 "import Data.List\n",
24 "import qualified Data.Set as S"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": 3,
30 "metadata": {},
31 "outputs": [],
32 "source": [
33 "type Point = (Int, Int)\n",
34 "type Infection = S.Set Point\n",
35 "\n",
36 "data Direction = Up | Right | Down | Left deriving (Show, Eq, Enum)\n",
37 "\n",
38 "data World = World { infected :: Infection\n",
39 " , position :: Point\n",
40 " , direction :: Direction\n",
41 " , infectionCount :: Int\n",
42 " } deriving (Eq, Show)\n",
43 " "
44 ]
45 },
46 {
47 "cell_type": "code",
48 "execution_count": 4,
49 "metadata": {},
50 "outputs": [],
51 "source": [
52 "text <- readFile \"../../data/advent22.txt\"\n",
53 "grid = lines text"
54 ]
55 },
56 {
57 "cell_type": "code",
58 "execution_count": 5,
59 "metadata": {},
60 "outputs": [],
61 "source": [
62 "sampleGrid = lines \"..#\\n#..\\n...\\n\""
63 ]
64 },
65 {
66 "cell_type": "code",
67 "execution_count": 6,
68 "metadata": {},
69 "outputs": [],
70 "source": [
71 "initialInfected g = S.fromList [(r, c) | r <- [0..(length g - 1)], c <- [0..((length . head) g - 1)],\n",
72 " g!!r!!c == '#']"
73 ]
74 },
75 {
76 "cell_type": "code",
77 "execution_count": 7,
78 "metadata": {},
79 "outputs": [
80 {
81 "data": {
82 "text/plain": [
83 "fromList [(0,2),(1,0)]"
84 ]
85 },
86 "metadata": {},
87 "output_type": "display_data"
88 }
89 ],
90 "source": [
91 "initialInfected sampleGrid"
92 ]
93 },
94 {
95 "cell_type": "code",
96 "execution_count": 8,
97 "metadata": {},
98 "outputs": [],
99 "source": [
100 "initialPosition g = (length g `div` 2, (length . head) g `div` 2)"
101 ]
102 },
103 {
104 "cell_type": "code",
105 "execution_count": 9,
106 "metadata": {},
107 "outputs": [
108 {
109 "data": {
110 "text/plain": [
111 "(1,1)"
112 ]
113 },
114 "metadata": {},
115 "output_type": "display_data"
116 }
117 ],
118 "source": [
119 "initialPosition sampleGrid"
120 ]
121 },
122 {
123 "cell_type": "code",
124 "execution_count": 10,
125 "metadata": {},
126 "outputs": [],
127 "source": [
128 "leftOf Up = Left\n",
129 "leftOf x = pred x\n",
130 "\n",
131 "rightOf Left = Up\n",
132 "rightOf x = succ x"
133 ]
134 },
135 {
136 "cell_type": "code",
137 "execution_count": 11,
138 "metadata": {},
139 "outputs": [
140 {
141 "data": {
142 "text/plain": [
143 "Down"
144 ]
145 },
146 "metadata": {},
147 "output_type": "display_data"
148 }
149 ],
150 "source": [
151 "leftOf Left"
152 ]
153 },
154 {
155 "cell_type": "code",
156 "execution_count": 12,
157 "metadata": {},
158 "outputs": [],
159 "source": [
160 "delta :: Direction -> Point\n",
161 "delta Up = (-1, 0)\n",
162 "delta Right = (0, 1)\n",
163 "delta Down = (1, 0)\n",
164 "delta Left = (0, -1)"
165 ]
166 },
167 {
168 "cell_type": "code",
169 "execution_count": 13,
170 "metadata": {},
171 "outputs": [],
172 "source": [
173 "(+:) (r, c) (dr, dc) = (r + dr, c + dc)"
174 ]
175 },
176 {
177 "cell_type": "code",
178 "execution_count": 14,
179 "metadata": {},
180 "outputs": [],
181 "source": [
182 "initialWorld grid = World \n",
183 " { infected = initialInfected grid\n",
184 " , position = initialPosition grid\n",
185 " , direction = Up\n",
186 " , infectionCount = 0\n",
187 " }"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": 15,
193 "metadata": {},
194 "outputs": [
195 {
196 "data": {
197 "text/plain": [
198 "World {infected = fromList [(0,2),(1,0)], position = (1,1), direction = Up, infectionCount = 0}"
199 ]
200 },
201 "metadata": {},
202 "output_type": "display_data"
203 }
204 ],
205 "source": [
206 "initialWorld sampleGrid"
207 ]
208 },
209 {
210 "cell_type": "code",
211 "execution_count": 16,
212 "metadata": {},
213 "outputs": [],
214 "source": [
215 "step world = World {infected = inf', position = pos', direction = dir', infectionCount = ic'}\n",
216 " where here = position world\n",
217 " infectedHere = here `S.member` infected world\n",
218 " dir' = if infectedHere then rightOf (direction world)\n",
219 " else leftOf (direction world)\n",
220 " inf' = if infectedHere then S.delete here $ infected world\n",
221 " else S.insert here $ infected world\n",
222 " ic' = if infectedHere then infectionCount world\n",
223 " else infectionCount world + 1\n",
224 " pos' = here +: delta dir'"
225 ]
226 },
227 {
228 "cell_type": "code",
229 "execution_count": 17,
230 "metadata": {},
231 "outputs": [
232 {
233 "data": {
234 "text/plain": [
235 "World {infected = fromList [(0,2),(1,1)], position = (0,0), direction = Up, infectionCount = 1}"
236 ]
237 },
238 "metadata": {},
239 "output_type": "display_data"
240 }
241 ],
242 "source": [
243 "step $ step $ initialWorld sampleGrid"
244 ]
245 },
246 {
247 "cell_type": "code",
248 "execution_count": 18,
249 "metadata": {},
250 "outputs": [],
251 "source": [
252 "progress n = (!! n) . iterate step "
253 ]
254 },
255 {
256 "cell_type": "code",
257 "execution_count": 19,
258 "metadata": {
259 "scrolled": true
260 },
261 "outputs": [
262 {
263 "data": {
264 "text/plain": [
265 "5587"
266 ]
267 },
268 "metadata": {},
269 "output_type": "display_data"
270 }
271 ],
272 "source": [
273 "infectionCount $ progress 10000 $ initialWorld sampleGrid"
274 ]
275 },
276 {
277 "cell_type": "code",
278 "execution_count": 20,
279 "metadata": {
280 "scrolled": true
281 },
282 "outputs": [
283 {
284 "data": {
285 "text/plain": [
286 "5182"
287 ]
288 },
289 "metadata": {},
290 "output_type": "display_data"
291 }
292 ],
293 "source": [
294 "infectionCount $ progress 10000 $ initialWorld grid"
295 ]
296 },
297 {
298 "cell_type": "code",
299 "execution_count": null,
300 "metadata": {},
301 "outputs": [],
302 "source": []
303 }
304 ],
305 "metadata": {
306 "kernelspec": {
307 "display_name": "Haskell",
308 "language": "haskell",
309 "name": "haskell"
310 },
311 "language_info": {
312 "codemirror_mode": "ihaskell",
313 "file_extension": ".hs",
314 "name": "haskell",
315 "version": "8.0.2"
316 }
317 },
318 "nbformat": 4,
319 "nbformat_minor": 2
320 }