--- /dev/null
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 5 - Advent of Code 2018</title>
+<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
+<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
+<link rel="stylesheet" type="text/css" href="/static/style.css?17"/>
+<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
+<link rel="shortcut icon" href="/favicon.png"/>
+</head><!--
+
+
+
+
+Oh, hello! Funny seeing you here.
+
+I appreciate your enthusiasm, but you aren't going to find much down here.
+There certainly aren't clues to any of the puzzles. The best surprises don't
+even appear in the source until you unlock them for real.
+
+Please be careful with automated requests; I'm not Google, and I can only take
+so much traffic. Please be considerate so that everyone gets to play.
+
+If you're curious about how Advent of Code works, it's running on some custom
+Perl code. Other than a few integrations (auth, analytics, ads, social media),
+I built the whole thing myself, including the design, animations, prose, and
+all of the puzzles.
+
+The puzzles are most of the work; the easiest ones take 3-4 hours each, but the
+harder ones take 6-8 hours, and a few even longer than that. A lot of effort
+went into building this thing - I hope you're enjoying playing it as much as I
+enjoyed making it for you!
+
+If you'd like to hang out, I'm @ericwastl on Twitter.
+
+- Eric Wastl
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+-->
+<body>
+<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2018/about">[About]</a></li><li><a href="/2018/events">[Events]</a></li><li><a href="https://teespring.com/adventofcode" target="_blank">[Shop]</a></li><li><a href="/2018/settings">[Settings]</a></li><li><a href="/2018/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2018/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">10*</span></div></div><div><h1 class="title-event"> <span class="title-event-wrap">int y=</span><a href="/2018">2018</a><span class="title-event-wrap">;</span></h1><nav><ul><li><a href="/2018">[Calendar]</a></li><li><a href="/2018/support">[AoC++]</a></li><li><a href="/2018/sponsors">[Sponsors]</a></li><li><a href="/2018/leaderboard">[Leaderboard]</a></li><li><a href="/2018/stats">[Stats]</a></li></ul></nav></div></header>
+
+<div id="sidebar">
+<div id="sponsor"><div class="quiet">Our <a href="/2018/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://www.novetta.com/our-story/" target="_blank" onclick="if(ga)ga('send','event','sponsor','click',this.href);" rel="noopener">Novetta</a> - TS/SCI engineers & data scientists developing mission-critical disruptive technologies by day, helping Santa by night.</div></div>
+</div><!--/sidebar-->
+
+<main>
+<article class="day-desc"><h2>--- Day 5: Alchemical Reduction ---</h2><p>You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.</p>
+<p>While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long <a href="https://en.wikipedia.org/wiki/Polymer">polymers</a> (one of which is <span title="I've always wanted a polymer!">available</span> as your puzzle input).</p>
+<p>The polymer is formed by smaller <em>units</em> which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, <code>r</code> and <code>R</code> are units with the same type but opposite polarity, whereas <code>r</code> and <code>s</code> are entirely different types and do not react.</p>
+<p>For example:</p>
+<ul>
+<li>In <code>aA</code>, <code>a</code> and <code>A</code> react, leaving nothing behind.</li>
+<li>In <code>abBA</code>, <code>bB</code> destroys itself, leaving <code>aA</code>. As above, this then destroys itself, leaving nothing.</li>
+<li>In <code>abAB</code>, no two adjacent units are of the same type, and so nothing happens.</li>
+<li>In <code>aabAAB</code>, even though <code>aa</code> and <code>AA</code> are of the same type, their polarities match, and so nothing happens.</li>
+</ul>
+<p>Now, consider a larger example, <code>dabAcCaCBAcCcaDA</code>:</p>
+<pre><code>dabA<em>cC</em>aCBAcCcaDA The first 'cC' is removed.
+dab<em>Aa</em>CBAcCcaDA This creates 'Aa', which is removed.
+dabCBA<em>cCc</em>aDA Either 'cC' or 'Cc' are removed (the result is the same).
+dabCBAcaDA No further actions can be taken.
+</code></pre>
+<p>After all possible reactions, the resulting polymer contains <em>10 units</em>.</p>
+<p><em>How many units remain after fully reacting the polymer you scanned?</em> <span class="quiet">(Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)</span></p>
+</article>
+<p>Your puzzle answer was <code>9060</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Time to improve the polymer.</p>
+<p>One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.</p>
+<p>For example, again using the polymer <code>dabAcCaCBAcCcaDA</code> from above:</p>
+<ul>
+<li>Removing all <code>A</code>/<code>a</code> units produces <code>dbcCCBcCcD</code>. Fully reacting this polymer produces <code>dbCBcD</code>, which has length 6.</li>
+<li>Removing all <code>B</code>/<code>b</code> units produces <code>daAcCaCAcCcaDA</code>. Fully reacting this polymer produces <code>daCAcaDA</code>, which has length 8.</li>
+<li>Removing all <code>C</code>/<code>c</code> units produces <code>dabAaBAaDA</code>. Fully reacting this polymer produces <code>daDA</code>, which has length 4.</li>
+<li>Removing all <code>D</code>/<code>d</code> units produces <code>abAcCaCBAcCcaA</code>. Fully reacting this polymer produces <code>abCBAc</code>, which has length 6.</li>
+</ul>
+<p>In this example, removing all <code>C</code>/<code>c</code> units was best, producing the answer <em>4</em>.</p>
+<p><em>What is the length of the shortest polymer you can produce</em> by removing all units of exactly one type and fully reacting the result?</p>
+</article>
+<p>Your puzzle answer was <code>6310</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
+<p>At this point, you should <a href="/2018">return to your advent calendar</a> and try another puzzle.</p>
+<p>If you still want to see it, you can <a href="5/input" target="_blank">get your puzzle input</a>.</p>
+<p>You can also <span class="share">[Share<span class="share-content">on
+ <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Alchemical+Reduction%22+%2D+Day+5+%2D+Advent+of+Code+2018&url=https%3A%2F%2Fadventofcode%2Ecom%2F2018%2Fday%2F5&related=ericwastl&hashtags=AdventOfCode" target="_blank">Twitter</a>
+ <a href="http://www.reddit.com/submit?url=https%3A%2F%2Fadventofcode%2Ecom%2F2018%2Fday%2F5&title=I%27ve+completed+%22Alchemical+Reduction%22+%2D+Day+5+%2D+Advent+of+Code+2018" target="_blank">Reddit</a
+></span>]</span> this puzzle.</p>
+</main>
+
+<!-- ga -->
+<script>
+(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+ga('create', 'UA-69522494-1', 'auto');
+ga('send', 'pageview');
+</script>
+<!-- /ga -->
+</body>
+</html>
\ No newline at end of file
--- /dev/null
+import Data.List
+import Data.Char (toLower)
+
+main :: IO ()
+main = do
+ text <- readFile "data/advent05.txt"
+ -- let ids = lines text
+ print $ part1 text
+ print $ part2 text
+ -- putStrLn $ part2 ids
+
+
+part1 = reactedLength
+
+part2 polymer = minimum finalLengths
+ where finalLengths = map (\u -> reactedLength $ removeUnit polymer u) units
+ units = unitsPresent polymer
+
+
+react :: String -> Char -> Char -> String -> (String, String)
+react prefix a b suffix =
+ if willReact a b
+ then (prefix, suffix)
+ else ((b:a:prefix), suffix)
+
+willReact :: Char -> Char -> Bool
+willReact a b = (a /= b) && (toLower a == toLower b)
+
+
+reactHere :: (String, String) -> Maybe ((String, String), (String, String))
+reactHere (prefix, suffix) =
+ if canContinue prefix suffix
+ then Just ((prefix'', suffix''), (prefix'', suffix''))
+ else Nothing
+ where (prefix', a, b, suffix') = reactionSite prefix suffix
+ (prefix'', suffix'') = react prefix' a b suffix'
+
+
+canContinue (_:_) (_:_) = True
+canContinue [] (_:_:_) = True
+canContinue _ _ = False
+
+reactionSite (a:prefix) (b:suffix) = (prefix, a, b, suffix)
+reactionSite [] (a:b:suffix) = ([], a, b, suffix)
+
+
+reactedLength polymer = length prefix + length suffix
+ where (prefix, suffix) = last $ unfoldr reactHere ("", polymer)
+
+
+unitsPresent = nub . sort . map toLower
+
+removeUnit polymer unit = filter (\c -> toLower c /= unit ) polymer