From: Neil Smith Date: Sun, 10 Dec 2017 12:34:14 +0000 (+0000) Subject: Day 9 X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-17.git;a=commitdiff_plain;h=8c21e88d45032e1836e31e6a3a40e17e575df4ad Day 9 --- diff --git a/advent-of-code.cabal b/advent-of-code.cabal index cd1f29a..347896b 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -88,4 +88,10 @@ executable advent08 , parsec , parsec-numbers , containers - \ No newline at end of file + +executable advent09 + hs-source-dirs: src/advent09 + main-is: advent09.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + \ No newline at end of file diff --git a/data/advent09.txt b/data/advent09.txt new file mode 100644 index 0000000..d508170 --- /dev/null +++ b/data/advent09.txt @@ -0,0 +1 @@ +{{{{{{{{ua,a,!>{!>!!!>a>,,!!!!,!!!"!,!a!e}!>!>,},,}},{{<}eaa'},<<>},<}},}}},{{{{}}},{{{<,!>,!!!>!!!>!>!>a!!,!!',>},<'!<,>},{{{i'u">}},!!eo!!""'!>,!!,,>}}},{{{{{{!>,},<>}}},{{!!!!,},<{!!!>>},{<>}}},{{{<},!!'>}},{{},{!>},'>}},{{{,}!!!>u>},<'a,!>,e!>!u>},{{{{},e!""!!'e">},<{e!!!>!}'}o!>,<,o!>},,},},},<}}<'o}!>,<'!!!>'>},{<{e>,{}}}},{{},{}}},{{,!>e"'!>},>},<,!>}}<,{!e>}},{{{},},{}au!>},,{}},{i!"!!!>a,uu!ao>},{,{e!!!>},!!!>!ei!!io}!>},<"!!o!}>,"!!!o!>,,},<>}},{},{{{{{<"!!!!!>>}}}},{<{!>},<}i'!!i!>!'},{{ioi"u>}}}}}},{{{<{"'e},>}},{{{{u!!!>ao'a>},{}},},!>},"!>!>},,},{{{<}',"'>},{{}}},<{<>},{<{!!"}!!!>!!!>{!,"!>},,ae}'>,!!!>},,}}},{{{{},i!!!>},}},{{{<"iao,}>},{{{}},{{{}},{},},>}},{{<>,{o"!>!>},<,ea>}},{{},{}},{!>},},<{!e,!>},<},e>,"!!u{u{!>,!>,<>}}}},{{{{},<<"!!!>},}e},,},a{{!!!>,<{!a}ua<',!!!o!!!>e,!!!>>},},}}},{{{,<}!>{!>o!>},'}},{"!!ou!!o>,{},},<>}}},{{}}},{{{<"!eui!>},!>o!!!}>,{!!!!!>!i!'!!e>}},{{i!>,<,""}a}{!!!>{u!>,},,a!!!>,>},{{{,<}>,!!!!!>i>},{<,a!{{{!>,!!a!>},},{{},{}}},{{u!>'!>!!!>,o!>,a!!,},}},{{},<'!{'u!>,},,},<>}}},{{{'>},},{{}},{{a>,<<"e,u!!!'i!'!!!!!!}!>}i"aa>}}},{{{ai!!!>!!!>'!>},!>u"i>,}},{{{<,i!!!>!!e"au!!'"u>},{}},{}}}}},{{{{},{{<,!>},<"!>,,<!!!!"!>,,<>},<"<<{},!!!o,o!!!>!!!>!>},<>}},{{<{,{>},<"oi,!!e!!!>i!>,<{!>,},}},{{{<,{!>'i!>!!!>!!!!!>},<'i{u>},},uo!!o!>,,<>},{{{"u!!{>},{},<},<>}},{}}},{{},{{{{{<,,>},{}},{{!>!!e!>o!!!!!!!e'!!!>"u>}},{{'aa!!!>},!'}!!!>>}}}},{{{,<>},{!!o!>a,!>,<,!!!!>}}},{{{{{}},{{!o<,e!>,},},{{<!},>},{},<}!>,},>}}},{{<{}!>!>i!>,!!!>!!!!"{!>,<,'!>},<>,{u"!{!!!!}>}},{<{>,{},}},{},}}},{{{{},<>},{{},,},a>}}},{{,},<'e>},{>}}},{{,<'{,!{!!!!!!}}e!!{},{{<,o"e!>},},},}}},{{{}!!'a"}}}}},{{},{<,!>},<>}},{{{},{<>}},{u!!a>}},{{},{,<}o!!!>!!!>>},{{{<<,,!!!>!!!>!>},i!!!>o!!!>'!>},>}}}}}},{{{},a{e!!!>!!!!!>>,,},,},{},<,>}},{,<>,!>},<'i'>},{{'},},},,}}}},{{{{},{{{}},,!!!!}!!!>'},>}},{<,!!!!!a!>},,>,<>},{<>}},{{,<!>},<{!!!>,{!!!!o!u",!!!>'o!!!!a!'!!,>},{<}!>,,!!!>u!!!!!>!>},a!!!>!>>}},{{{},{!>},,e}<>,<}{uuo"!!o!e>}},{{{<}!>"!!!>{o!!!!<>},{},{{}}},{},{{<{!!!>eo!>},<,>},,<{iu{!,!!oi!!"!{'!!>}}}}},{{{{e,"!!e!>!!,'!>,<>},{{{<},u>},{",,<>}}}},{{,i!!a!>},>},{{<<''o!>!!!>>}}},{{{ao>},{<,,{!!>,{<},,o}!!"ioo!>,<}!!,o}i!>,<,{!!!!!!!>!>},<,{!!ia!>,!!!>a!!{}o>}}},{,!!o>}},{{{{{<{o!!e'}{,}},{{!>},}'o{!'{>},,<"!!{>}},{{!!o!!!>},<"!>a{!>},<'!>},},,{<}!>},,},!>},i!!!>>}},{<}ia"!!!>o,<",!,>}}},{{,<,<>,<}a},},<'!e>},{},},{{}}},{{}},{{{{}},{{<<<,!!o!!!!e!!!>}!!!>!>,},}},{{<,oe!a}!>>},{<{!>},!>!!o!!!>u'"!>,>}}},{{{<<'!>},!!!!!u!{e!!i!>,},<'!!}!>,},,e>},{<"!!!>,},}'!!"!>},,e!}oe>}},{<">}}}}},{{{,a>},{!>,{!>,,<>}},{{{{!>,<>},{{<">}}},{{{<,i!!!>}!>,!>,},},''!>>},{}},{{,<{!!!>'!ai!!!>,<>}}},{{{{{!!!>!>,{!}'o!>!!ui}>}},{{!!!>,},!uu!a!>},<>}}},{<o!>,,<}o'!>>,<}!,,},!>},}},{{,<'!>,o!'o>,{}},{}},{{<,!>},<"!!!!!>''>},{{,,!!!!!>},,>}},{{},,!!'!>},},,<<}}},{{{<},<,<"oai,!>>},{<'ue!!!>{!>},}}},{},{{{},{}},{,},<{o!!">,<>}}},{{{}},{{},{<,u!!'!>!>!>},<}!!e!{!!u!!!>},!>},<,>}},{<>}},{{{,}!!!>!''!>!!>},!!!>},},{'>,u{'!!,'>},{}}},{{{{{{},{}}}},{}}},{{{{{!>},!!u!!{{e<'!>},<'!>!>},<>},{<"o!>,,,}}}}},{{{{!>,,}},{<'au!>},<{{aaiu!!>},{i"!!iiiu}>}},{},{{<}!!',},<>,{}}},{{{{iie!o"!!,!>,,,<>},{e!>'>}},{}},{{<'!>},<'!!}!!a!>},e>}},{{<}i"'!>,!>},!!,!!!!!>,},{{,{<"i!>},'euu!!!>,<,>}}}}}}},{{{{{{<'u!}!>!>},}}},{{{,{!>,<,{oee}},}}},{{{,ai!>},<""e!!!>!>,},},>},{aui!>},}"}!>,<,!!!>!>},!!>},{{{}},{!ue"a,!!!!!>e!!,!>!>,<<>}}},{{{,<"!!{,},<},<>},{}},{{},,<}!>,e!!">},{{!o!!uu!,!>,},{!>,!>,,<,o!!!>!>!!!>e{>}}},{{{o!!!>}oa"!!!>!a"!{>,},{{<'>},{!!!a!!ia!,!>,u}},{{}}},{{!u!!!>"!>,>}}}},{{{{}},{{{,}}},{{{!>},!>},}},},},,o{!>>}},{<"e!>,ia"!>},}},{{}}},{{{{{!!!>},},,{<'oo!!!>!>,!u!>},<>}},<"},!!!>,!>!!!>!!!i,>},{{<{!!u">,<{,{!>},<{!}'>},{{<,<>},{}},{,<<>}},{{{{,},<"e,}},{{},},}},,}},{{<",{<>}},{<>,},<"!!!!!>!"{{"''au!!o,},{{!>!e,<>,<>},{<>}}}},{{{{{{<"}>},{{,},<},{},<'"}o,o!>},},}!>,}},{<{'e,a!>i!>,,}},{{},!>'a!{!>,,},},{{,<>}}},{{<'i!>,<,o,!!i'i!!e},}e{!!oo!!,!>},<,!>,},},}}},{{,{{},{<,>}}},{{,'!!u}!!,,u}},{{'o"{ou,'}>},>}}},{{},,!>,<"{u}},{{{},{{},<>}}},{{},{a<<}{""'!!!>>}}},{{<}!{'!!}}e,'e!!'}!>},},{{{,<{!!,<>},{,<>}}},{,<{!!!>,>,,},},}}},{{{{{},a!">,},<}>},{{},{>}}},{{},{,{}},{<""{!a>}}},{{{!!'!au',a!'aie'!>,<{o>,{<<<}},{{},{},<,<>}},{{<}i'!>,<}!!oa!!eeu!>},}}},{{},{{{{}},{!!}}}},{},<"{!!"},>,{},<,!>!"!>!!'>}}},{{{,},{,!!>},!!!>'a,"!>},<>}}},{{{<>}}}},{{{{{{<{!>},a>}},{},,'o'u>,{,<}!>!!}!!<"ui!>,},<,ou>}},{>,},<{!!,a!>,'!!!!!>"!o!>,a>}},{{,!o,'>,{},<'!,!>},}}},{{{,,i,!>},<,>},}}},{{{},{{!>},},<}!>!!!!i{>}},{<<"!>!>,<<,!!!>!!!"{a>,,>}},{{{{<>,{<{o{i'<{!!"i,>}}},{{,,<>}},{{{{},<'!!!>ue!o!>!!},<},}>},!>u<>},{{},{},,}},{{e}>,{,>}},{}}}},{{{,!!!>e>}}},{{<,oe>},{{<},,'!!'"!!!!!>>}},{{{}}}}},{{{},,!!}a"<'>,{},i!!o'e!!!a>}},{},i'!a!!,<'},{{<'!!!>},<}i{i!!!>!!{!!a,!,>}}},{{}}}}},{{{{,{!!!!!'u{!!"!!!}o!'aou!!!!!>a>,,}},{},,<'!!!>u!o{oo{'!!!>>,{}},{{}}},{{{<,},,},,}},{<"}{!>,,>},{{<!!,},},<<{!!>},{<,,},,}}}},{{<{o!!!!!>{!>,<}>},{{}}},{{<{!!!>,<!!!!u!>!!!>'i{,}u>}}},{{{{{},e!>},}},{<,,!'!>,ua>}},{{{<{!,!>,"!!!!,<>}},{{}}},{{{"!!!>},<>},{u'ea!>eu!u!!!>}}}},{{{{{},!u!!!>},<,{oi!>,<'!!!>,<>}},{{{{,<"u}!!">,{}}},{,<{!!!>!>!!uuo!>,<'!>,<>,},<,>},{}},{{<<}!!{!{!!>},{!ioaau!>!>},,{{e<,iei!!au!!!>},<>,'!""!!!>e{!!!>u>}}}},{}}}},{{{{<>},<}!!'!>,<,}'!>,u>},{!{o!uaa,!!!>{!!!!}!>},<>,{<,"{ae!>},},!!!>!!!>}e,>}}},{{{<<"!>'a'!!uu>},<,!>!>a!>!!>},{e!!!!!}uu"eo>,{}},{{},{{{},{}},{,<'!>e!}!!a}}}},{{},u!!!>e<{!!u">},{}},{{<,'i!!!>>,<"e<,'e{u!!!!o}"!""uu!!!!!>!>},<'o>},{{{}},},!!!>e,!!!>,},<,!!!>,}}}},{{{},{{}},{{{<>}},{<>}}},{{},{{{,},!>ei"e},{,,,}}}},{{{{{{{<"!>">}}},{{<{!,}!>!!!>,<"!>},},!>},<>,{{}}}},{{{<>,,,<{{}!!!!!!'>}},{},,>,{}},{{{<>,{{{u!!!>">},"!!!!!u!!!<,!!!>>}}},{!!a!>},<}u!!!>,!uo}"u!{!>!!<>}},{{,}}!>},<"e,>,{},,,<'",<>}},{},<}"e'a!>},,},},<'>}},{{{{},<<},,},{{},{}},{{}}}}}},{{{<"!!e!>},i>,},!>},<>},{},!>!>,>,{{{}}}}},{{{{{,}},{>}},{<{},!!iue''o!!!!!>!>},<>}},{{}},{e'!!,!!!>},<>}}}},{{<>}},{{{<'}{ae>},{,<<}!!!>a!>},,!!!>a!!o>,{},<<{a,e,,i}o!>,<>}}},{{}},{,<{!>},<>,{}}}},{{{{{},,},<{!",>},{!!u!!!!!>a!!!!!>,>,{!>,i}u!!!>!!!>,},}}},{{{{'!>,<,u!!!>},!!ao>},{{!!!u!!',a!!!!!>u!>,<{>}},{{{}}}}},{{}}},{{{,<"!>,<{>},{{!!i{>},{!!!>}!!a!{,e!>a!!,!!!>>}},{}}},{{{{{}}},{{},<,!>,<>,{}},,!{!!!>},>}},{{},{{},<{!>,},<}!!">,{}},,,<,o!>,},<,!!!>}"o,},<>}},{{{},,<>}}}}}}}},{{{{}}},{{},{,,,},<{ea!u!>a!!!><>,{{},,u{!!!>>}}},{a!>,<<{e{!>},<}}!>>}},{{,,{},a!!!>!!!>o!uio{o>}},{{!!o!!iu!<"}!!o{'!{ooio<>},}}},{}},{{{{<>},{{{},{},!iaa,}e!>!!o}a!!!>},<>,},,}}},{{<>,<>}}},{{{!>},<'!!!>!>i>}}}},{{}},{{{<}!!!>{!!>},{<'a,!{i!!,!>!!'!>}u>}},{!>,<">,{<<},},>}},{}},{{{!!!!!!<"},e}'!!{!!i>}},{{>,!>'!>,},},,,},{{},{}}},{,,{>}}}},{{{,}>},{{{}},{},},<>}},{{ao!>},<},,,,,},},<{>}}},{{{<}!>},<},<,!!!>ua!>!>,>},{{<'o>}},{{},>},!>,<"}},{{{{},{{},<>,{<>}},{{}},{}},{{{<>},,<>},{{{,"o!>,<,!!!>o'!!},{{{},{}},{}},{{!>},},<>}},{{!>",!!!>,>},{!>},<},<"{"<>},{}},{{},!>i!>'{ei!!!a!!!>}<>}},{{{{}},{!>},a!>},<"}!!,u,'{a!>},<{>,io!!},{{},{}}}}},{{{{{,<">,{<uo!>,<>}}}},{<},{{<}!!!>},},},<}>},{,{}}}},{{<}"o!},},<'u'!>},<<>},{{{e!>,}},{{!>o!!!>},<,{,a}},{{{<'!>}!!'u,!!!i!!u>}},{,!,>}}},{{{{}}},{{,a!!{},!!i"!>,!!iu!>},,<<,>}}},{{{{{},<'aae!o!!!>{aa,!>!!!>,<>},{,io<{u!!!!u>,{}}},{<}!!>},<}!>!>!>!!i,!>'!>,<,">,<{ioo!>},{}a'e"!o>},{}},{}},{{{{}},{{},{{}}},{{,{}}}},{{{}}},{},{{{!>,i!>},<,e!!!u},<>,{<{a!!!!'oi!!}!>},{>}}},{{{{,}},{{},<{!!<},,!!!>!>,},{{{{},{{!!!>!>,,,<'!>e!>!!ee,>}},{{{<,o!,,o>,{',>}},<"a!!!!{a!>},}}>},{{{<"',!a>},},<'!>,,<{i!>},!>,!'!!}<>},<{!o"o>},{{{{}},{{e!!!>!!!>i'e,,a}!>u">},{},,i!i!!!>!>,<,a!!'"",uu>}}},{{},{{a"}>},{{}}}}}}},{},{<'!oue!!!>oa!u!>,<}e!>{o>}},{{},{{{},<>}},{{<}!>},!!!>i,!!!>},e!!!!!>">}}}},{}}},{{{"!,!>,!!!>,!!!!!>!!!!!!!!'oa!!u{a"!!!>e>},{<{,!!!>!>,<,<"!!!>!>},}},{{<>},{{},<"!!!!!>'!>,>},{!>,,},'>}}}}},{{{{o!!!!e"!!",!!!!!!i!!!!!>!'>},{,<'!!!>>}},{}},{{{!ao!>,},i!>},<'!>,<>}},{{{},{{}}}},{{{<"!>,,<'>}}},{{},{<"!a!>},!!!>{!!!>},},{{},<'!!!>i!>,<',}}>},{}}}},{{},{{!!!>!i}''eu!>},>},{<{!!!>o'u'oa!!e,>}}},{{,u,!>!!!!!!!>!!!!o>,{<{!>},'e!!"!!e>}},{{},{}}}},{{{,",!>oo{!>},>},{'a!>,u}!!!!u">}}}},{{,<,!!",a!>},<>,{<{}oe!!!>}{!>},<,{'!>},,<>}},{{<'ao'a!!,!!>},{,<'e>}}},{{{<,}>}}}}}},{{{{{{{<{!>,{o>},{}},{},},}},{{<{{!!i{!>},!!!!!>},},<>,{,<'!>{!!!>"!}"!!e!!ii!>>}}},{{{{{}},{!,!>,,,<,>}}},{{<!!!>},<"'o!>,,,{}},{{!>},},},{}}},{{<<{>},{,!!i!!ea<}!!!!o{}a!>,<,}!>>}}}},{{<"a!>},,<}!!!!!>'eoi{!!}!>},<>},{!>},!>,<}",!!'!>!<>},{}},{{{,,iia>},{,a{u}ieu!!a}!!!!e!>},},u!!u!>},<>}},{{>,{<'>}}},{},<,!!o!!!>!!a!!!>!!}>}},{{},{{}}}},{}},{{{},,<,!>,!!e!>},,<>,{u!>,!!!>!e>}}},{<,!e{aaoui>,{<,}i'i"}"ou>}}},{{{<>},{!!!!!>}}}eu>}},{},{{{},<!!o!>,<',!!!>u!!!>!!'{!>'!!,e>},{}}}}},{{{<}!>,!!!>},<>},{{{}},{{<{,!>u"!!a"',<}!!!>u!!!!!>a!>}},{,>}}},{}},{{{},{{{}},{{{!!,">}},{},{{",,<">},{,!'!!!>u'!>,<,!>,}}}},{<"iii!>!!!>},<,},,ui">,<}},{},,{!!!!!!!!}e<>}},{{<'}i!>},ue!!'oa>},>}}}},{{{<'{!>,,<"!>},<>},},<}eo!!!>ai!>},,!a"}>},{{<}o!>ao"!}<<>},{{<>}}}}},{{{{{!>},},{<{>}},{}},{{{},}}},{{},!>,},<<}!>},},<"}a>},{<,!!!>}!>},},u,<'u!>,,>}},{{{{{<"e!!!!!!u!!!>!!"!!!>"!}!oo!!!>,<>},{},,}},{{},{}}},{{{<{'!!<}!>,<,i,!>},,,<}!!!!o>},!e}a!e!>}!!o>},{},<}}!>},<}{!!>},{io"'{!,<>,,}}!!!>,,}},{{{{},{<{!!!!!>!!u'!!!!!!!<}e"a}>}},{},io!!!!!>},<,e,}!>!>!!e}!>},,{{!!!!,{!!ee!>,<,!!>}}},{},<,!>},<>,{{<{!>,<{"a}!!!>!!!>!!a!!ai!u>,{}}}}},{{{{!}!!a!!>,u!!!>!!,o!!,,<"!>,,{}!>},}},{{}}},{{{},,{},!!e,e!!u!u,i!!i,>,{}}},{{{,<'i!>},<{e!{!!i>}},{}},{}},{e>,{{},<,!>{!!!>!a!}>}},{{},{{,!>,,!"!!a!!!>!}}}},{{<"ioe!>,!!i{!!!>,!!o>}},{{{},{}},{<>,{<'!!'!>!i>}},{{,,a,!!!>>}}}}}},{{<"'",<'>},{{",!>},!!!>!>,,,<{{}i"a{!!!!!!!>,<}o,>},},{{<{!>,}}},{},{{<'uo!>ui{!!!>!>,,{,}},{{},{}}}},{{{{{{},{}},{}},{{e!!o!!!>ue"{!!!>!!!>!>>}}},{{<>,{}},{{,o}e"}>},{}}}},{{{<},!>,uu!!'}!!!>o!!}<{!!!>,,{{{},{{},{<},},<>}}}}},{{!>o!!!>!>,<<,!"!!}">}},{{},{}}},{!!!>!!!>!>},},>},{{<'oa{!u}!o!!e!>},}}}}},{{{{{},,<'>}},{}},{}},{{{},,},<<,},{,>,<{!!!>,<'!!!>},<"!>"}!>},,!!!!!e!!!!,!>,<>},{{,<>}}},{{!>},<<>},{{<}!>},<'>}}}}}} \ No newline at end of file diff --git a/src/advent09/advent09.hs b/src/advent09/advent09.hs new file mode 100644 index 0000000..d7383b9 --- /dev/null +++ b/src/advent09/advent09.hs @@ -0,0 +1,46 @@ +data ParseState = ParseState + { total :: Int + , depth :: Int + , garbageCount :: Int + , readingGarbage :: Bool + , ignoreCharacter :: Bool + } deriving (Show, Eq) + +main :: IO () +main = do + text <- readFile "data/advent09.txt" + print $ part1 text + print $ part2 text + + +part1 :: String -> Int +part1 = total . process + +part2 :: String -> Int +part2 = garbageCount . process + + +process :: String -> ParseState +process = foldl parse ps0 + where ps0 = ParseState {total = 0, depth = 0, garbageCount = 0, + readingGarbage = False, ignoreCharacter = False} + +parse :: ParseState -> Char -> ParseState +parse ps c + | ignoreCharacter ps = ps {ignoreCharacter = False} + | c == '!' = ps {ignoreCharacter = True} + | readingGarbage ps = if c == '>' + then ps {readingGarbage = False} + else ps {garbageCount = garbageCount ps + 1} + | otherwise = + case c of + '<' -> ps {readingGarbage = True} + '{' -> openGroup ps + '}' -> closeGroup ps + _ -> ps + +openGroup :: ParseState -> ParseState +openGroup ps = ps {depth = depth ps + 1} + +closeGroup :: ParseState -> ParseState +closeGroup ps = ps {total = total ps + depth ps, depth = depth ps - 1} 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 +}