this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

changes

+168 -32
+168 -32
aoc-2019/AOC19-02.ipynb
··· 2 2 "cells": [ 3 3 { 4 4 "cell_type": "code", 5 - "execution_count": 4, 5 + "execution_count": 1, 6 6 "metadata": {}, 7 7 "outputs": [], 8 8 "source": [ ··· 13 13 }, 14 14 { 15 15 "cell_type": "code", 16 - "execution_count": 5, 16 + "execution_count": 2, 17 17 "metadata": {}, 18 18 "outputs": [], 19 19 "source": [ ··· 21 21 "import Control.Monad.State\n", 22 22 "import qualified Prelude as P (read, undefined)\n", 23 23 "import qualified Data.IntMap as IM\n", 24 - "import Data.Monoid\n" 24 + "import Data.Monoid\n", 25 + "import Control.Monad.Reader\n", 26 + "\n" 25 27 ] 26 28 }, 27 29 { 28 30 "cell_type": "code", 29 - "execution_count": 23, 31 + "execution_count": 15, 30 32 "metadata": {}, 31 33 "outputs": [], 32 34 "source": [ ··· 40 42 " _ `fmap` Unknown' = Unknown'\n", 41 43 " f `fmap` Result' i a = Result' i (f a)\n", 42 44 "\n", 45 + "\n", 43 46 "isHalt' :: ParseResult' a -> Bool\n", 44 47 "isHalt' (Halt' _) = True\n", 45 48 "isHalt' _ = False\n", ··· 55 58 "onResult (Halt' i) _ = Halt' i\n", 56 59 "onResult (Result' i a) k = k i a\n", 57 60 "\n", 58 - "onHalt :: ParseResult' a -> Maybe Output\n", 59 - "onHalt (Halt' i) = Just i\n", 60 - "onHalt _ = Nothing\n", 61 + "-- onHalt :: ParseResult' a -> Maybe Output\n", 62 + "-- onHalt (Halt' i) = Just i\n", 63 + "-- onHalt _ = Nothing\n", 61 64 "\n", 62 65 "newtype Parser' a = Parser' (Input -> ParseResult' a) \n", 63 66 "\n", ··· 78 81 "instance Monad Parser' where\n", 79 82 " (Parser' p) >>= f = Parser' (\\i ->\n", 80 83 " onResult (p i) (\\j a -> parse' (f a) j))\n", 81 - " \n", 84 + " \n", 85 + "(|||) :: Parser' a -> Parser' a -> Parser' a\n", 86 + "(Parser' p1) ||| (Parser' p2) = Parser' (\\i -> let v = p1 i in bool v (p2 i) (isError v || isHalt' v))\n", 82 87 "\n", 88 + "list1 :: Parser' a -> Parser' [a]\n", 89 + "list1 p = p >>= (\\a -> (a :) <$> list p)\n", 83 90 "\n", 84 - "addParser :: Parser' Output\n", 91 + "list :: Parser' a -> Parser' [a]\n", 92 + "list p = list1 p ||| pure []\n", 93 + "\n", 94 + "listt :: Parser' a -> Parser' [a]\n", 95 + "listt = P.undefined\n", 96 + "\n", 97 + "\n", 98 + "addParser :: Parser' (Input -> Output)\n", 85 99 "addParser = Parser' (\n", 86 100 " \\case\n", 87 101 " l@(o : a : b : c : xs) | o == 1 -> \n", 88 - " let v = IM.fromList $ itoList l\n", 89 - " in\n", 90 102 " Result' xs $ \n", 91 - " maybe \n", 92 - " v\n", 93 - " ((`IM.union` v) . IM.singleton c) \n", 94 - " (liftA2 (liftA2 (+)) (!! a) (!! b) l)\n", 103 + " \\j -> \n", 104 + " let v = IM.fromList $ itoList j\n", 105 + " in\n", 106 + " maybe \n", 107 + " v\n", 108 + " ((`IM.union` v) . IM.singleton c) \n", 109 + " (liftA2 (liftA2 (+)) (!! a) (!! b) j)\n", 95 110 " _ -> Unknown')\n", 96 - " \n", 97 - " \n", 98 - "prodParser :: Parser' Output\n", 111 + " \n", 112 + "prodParser :: Parser' (Input -> Output)\n", 99 113 "prodParser = Parser' (\n", 100 - " \\case \n", 114 + " \\case\n", 101 115 " l@(o : a : b : c : xs) | o == 2 -> \n", 102 - " let v = IM.fromList $ itoList l\n", 103 - " in\n", 104 116 " Result' xs $ \n", 105 - " maybe \n", 106 - " v \n", 107 - " ((`IM.union` v) . IM.singleton c) \n", 108 - " (liftA2 (liftA2 (*)) (!! a) (!! b) l) \n", 109 - " _ -> Unknown')\n", 117 + " \\j -> \n", 118 + " let v = IM.fromList $ itoList j\n", 119 + " in\n", 120 + " maybe \n", 121 + " v\n", 122 + " ((`IM.union` v) . IM.singleton c) \n", 123 + " (liftA2 (liftA2 (*)) (!! a) (!! b) j)\n", 124 + " _ -> Unknown') \n", 125 + " \n", 110 126 " \n", 111 - "haltParser :: Parser' Output\n", 127 + "-- prodParser :: Parser' Output\n", 128 + "-- prodParser = haltParser ||| Parser' (\n", 129 + "-- \\case \n", 130 + "-- l@(o : a : b : c : xs) | o == 2 -> \n", 131 + "-- let v = IM.fromList $ itoList l\n", 132 + "-- in\n", 133 + "-- Result' l $ \n", 134 + "-- maybe \n", 135 + "-- v \n", 136 + "-- ((`IM.union` v) . IM.singleton c) \n", 137 + "-- (liftA2 (liftA2 (*)) (!! a) (!! b) l) \n", 138 + "-- _ -> Unknown')\n", 139 + " \n", 140 + "haltParser :: Parser' (Input -> Output)\n", 112 141 "haltParser = Parser' (\n", 113 142 " \\case\n", 114 143 " l@(o : _) | o == 99 -> Halt' (IM.fromList $ itoList l)\n", 115 144 " _ -> Unknown')\n", 116 145 " \n", 117 146 "\n", 118 - "(|||) :: Parser' a -> Parser' a -> Parser' a\n", 119 - "(Parser' p1) ||| (Parser' p2) = Parser' (\\i -> let v = p1 i in bool v (p2 i) (isError v))\n", 147 + "\n", 148 + "\n", 149 + "\n", 150 + "\n", 151 + "\n", 120 152 " " 121 153 ] 122 154 }, 123 155 { 124 156 "cell_type": "code", 125 - "execution_count": null, 157 + "execution_count": 14, 126 158 "metadata": {}, 127 159 "outputs": [ 128 160 { 129 161 "data": { 130 162 "text/plain": [ 131 - "Result' [5,99] (fromList [(0,2),(1,2),(2,3),(3,4),(4,12),(5,99)])" 163 + "Result' [99,30,40,50] [fromList [(0,1),(1,9),(2,10),(3,70),(4,2),(5,3),(6,11),(7,0),(8,99),(9,30),(10,40),(11,50)],fromList [(0,150),(1,9),(2,10),(3,3),(4,2),(5,3),(6,11),(7,0),(8,99),(9,30),(10,40),(11,50)]]" 132 164 ] 133 165 }, 134 166 "metadata": {}, ··· 136 168 } 137 169 ], 138 170 "source": [ 139 - "parse' (addParser ||| prodParser ||| haltParser) [2,2,3,4,5,99]" 171 + "let d = [1,9,10,3,2,3,11,0,99,30,40,50]\n", 172 + "fmap (flip Papa.sequence d) $ parse' (list (addParser ||| prodParser ||| haltParser)) d" 173 + ] 174 + }, 175 + { 176 + "cell_type": "code", 177 + "execution_count": 70, 178 + "metadata": {}, 179 + "outputs": [ 180 + { 181 + "data": { 182 + "text/html": [ 183 + "<style>/* Styles used for the Hoogle display in the pager */\n", 184 + ".hoogle-doc {\n", 185 + "display: block;\n", 186 + "padding-bottom: 1.3em;\n", 187 + "padding-left: 0.4em;\n", 188 + "}\n", 189 + ".hoogle-code {\n", 190 + "display: block;\n", 191 + "font-family: monospace;\n", 192 + "white-space: pre;\n", 193 + "}\n", 194 + ".hoogle-text {\n", 195 + "display: block;\n", 196 + "}\n", 197 + ".hoogle-name {\n", 198 + "color: green;\n", 199 + "font-weight: bold;\n", 200 + "}\n", 201 + ".hoogle-head {\n", 202 + "font-weight: bold;\n", 203 + "}\n", 204 + ".hoogle-sub {\n", 205 + "display: block;\n", 206 + "margin-left: 0.4em;\n", 207 + "}\n", 208 + ".hoogle-package {\n", 209 + "font-weight: bold;\n", 210 + "font-style: italic;\n", 211 + "}\n", 212 + ".hoogle-module {\n", 213 + "font-weight: bold;\n", 214 + "}\n", 215 + ".hoogle-class {\n", 216 + "font-weight: bold;\n", 217 + "}\n", 218 + ".get-type {\n", 219 + "color: green;\n", 220 + "font-weight: bold;\n", 221 + "font-family: monospace;\n", 222 + "display: block;\n", 223 + "white-space: pre-wrap;\n", 224 + "}\n", 225 + ".show-type {\n", 226 + "color: green;\n", 227 + "font-weight: bold;\n", 228 + "font-family: monospace;\n", 229 + "margin-left: 1em;\n", 230 + "}\n", 231 + ".mono {\n", 232 + "font-family: monospace;\n", 233 + "display: block;\n", 234 + "}\n", 235 + ".err-msg {\n", 236 + "color: red;\n", 237 + "font-style: italic;\n", 238 + "font-family: monospace;\n", 239 + "white-space: pre;\n", 240 + "display: block;\n", 241 + "}\n", 242 + "#unshowable {\n", 243 + "color: red;\n", 244 + "font-weight: bold;\n", 245 + "}\n", 246 + ".err-msg.in.collapse {\n", 247 + "padding-top: 0.7em;\n", 248 + "}\n", 249 + ".highlight-code {\n", 250 + "white-space: pre;\n", 251 + "font-family: monospace;\n", 252 + "}\n", 253 + ".suggestion-warning { \n", 254 + "font-weight: bold;\n", 255 + "color: rgb(200, 130, 0);\n", 256 + "}\n", 257 + ".suggestion-error { \n", 258 + "font-weight: bold;\n", 259 + "color: red;\n", 260 + "}\n", 261 + ".suggestion-name {\n", 262 + "font-weight: bold;\n", 263 + "}\n", 264 + "</style><span class='get-type'>Papa.sequence [(+1)] 2 :: forall t. Num t => [t]</span>" 265 + ], 266 + "text/plain": [ 267 + "Papa.sequence [(+1)] 2 :: forall t. Num t => [t]" 268 + ] 269 + }, 270 + "metadata": {}, 271 + "output_type": "display_data" 272 + } 273 + ], 274 + "source": [ 275 + ":t Papa.sequence [(+1)] 2" 140 276 ] 141 277 } 142 278 ],