this repo has no description
0
fork

Configure Feed

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

aoc19 day02 sol

+59 -141
+58 -141
aoc-2019/AOC19-02.ipynb
··· 19 19 "source": [ 20 20 "import Papa\n", 21 21 "import Control.Monad.State\n", 22 + "import Control.Monad.State.Class\n", 22 23 "import qualified Prelude as P (read, undefined)\n", 23 24 "import qualified Data.IntMap as IM\n", 24 25 "import Data.Monoid\n", ··· 28 29 }, 29 30 { 30 31 "cell_type": "code", 31 - "execution_count": 15, 32 + "execution_count": 3, 32 33 "metadata": {}, 33 34 "outputs": [], 34 35 "source": [ ··· 91 92 "list :: Parser' a -> Parser' [a]\n", 92 93 "list p = list1 p ||| pure []\n", 93 94 "\n", 94 - "listt :: Parser' a -> Parser' [a]\n", 95 - "listt = P.undefined\n", 96 - "\n", 97 - "\n", 98 - "addParser :: Parser' (Input -> Output)\n", 99 - "addParser = Parser' (\n", 95 + " \n", 96 + "addParser' :: Parser' (State Output ())\n", 97 + "addParser' = Parser' (\n", 100 98 " \\case\n", 101 - " l@(o : a : b : c : xs) | o == 1 -> \n", 102 - " Result' xs $ \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", 110 - " _ -> Unknown')\n", 99 + " l@(o : a : b : c : xs) | o == 1 ->\n", 100 + " Result' xs (modify (\\s -> \n", 101 + " IM.update \n", 102 + " ((liftA2 (+) (s IM.!? a) (s IM.!? b) <|>) . Just) \n", 103 + " c\n", 104 + " s\n", 105 + " ))\n", 106 + " _ -> Unknown'\n", 107 + " )\n", 111 108 " \n", 112 - "prodParser :: Parser' (Input -> Output)\n", 113 - "prodParser = Parser' (\n", 109 + "prodParser' :: Parser' (State Output ())\n", 110 + "prodParser' = Parser' (\n", 114 111 " \\case\n", 115 - " l@(o : a : b : c : xs) | o == 2 -> \n", 116 - " Result' xs $ \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", 112 + " l@(o : a : b : c : xs) | o == 2 ->\n", 113 + " Result' xs (modify (\\s -> \n", 114 + " IM.update \n", 115 + " ((liftA2 (*) (s IM.!? a) (s IM.!? b) <|>) . Just) \n", 116 + " c\n", 117 + " s\n", 118 + " ))\n", 119 + " _ -> Unknown'\n", 120 + " )\n", 126 121 " \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 122 " \n", 140 - "haltParser :: Parser' (Input -> Output)\n", 141 - "haltParser = Parser' (\n", 123 + "haltParser' :: Parser' (State Output ())\n", 124 + "haltParser' = Parser' (\n", 142 125 " \\case\n", 143 126 " l@(o : _) | o == 99 -> Halt' (IM.fromList $ itoList l)\n", 144 127 " _ -> Unknown')\n", 145 128 " \n", 146 129 "\n", 147 - "\n", 148 - "\n", 149 - "\n", 150 - "\n", 151 - "\n", 152 - " " 130 + " \n", 131 + "extract :: Input -> ParseResult' [State Output ()] -> Output\n", 132 + "extract i p = let ii = IM.fromList $ itoList i in \n", 133 + " case fmap (foldl' (flip execState) ii) p of\n", 134 + " Result' _ a -> a\n", 135 + " _ -> IM.empty" 153 136 ] 154 137 }, 155 138 { 156 139 "cell_type": "code", 157 - "execution_count": 14, 140 + "execution_count": 21, 158 141 "metadata": {}, 159 142 "outputs": [ 160 143 { 161 144 "data": { 162 145 "text/plain": [ 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)]]" 146 + "Just 3790645" 164 147 ] 165 148 }, 166 149 "metadata": {}, ··· 168 151 } 169 152 ], 170 153 "source": [ 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" 154 + "let d = [1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,6,23,2,23,13,27,1,27,5,31,2,31,10,35,1,9,35,39,1,39,9,43,2,9,43,47,1,5,47,51,2,13,51,55,1,55,9,59,2,6,59,63,1,63,5,67,1,10,67,71,1,71,10,75,2,75,13,79,2,79,13,83,1,5,83,87,1,87,6,91,2,91,13,95,1,5,95,99,1,99,2,103,1,103,6,0,99,2,14,0,0]\n", 155 + "let dd = d & ix 1 .~ 12 & ix 2 .~ 2\n", 156 + "\n", 157 + "let resParser = parse' (list (prodParser' ||| addParser' ||| haltParser'))\n", 158 + "\n", 159 + "liftA2 (IM.lookup 0 .) extract resParser dd" 173 160 ] 174 161 }, 175 162 { 176 163 "cell_type": "code", 177 - "execution_count": 70, 164 + "execution_count": 62, 178 165 "metadata": {}, 179 166 "outputs": [ 180 167 { 181 168 "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 169 "text/plain": [ 267 - "Papa.sequence [(+1)] 2 :: forall t. Num t => [t]" 170 + "Just 6577" 268 171 ] 269 172 }, 270 173 "metadata": {}, ··· 272 175 } 273 176 ], 274 177 "source": [ 275 - ":t Papa.sequence [(+1)] 2" 178 + "let anvs = [(noun,verb)| noun <- [0..99], verb <- [0..99]]\n", 179 + "\n", 180 + "let mkInp n v = d & ix 1 .~ n & ix 2 .~ v\n", 181 + "\n", 182 + "f (a : n : v : _) | a == 19690720 = True\n", 183 + "f _ = False\n", 184 + "\n", 185 + "g (_ : n : v : _) = Just $ 100 * n + v\n", 186 + "g _ = Nothing\n", 187 + "\n", 188 + "\n", 189 + "g =<< (find f $ \n", 190 + " (liftA2 (toListOf (taking 3 traverse) .) extract resParser) \n", 191 + " <$> \n", 192 + " (uncurry mkInp <$> anvs))" 276 193 ] 277 194 } 278 195 ],
+1
aoc-2019/data/day02
··· 1 + 1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,6,23,2,23,13,27,1,27,5,31,2,31,10,35,1,9,35,39,1,39,9,43,2,9,43,47,1,5,47,51,2,13,51,55,1,55,9,59,2,6,59,63,1,63,5,67,1,10,67,71,1,71,10,75,2,75,13,79,2,79,13,83,1,5,83,87,1,87,6,91,2,91,13,95,1,5,95,99,1,99,2,103,1,103,6,0,99,2,14,0,0