--- /dev/null
+-- Writeup at https://work.njae.me.uk/2021/12/04/advent-of-code-2021-day-4/
+
+import Data.Text ()
+import qualified Data.Text.IO as TIO
+
+import Data.Attoparsec.Text
+import Control.Applicative
+
+import qualified Data.Map.Strict as M
+import Linear (V2(..), (^+^))
+
+
+type Point = V2 Int
+type Grid = M.Map Point Int
+
+data Line = Line Point Point deriving (Eq, Show)
+
+
+main :: IO ()
+main =
+ do text <- TIO.readFile "data/advent05.txt"
+ let trace = successfulParse text
+ print $ part1 trace
+ print $ part2 trace
+
+part1 trace = M.size $ M.filter (> 1) diagramHV
+ where hLines = map expandLineH $ filter isHoriz trace
+ vLines = map expandLineV $ filter isVert trace
+ diagramH = addLines M.empty hLines
+ diagramHV = addLines diagramH vLines
+
+part2 trace = M.size $ M.filter (> 1) diagram4
+ where hLines = map expandLineH $ filter isHoriz trace
+ vLines = map expandLineV $ filter isVert trace
+ pdLines = map expandLinePD $ filter isPDiag trace
+ ndLines = map expandLineND $ filter isNDiag trace
+ diagram1 = addLines M.empty hLines
+ diagram2 = addLines diagram1 vLines
+ diagram3 = addLines diagram2 pdLines
+ diagram4 = addLines diagram3 ndLines
+
+
+
+isHoriz :: Line -> Bool
+isHoriz (Line (V2 x0 _y0) (V2 x1 _y1)) = x0 == x1
+
+isVert :: Line -> Bool
+isVert (Line (V2 _x0 y0) (V2 _x1 y1)) = y0 == y1
+
+-- x and y increasing together
+isPDiag :: Line -> Bool
+isPDiag (Line (V2 x0 y0) (V2 x1 y1)) =
+ (x0 /= x1)
+ && (y0 /= y1)
+ && ((x1 - x0) == (y1 - y0))
+
+-- x increasing, y decreasing
+isNDiag :: Line -> Bool
+isNDiag (Line (V2 x0 y0) (V2 x1 y1)) =
+ (x0 /= x1)
+ && (y0 /= y1)
+ && ((x1 - x0) == (y0 - y1))
+
+
+-- horizOrVert :: Line -> Bool
+-- horizOrVert line = isHoriz line || isVert line
+
+
+-- hovLines = filter horizOrVert
+
+expandLineH :: Line -> [Point]
+expandLineH (Line (V2 x0 y0) (V2 _x1 y1)) = -- x0 == x1
+ [V2 x0 y | y <- [yMin..yMax]]
+ where yMin = min y0 y1
+ yMax = max y0 y1
+
+expandLineV :: Line -> [Point]
+expandLineV (Line (V2 x0 y0) (V2 x1 _y1)) = -- y0 == y1
+ [V2 x y0 | x <- [xMin..xMax]]
+ where xMin = min x0 x1
+ xMax = max x0 x1
+
+-- x and y increasing together
+expandLinePD :: Line -> [Point]
+expandLinePD (Line (V2 x0 y0) (V2 x1 y1)) = map (uncurry V2) coords
+ where xMin = min x0 x1
+ xMax = max x0 x1
+ yMin = min y0 y1
+ yMax = max y0 y1
+ coords = zip [xMin..xMax] [yMin..yMax]
+
+-- x increasing, y decreasing
+expandLineND :: Line -> [Point]
+expandLineND (Line (V2 x0 y0) (V2 x1 y1)) = map (uncurry V2) coords
+ where xMin = min x0 x1
+ xMax = max x0 x1
+ yMin = min y0 y1
+ yMax = max y0 y1
+ yMax1 = yMax - 1
+ coords = zip [xMin..xMax] [yMax, yMax1..yMin]
+
+
+addLines :: Grid -> [[Point]] -> Grid
+addLines diagram expandedLines = foldr insertLine diagram expandedLines
+ where insertPoint p d = M.insertWith (+) p 1 d
+ insertLine l d = foldr insertPoint d l
+
+
+-- Parse the input file
+
+traceP = lineP `sepBy` endOfLine
+lineP = Line <$> (pointP <* " -> ") <*> pointP
+pointP = V2 <$> (decimal <* ",") <*> decimal
+
+-- successfulParse :: Text -> (Integer, [Maybe Integer])
+successfulParse input =
+ case parseOnly traceP input of
+ Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
+ Right trace -> trace
--- /dev/null
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 5 - Advent of Code 2021</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?26"/>
+<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 a massive company, 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, 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; preparing a new calendar and a new set of
+puzzles each year takes all of my free time for 4-5 months. 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="/2021/about">[About]</a></li><li><a href="/2021/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2021/settings">[Settings]</a></li><li><a href="/2021/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2021/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">/*</span><a href="/2021">2021</a><span class="title-event-wrap">*/</span></h1><nav><ul><li><a href="/2021">[Calendar]</a></li><li><a href="/2021/support">[AoC++]</a></li><li><a href="/2021/sponsors">[Sponsors]</a></li><li><a href="/2021/leaderboard">[Leaderboard]</a></li><li><a href="/2021/stats">[Stats]</a></li></ul></nav></div></header>
+
+<div id="sidebar">
+<div id="sponsor"><div class="quiet">Our <a href="/2021/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://www.tcgplayer.com/adventofcode/?utm_campaign=aoc&utm_source=adventOfCode&utm_medium=aocPromo" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">TCGplayer.com</a> - We grok you, your passion, and your local game store. And what we have done for trading card games, we'll now be doing for the collectibles industry! Looking for engineers, DevOps, product & more - join us!</div></div>
+</div><!--/sidebar-->
+
+<main>
+<script>window.addEventListener('click', function(e,s,r){if(e.target.nodeName==='CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(e.target);s.addRange(r);}});</script>
+<article class="day-desc"><h2>--- Day 5: Hydrothermal Venture ---</h2><p>You come across a field of <a href="https://en.wikipedia.org/wiki/Hydrothermal_vent" target="_blank">hydrothermal vents</a> on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible.</p>
+<p>They tend to form in <em>lines</em>; the submarine helpfully produces a list of nearby <span title="Maybe they're Bresenham vents.">lines of vents</span> (your puzzle input) for you to review. For example:</p>
+<pre><code>0,9 -> 5,9
+8,0 -> 0,8
+9,4 -> 3,4
+2,2 -> 2,1
+7,0 -> 7,4
+6,4 -> 2,0
+0,9 -> 2,9
+3,4 -> 1,4
+0,0 -> 8,8
+5,5 -> 8,2
+</code></pre>
+<p>Each line of vents is given as a line segment in the format <code>x1,y1 -> x2,y2</code> where <code>x1</code>,<code>y1</code> are the coordinates of one end the line segment and <code>x2</code>,<code>y2</code> are the coordinates of the other end. These line segments include the points at both ends. In other words:</p>
+<ul>
+<li>An entry like <code>1,1 -> 1,3</code> covers points <code>1,1</code>, <code>1,2</code>, and <code>1,3</code>.</li>
+<li>An entry like <code>9,7 -> 7,7</code> covers points <code>9,7</code>, <code>8,7</code>, and <code>7,7</code>.</li>
+</ul>
+<p>For now, <em>only consider horizontal and vertical lines</em>: lines where either <code>x1 = x2</code> or <code>y1 = y2</code>.</p>
+<p>So, the horizontal and vertical lines from the above list would produce the following diagram:</p>
+<pre><code>.......1..
+..1....1..
+..1....1..
+.......1..
+.112111211
+..........
+..........
+..........
+..........
+222111....
+</code></pre>
+<p>In this diagram, the top left corner is <code>0,0</code> and the bottom right corner is <code>9,9</code>. Each position is shown as <em>the number of lines which cover that point</em> or <code>.</code> if no line covers that point. The top-left pair of <code>1</code>s, for example, comes from <code>2,2 -> 2,1</code>; the very bottom row is formed by the overlapping lines <code>0,9 -> 5,9</code> and <code>0,9 -> 2,9</code>.</p>
+<p>To avoid the most dangerous areas, you need to determine <em>the number of points where at least two lines overlap</em>. In the above example, this is anywhere in the diagram with a <code>2</code> or larger - a total of <code><em>5</em></code> points.</p>
+<p>Consider only horizontal and vertical lines. <em>At how many points do at least two lines overlap?</em></p>
+</article>
+<p>Your puzzle answer was <code>5092</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider <em>diagonal lines</em>.</p>
+<p>Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:</p>
+<ul>
+<li>An entry like <code>1,1 -> 3,3</code> covers points <code>1,1</code>, <code>2,2</code>, and <code>3,3</code>.</li>
+<li>An entry like <code>9,7 -> 7,9</code> covers points <code>9,7</code>, <code>8,8</code>, and <code>7,9</code>.</li>
+</ul>
+<p>Considering all lines from the above example would now produce the following diagram:</p>
+<pre><code>1.1....11.
+.111...2..
+..2.1.111.
+...1.2.2..
+.112313211
+...1.2....
+..1...1...
+.1.....1..
+1.......1.
+222111....
+</code></pre>
+<p>You still need to determine <em>the number of points where at least two lines overlap</em>. In the above example, this is still anywhere in the diagram with a <code>2</code> or larger - now a total of <code><em>12</em></code> points.</p>
+<p>Consider all of the lines. <em>At how many points do at least two lines overlap?</em></p>
+</article>
+<p>Your puzzle answer was <code>20484</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="/2021">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+%22Hydrothermal+Venture%22+%2D+Day+5+%2D+Advent+of+Code+2021&url=https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F5&related=ericwastl&hashtags=AdventOfCode" target="_blank">Twitter</a>
+ <a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' && mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=I%27ve+completed+%22Hydrothermal+Venture%22+%2D+Day+5+%2D+Advent+of+Code+2021+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F5'}else{return false;}" target="_blank">Mastodon</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('set', 'anonymizeIp', true);
+ga('send', 'pageview');
+</script>
+<!-- /ga -->
+</body>
+</html>
\ No newline at end of file