X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=src%2Fadvent09%2Fadvent09.ipynb;fp=src%2Fadvent09%2Fadvent09.ipynb;h=11058b3252044767b1a53a14ceec2f4560ec8962;hb=8c21e88d45032e1836e31e6a3a40e17e575df4ad;hp=0000000000000000000000000000000000000000;hpb=51a92db02eaf7bea8f14f9e6a2b01223b606de69;p=advent-of-code-17.git diff --git a/src/advent09/advent09.ipynb b/src/advent09/advent09.ipynb new file mode 100644 index 0000000..11058b3 --- /dev/null +++ b/src/advent09/advent09.ipynb @@ -0,0 +1,333 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "{-# LANGUAGE NegativeLiterals #-}\n", + "{-# LANGUAGE FlexibleContexts #-}" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "data ParseState = ParseState\n", + " { total :: Int\n", + " , depth :: Int\n", + " , garbageCount :: Int\n", + " , readingGarbage :: Bool\n", + " , ignoreCharacter :: Bool\n", + " } deriving (Show, Eq)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "openGroup ps = ps {depth = depth ps + 1}\n", + "\n", + "closeGroup ps = ps {total = total ps + depth ps, depth = depth ps - 1}" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "-- parse ps c = if ignoreCharacter ps\n", + "-- then ps {ignoreCharacter = False}\n", + "-- else if readingGarbage ps \n", + "-- then if c == '>'\n", + "-- then ps {readingGarbage = False}\n", + "-- else ps\n", + "-- else case c of '<' -> ps {readingGarbage = True}\n", + "-- '{' -> openGroup ps\n", + "-- '}' -> closeGroup ps\n", + "-- _ -> ps" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "parse ps c \n", + " | ignoreCharacter ps = ps {ignoreCharacter = False}\n", + " | c == '!' = ps {ignoreCharacter = True}\n", + " | readingGarbage ps = if c == '>'\n", + " then ps {readingGarbage = False}\n", + " else ps {garbageCount = garbageCount ps + 1}\n", + " | otherwise = \n", + " case c of \n", + " '<' -> ps {readingGarbage = True}\n", + " '{' -> openGroup ps\n", + " '}' -> closeGroup ps\n", + " _ -> ps" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "process = foldl parse ps0\n", + " where ps0 = ParseState {total = 0, depth = 0, garbageCount = 0,\n", + " readingGarbage = False, ignoreCharacter = False}" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "score \"{{},{},{},{}}\"" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "score \"{{},{},{},{}}\"" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "score \"{{},{},{},{}}\"" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [], + "source": [ + "part1 = total . process" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "part2 = garbageCount . process" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " text <- readFile \"../../data/advent09.txt\"\n", + " print $ part1 text\n", + " print $ part2 text" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11089\n", + "5288" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "main" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "17" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"<<<<>\"" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"<{!>}>\"" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \">\"" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"<{o\\\"i!a,<{i\"" + ] + }, + { + "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 +}