9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}\n",
11 "{-# LANGUAGE OverloadedStrings #-}\n",
12 "{-# LANGUAGE TypeFamilies #-}\n",
13 "{-# LANGUAGE BangPatterns #-}"
22 "import Prelude hiding (Left, Right)\n",
24 "import qualified Data.Set as S"
33 "type Point = (Int, Int)\n",
34 "type Infection = S.Set Point\n",
36 "data Direction = Up | Right | Down | Left deriving (Show, Eq, Enum)\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",
52 "text <- readFile \"../../data/advent22.txt\"\n",
62 "sampleGrid = lines \"..#\\n#..\\n...\\n\""
71 "initialInfected g = S.fromList [(r, c) | r <- [0..(length g - 1)], c <- [0..((length . head) g - 1)],\n",
83 "fromList [(0,2),(1,0)]"
87 "output_type": "display_data"
91 "initialInfected sampleGrid"
100 "initialPosition g = (length g `div` 2, (length . head) g `div` 2)"
105 "execution_count": 9,
115 "output_type": "display_data"
119 "initialPosition sampleGrid"
124 "execution_count": 10,
128 "leftOf Up = Left\n",
129 "leftOf x = pred x\n",
131 "rightOf Left = Up\n",
137 "execution_count": 11,
147 "output_type": "display_data"
156 "execution_count": 12,
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)"
169 "execution_count": 13,
173 "(+:) (r, c) (dr, dc) = (r + dr, c + dc)"
178 "execution_count": 14,
182 "initialWorld grid = World \n",
183 " { infected = initialInfected grid\n",
184 " , position = initialPosition grid\n",
185 " , direction = Up\n",
186 " , infectionCount = 0\n",
192 "execution_count": 15,
198 "World {infected = fromList [(0,2),(1,0)], position = (1,1), direction = Up, infectionCount = 0}"
202 "output_type": "display_data"
206 "initialWorld sampleGrid"
211 "execution_count": 16,
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'"
229 "execution_count": 17,
235 "World {infected = fromList [(0,2),(1,1)], position = (0,0), direction = Up, infectionCount = 1}"
239 "output_type": "display_data"
243 "step $ step $ initialWorld sampleGrid"
248 "execution_count": 18,
252 "progress n = (!! n) . iterate step "
257 "execution_count": 19,
269 "output_type": "display_data"
273 "infectionCount $ progress 10000 $ initialWorld sampleGrid"
278 "execution_count": 20,
290 "output_type": "display_data"
294 "infectionCount $ progress 10000 $ initialWorld grid"
299 "execution_count": null,
307 "display_name": "Haskell",
308 "language": "haskell",
312 "codemirror_mode": "ihaskell",
313 "file_extension": ".hs",