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.Map as M"
33 "type Point = (Int, Int)\n",
35 "data Flag = Clean | Weakened | Infected | Flagged deriving (Show, Eq)\n",
37 "type Infection = M.Map Point Flag\n",
39 "data Direction = Up | Right | Down | Left deriving (Show, Eq, Enum)\n",
41 "data World = World { infected :: Infection\n",
42 " , position :: Point\n",
43 " , direction :: Direction\n",
44 " , infectionCount :: Int\n",
45 " } deriving (Eq, Show)\n",
55 "text <- readFile \"../../data/advent22.txt\"\n",
65 "sampleGrid = lines \"..#\\n#..\\n...\\n\""
74 "initialInfected g = M.fromList [((r, c), Infected) | r <- [0..(length g - 1)], c <- [0..((length . head) g - 1)],\n",
86 "fromList [((0,2),Infected),((1,0),Infected)]"
90 "output_type": "display_data"
94 "initialInfected sampleGrid"
103 "initialPosition g = (length g `div` 2, (length . head) g `div` 2)"
108 "execution_count": 10,
118 "output_type": "display_data"
122 "initialPosition sampleGrid"
127 "execution_count": 11,
131 "leftOf Up = Left\n",
132 "leftOf x = pred x\n",
134 "rightOf Left = Up\n",
140 "execution_count": 12,
150 "output_type": "display_data"
159 "execution_count": 13,
163 "delta :: Direction -> Point\n",
164 "delta Up = (-1, 0)\n",
165 "delta Right = (0, 1)\n",
166 "delta Down = (1, 0)\n",
167 "delta Left = (0, -1)"
172 "execution_count": 14,
176 "(+:) (r, c) (dr, dc) = (r + dr, c + dc)"
181 "execution_count": 15,
185 "initialWorld grid = World \n",
186 " { infected = initialInfected grid\n",
187 " , position = initialPosition grid\n",
188 " , direction = Up\n",
189 " , infectionCount = 0\n",
195 "execution_count": 16,
201 "World {infected = fromList [((0,2),Infected),((1,0),Infected)], position = (1,1), direction = Up, infectionCount = 0}"
205 "output_type": "display_data"
209 "initialWorld sampleGrid"
214 "execution_count": 17,
218 "step world = World {infected = inf', position = pos', direction = dir', infectionCount = ic'}\n",
219 " where here = position world\n",
220 " stateHere = M.findWithDefault Clean here (infected world)\n",
221 " dir' = case stateHere of \n",
222 " Clean -> leftOf (direction world)\n",
223 " Weakened -> direction world\n",
224 " Infected -> rightOf (direction world)\n",
225 " Flagged -> rightOf (rightOf (direction world))\n",
226 " stateHere' = case stateHere of \n",
227 " Clean -> Weakened\n",
228 " Weakened -> Infected\n",
229 " Infected -> Flagged\n",
230 " Flagged -> Clean\n",
231 " inf' = M.insert here stateHere' (infected world)\n",
233 " ic' = if stateHere' == Infected then infectionCount world + 1\n",
234 " else infectionCount world\n",
235 " pos' = here +: delta dir'"
240 "execution_count": 19,
246 "World {infected = fromList [((0,2),Infected),((1,0),Flagged),((1,1),Weakened)], position = (0,0), direction = Up, infectionCount = 0}"
250 "output_type": "display_data"
254 "step $ step $ initialWorld sampleGrid"
259 "execution_count": 21,
263 "progress n = (!! n) . iterate step "
268 "execution_count": 25,
276 "World {infected = fromList [((0,-1),Weakened),((0,0),Weakened),((0,2),Infected),((1,-1),Infected),((1,0),Clean),((1,1),Weakened)], position = (1,-2), direction = Left, infectionCount = 1}"
280 "output_type": "display_data"
284 "progress 7 $ initialWorld sampleGrid"
289 "execution_count": 26,
301 "output_type": "display_data"
305 "infectionCount $ progress 100 $ initialWorld sampleGrid"
310 "execution_count": 27,
322 "output_type": "display_data"
326 "infectionCount $ progress 10000000 $ initialWorld sampleGrid"
331 "execution_count": 28,
343 "output_type": "display_data"
347 "infectionCount $ progress 10000000 $ initialWorld grid"
352 "execution_count": null,
360 "display_name": "Haskell",
361 "language": "haskell",
365 "codemirror_mode": "ihaskell",
366 "file_extension": ".hs",