From 302ba8466294fff53aefb49f4ef0503ecbad88d2 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 29 Nov 2017 08:20:50 +0000 Subject: [PATCH] Got IHaskell working --- .gitignore | 4 +- adventofcode1701/app/advent01.ipynb | 127 ++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 adventofcode1701/app/advent01.ipynb diff --git a/.gitignore b/.gitignore index 6d84b97..593051d 100644 --- a/.gitignore +++ b/.gitignore @@ -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 index 0000000..2fe0e24 --- /dev/null +++ b/adventofcode1701/app/advent01.ipynb @@ -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 +} -- 2.34.1