9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
19 "import Text.Parsec \n",
20 "import Text.ParserCombinators.Parsec.Number\n",
21 "import qualified Data.Map.Strict as M"
30 "data Instruction = Instruction \n",
31 " { register :: String\n",
32 " , direction :: String\n",
34 " , conditionRegister :: String\n",
35 " , operation :: String\n",
36 " , comparator :: Int\n",
37 " } deriving (Show, Eq)"
46 "type Memory = M.Map String Int"
55 "onlySpaces = many (oneOf \" \\t\")\n",
56 "symP = (many lower) <* onlySpaces\n",
57 "operationP = (many1 (oneOf \"!<>=\")) <* onlySpaces"
66 "iFile = iLine `sepBy` newline \n",
67 "iLine = instructify <$> symP \n",
70 " <*> ( onlySpaces *> string \"if\" *> onlySpaces *> symP )\n",
73 " where instructify r d c cr o p = Instruction { register = r\n",
76 " , conditionRegister = cr\n",
78 " , comparator = p\n",
88 "parseFile :: String -> Either ParseError [Instruction]\n",
89 "parseFile input = parse iFile \"(unknown)\" input\n",
91 "parseLine :: String -> Either ParseError Instruction\n",
92 "parseLine input = parse iLine \"(unknown)\" input\n",
94 "successfulParse :: Either ParseError [a] -> [a]\n",
95 "successfulParse (Left _) = []\n",
96 "successfulParse (Right a) = a"
101 "execution_count": 8,
107 "Right (Instruction {register = \"b\", direction = \"inc\", change = 5, conditionRegister = \"a\", operation = \">\", comparator = 1})"
111 "output_type": "display_data"
115 "parseLine \"b inc 5 if a > 1\""
120 "execution_count": 9,
124 "sampleT = \"b inc 5 if a > 1\\na inc 1 if b < 5\\nc dec -10 if a >= 1\\nc inc -20 if c == 10\""
129 "execution_count": 10,
135 "[Instruction {register = \"b\", direction = \"inc\", change = 5, conditionRegister = \"a\", operation = \">\", comparator = 1},Instruction {register = \"a\", direction = \"inc\", change = 1, conditionRegister = \"b\", operation = \"<\", comparator = 5},Instruction {register = \"c\", direction = \"dec\", change = -10, conditionRegister = \"a\", operation = \">=\", comparator = 1},Instruction {register = \"c\", direction = \"inc\", change = -20, conditionRegister = \"c\", operation = \"==\", comparator = 10}]"
139 "output_type": "display_data"
143 "sample = successfulParse $ parseFile sampleT\n",
149 "execution_count": 11,
153 "conditionEval :: Int -> String -> Int -> Bool\n",
154 "conditionEval reg op val\n",
155 " | op == \"==\" = reg == val\n",
156 " | op == \"<\" = reg < val\n",
157 " | op == \">\" = reg > val\n",
158 " | op == \"<=\" = reg <= val\n",
159 " | op == \">=\" = reg >= val\n",
160 " | op == \"!=\" = reg /= val"
165 "execution_count": 12,
169 "-- effectiveChange :: String -> Int -> Int\n",
170 "-- effectiveChange dir val\n",
171 "-- | dir == \"inc\" = val\n",
172 "-- | dir == \"dec\" = - val"
177 "execution_count": 30,
181 "effectiveChange :: String -> Int -> Int\n",
182 "effectiveChange \"inc\" val = val\n",
183 "effectiveChange \"dec\" val = -val"
188 "execution_count": 32,
192 "processInstruction memory instruction = memory'\n",
193 " where v = M.findWithDefault 0 (register instruction) memory\n",
194 " cv = M.findWithDefault 0 (conditionRegister instruction) memory\n",
195 " condition = conditionEval cv (operation instruction) (comparator instruction)\n",
196 " delta = effectiveChange (direction instruction) (change instruction)\n",
197 " memory' = if condition\n",
198 " then M.insert (register instruction) (v + delta) memory\n",
204 "execution_count": 33,
208 "processInstructions = foldl processInstruction M.empty "
213 "execution_count": 34,
219 "fromList [(\"a\",1),(\"c\",-10)]"
223 "output_type": "display_data"
227 "processInstructions sample"
232 "execution_count": 47,
238 " | otherwise = maximum $ M.elems m"
243 "execution_count": 48,
253 "output_type": "display_data"
257 "largestValue $ processInstructions sample"
262 "execution_count": 49,
266 "part1 = largestValue . processInstructions"
271 "execution_count": 50,
275 "-- part1 = processInstructions"
280 "execution_count": 51,
286 " text <- readFile \"../../data/advent08.txt\"\n",
287 " let instrs = successfulParse $ parseFile text\n",
288 " print $ part1 instrs"
293 "execution_count": 52,
303 "output_type": "display_data"
312 "execution_count": 53,
316 "processInstructionH (highest, memory) instruction = (highest', memory')\n",
317 " where memory' = processInstruction memory instruction\n",
318 " h = largestValue memory'\n",
319 " highest' = if h > highest then h else highest"
324 "execution_count": 54,
328 "processInstructionsH = foldl processInstructionH (0, M.empty)"
333 "execution_count": 55,
339 "(10,fromList [(\"a\",1),(\"c\",-10)])"
343 "output_type": "display_data"
347 "processInstructionsH sample"
352 "execution_count": 56,
356 "part2 = fst . processInstructionsH"
361 "execution_count": 57,
367 " text <- readFile \"../../data/advent08.txt\"\n",
368 " let instrs = successfulParse $ parseFile text\n",
369 " print $ part1 instrs\n",
370 " print $ part2 instrs"
375 "execution_count": 58,
386 "output_type": "display_data"
395 "execution_count": null,
403 "display_name": "Haskell",
404 "language": "haskell",
408 "codemirror_mode": "ihaskell",
409 "file_extension": ".hs",