Got IHaskell working
authorNeil Smith <neil.git@njae.me.uk>
Wed, 29 Nov 2017 08:20:50 +0000 (08:20 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Wed, 29 Nov 2017 08:20:50 +0000 (08:20 +0000)
.gitignore
adventofcode1701/app/advent01.ipynb [new file with mode: 0644]

index 6d84b977ec24f55fafa2caea8a1ff8c8f24df32d..593051d2c8874881144556dbc96a344bca67b625 100644 (file)
@@ -25,10 +25,8 @@ cabal.sandbox.config
 cabal.project.local
 .HTF/
 
-# A semelance of purity!
-# IPython
+# IPython / IHaskell notebook checkpoints
 .ipynb*
-*.ipynb
 
 # Sublime text
 *.sublime-workspace
diff --git a/adventofcode1701/app/advent01.ipynb b/adventofcode1701/app/advent01.ipynb
new file mode 100644 (file)
index 0000000..2fe0e24
--- /dev/null
@@ -0,0 +1,127 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "{-# LANGUAGE NegativeLiterals #-}\n",
+    "{-# LANGUAGE FlexibleContexts #-}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import Text.Parsec hiding (State)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 19,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "instructionLine = many (up <|> down)\n",
+    "\n",
+    "up   = char '(' *> pure  1\n",
+    "down = char ')' *> pure -1"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 20,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "parseIline :: String -> Either ParseError [Int]\n",
+    "parseIline input = parse instructionLine \"(unknown)\" input\n",
+    "\n",
+    "successfulParse :: Either ParseError [a] -> [a]\n",
+    "successfulParse (Left _) = []\n",
+    "successfulParse (Right a) = a"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 21,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "part1 :: [Int] -> IO ()\n",
+    "part1 instructions = do\n",
+    "        print $ sum instructions"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 25,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "part2 :: [Int] -> IO ()\n",
+    "part2 instructions = do\n",
+    "        print $ length $ takeWhile (>= 0) $ scanl (+) 0 instructions"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "main :: IO ()\n",
+    "main = do \n",
+    "        text <- readFile \"../../data/advent01.txt\"\n",
+    "        let instructions = successfulParse $ parseIline text\n",
+    "        part1 instructions\n",
+    "        part2 instructions"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 26,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "138\n",
+       "1771"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "main"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Haskell",
+   "language": "haskell",
+   "name": "haskell"
+  },
+  "language_info": {
+   "codemirror_mode": "ihaskell",
+   "file_extension": ".hs",
+   "name": "haskell",
+   "version": "8.0.2"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}