Got IHaskell working
[advent-of-code-17.git] / adventofcode1701 / app / advent01.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 6,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
11 ]
12 },
13 {
14 "cell_type": "code",
15 "execution_count": 7,
16 "metadata": {},
17 "outputs": [],
18 "source": [
19 "import Text.Parsec hiding (State)"
20 ]
21 },
22 {
23 "cell_type": "code",
24 "execution_count": 19,
25 "metadata": {},
26 "outputs": [],
27 "source": [
28 "instructionLine = many (up <|> down)\n",
29 "\n",
30 "up = char '(' *> pure 1\n",
31 "down = char ')' *> pure -1"
32 ]
33 },
34 {
35 "cell_type": "code",
36 "execution_count": 20,
37 "metadata": {},
38 "outputs": [],
39 "source": [
40 "parseIline :: String -> Either ParseError [Int]\n",
41 "parseIline input = parse instructionLine \"(unknown)\" input\n",
42 "\n",
43 "successfulParse :: Either ParseError [a] -> [a]\n",
44 "successfulParse (Left _) = []\n",
45 "successfulParse (Right a) = a"
46 ]
47 },
48 {
49 "cell_type": "code",
50 "execution_count": 21,
51 "metadata": {},
52 "outputs": [],
53 "source": [
54 "part1 :: [Int] -> IO ()\n",
55 "part1 instructions = do\n",
56 " print $ sum instructions"
57 ]
58 },
59 {
60 "cell_type": "code",
61 "execution_count": 25,
62 "metadata": {},
63 "outputs": [],
64 "source": [
65 "part2 :: [Int] -> IO ()\n",
66 "part2 instructions = do\n",
67 " print $ length $ takeWhile (>= 0) $ scanl (+) 0 instructions"
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": 23,
73 "metadata": {},
74 "outputs": [],
75 "source": [
76 "main :: IO ()\n",
77 "main = do \n",
78 " text <- readFile \"../../data/advent01.txt\"\n",
79 " let instructions = successfulParse $ parseIline text\n",
80 " part1 instructions\n",
81 " part2 instructions"
82 ]
83 },
84 {
85 "cell_type": "code",
86 "execution_count": 26,
87 "metadata": {},
88 "outputs": [
89 {
90 "data": {
91 "text/plain": [
92 "138\n",
93 "1771"
94 ]
95 },
96 "metadata": {},
97 "output_type": "display_data"
98 }
99 ],
100 "source": [
101 "main"
102 ]
103 },
104 {
105 "cell_type": "code",
106 "execution_count": null,
107 "metadata": {},
108 "outputs": [],
109 "source": []
110 }
111 ],
112 "metadata": {
113 "kernelspec": {
114 "display_name": "Haskell",
115 "language": "haskell",
116 "name": "haskell"
117 },
118 "language_info": {
119 "codemirror_mode": "ihaskell",
120 "file_extension": ".hs",
121 "name": "haskell",
122 "version": "8.0.2"
123 }
124 },
125 "nbformat": 4,
126 "nbformat_minor": 2
127 }