9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}\n",
11 "{-# LANGUAGE OverloadedStrings #-}\n",
12 "{-# LANGUAGE TypeFamilies #-}"
33 "[\" | \",\" | +--+ \",\" A | C \",\" F---|----E|--+ \",\" | | | D \",\" +B-+ +--+ \",\" \"]"
37 "output_type": "display_data"
41 "sampleText <- readFile \"sample-maze.txt\"\n",
42 "sample = lines sampleText\n",
52 "type Maze = [String]"
61 "data Direction = Up | Down | Left | Right deriving (Show, Eq)"
70 "data Progress = Progress { row :: Int\n",
72 " , direction :: Direction\n",
73 " , letters :: String\n",
74 " , stepCount :: Int\n",
75 " } deriving (Show, Eq)"
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"
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)"
105 "execution_count": 9,
109 "isContinuation '|' = True\n",
110 "isContinuation '-' = True\n",
111 "isContinuation _ = False\n",
113 "isJunction '+' = True\n",
114 "isJunction _ = False "
119 "execution_count": 10,
123 "location :: Maze -> Int -> Int -> Char\n",
124 "location maze r c = (maze!!r)!!c"
129 "execution_count": 11,
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",
150 "execution_count": 12,
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",
164 " sc' = stepCount progress + 1"
169 "execution_count": 13,
174 "isFinished :: Maze -> Progress -> Bool\n",
175 "isFinished maze progress = isSpace $ location maze (row progress) (column progress)"
180 "execution_count": 14,
184 "navigate' maze progress = \n",
185 " if isFinished maze progress \n",
187 " else navigate' maze (step maze progress)"
192 "execution_count": 15,
196 "navigate :: Maze -> Progress\n",
197 "navigate maze = navigate' maze progress\n",
198 " where progress = startProgress maze"
203 "execution_count": 16,
209 "Progress {row = 3, column = 0, direction = Left, letters = \"ABCDEF\", stepCount = 38}"
213 "output_type": "display_data"
222 "execution_count": 17,
232 "output_type": "display_data"
241 "execution_count": 18,
251 "output_type": "display_data"
260 "execution_count": 19,
266 "Progress {row = 5, column = 8, direction = Right, letters = \"\", stepCount = 0}"
270 "output_type": "display_data"
274 "pt = (startProgress sample) {row = 5, column = 8, direction = Right}\n",
280 "execution_count": 20,
290 "output_type": "display_data"
294 "newDirection sample pt"
299 "execution_count": 21,
305 "Progress {row = 4, column = 8, direction = Up, letters = \"\", stepCount = 1}"
309 "output_type": "display_data"
318 "execution_count": 25,
327 "execution_count": 26,
336 "execution_count": 27,
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"
351 "execution_count": 28,
362 "output_type": "display_data"
371 "execution_count": null,
381 "display_name": "Haskell",
382 "language": "haskell",
386 "codemirror_mode": "ihaskell",
387 "file_extension": ".hs",