{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "{-# LANGUAGE NegativeLiterals #-}\n", "{-# LANGUAGE FlexibleContexts #-}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import Text.Parsec hiding (State)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "instructionLine = many (up <|> down)\n", "\n", "up = char '(' *> pure 1\n", "down = char ')' *> pure -1" ] }, { "cell_type": "code", "execution_count": 4, "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": 5, "metadata": {}, "outputs": [], "source": [ "part1 :: [Int] -> IO ()\n", "part1 instructions = do\n", " print $ sum instructions" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "part2 :: [Int] -> IO ()\n", "part2 instructions = do\n", " print $ length $ takeWhile (>= 0) $ scanl (+) 0 instructions" ] }, { "cell_type": "code", "execution_count": 7, "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": 8, "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 }