--- /dev/null
+-- import Debug.Trace
+
+-- import Data.Text (Text)
+-- import qualified Data.Text as T
+import qualified Data.Text.IO as TIO
+
+import Data.Attoparsec.Text hiding (take)
+-- import Data.Attoparsec.Combinator
+import Control.Applicative
+-- import Control.Applicative.Combinators
+
+import qualified Data.Array.Unboxed as A
+import Data.Array.Unboxed ((!))
+import qualified Data.Map.Strict as M
+import Data.Bool (bool)
+import Data.List (delete)
+import Control.Monad (guard)
+-- import Data.Either (fromRight)
+
+
+type Coord = (Int, Int)
+type Pixels = A.UArray Coord Bool
+type Border = A.UArray Int Bool
+
+data Tile = Tile
+ { tId :: Integer
+ , pixels :: Pixels
+ } deriving (Show, Eq)
+
+type Arrangement = M.Map Coord Tile
+
+
+main :: IO ()
+main =
+ do text <- TIO.readFile "data/advent20.txt"
+ let tiles = successfulParse text
+ let arrangeRMax = (floor $ sqrt @Double $ fromIntegral $ length tiles) - 1
+ let arrangement = arrangeTiles arrangeRMax tiles
+ let image = assembleImage arrangeRMax arrangement
+ seaMonster <- readSeaMonster
+ print $ part1 arrangeRMax arrangement
+ print $ part2 seaMonster image
+
+
+part1 rMax arrangement
+ = product $ M.elems
+ $ M.map tId
+ $ M.filterWithKey (isCorner rMax) arrangement
+
+part2 seaMonster image = minimum $ map (countRoughness seaMonster) transImages
+ where imgTile = Tile 0 image
+ transImages = map pixels $ transforms imgTile
+
+
+readSeaMonster :: IO Pixels
+readSeaMonster =
+ do text <- TIO.readFile "data/advent20seamonster.txt"
+ -- return $ fromRight (A.listArray ((0, 0), (1, 1)) []) $ parseOnly pixelsP text
+ return $ case parseOnly pixelsP text of
+ Left _err -> A.listArray ((0, 0), (1, 1)) []
+ Right seaMonster -> seaMonster
+
+
+isCorner _ (0, 0) _ = True
+isCorner l (0, c) _ = c == l
+isCorner l (r, 0) _ = r == l
+isCorner l (r, c) _ = r == l && c == l
+
+arrangeTiles :: Int -> [Tile] -> Arrangement
+arrangeTiles rMax tiles = head $ arrange (0, 0) rMax M.empty tiles
+
+arrange :: Coord -> Int -> Arrangement -> [Tile] -> [Arrangement]
+-- arrange h _ g ts | trace (show h ++ " " ++ show (M.map tId g) ++ " > " ++ show (length ts)) False = undefined
+arrange _ _ grid [] = return grid
+arrange (r, c) cMax grid tiles =
+ do tile <- tiles
+ transTile <- transforms tile
+ guard $ if r == 0 then True else matchVertical tileAbove transTile
+ guard $ if c == 0 then True else matchHorizontal tileLeft transTile
+ arrange (r', c')
+ cMax
+ (M.insert (r, c) transTile grid)
+ (delete tile tiles)
+ where tileAbove = grid M.! (r - 1 , c)
+ tileLeft = grid M.! (r, c - 1)
+ (r', c') = if c == cMax then (r + 1, 0) else (r, c + 1)
+
+
+matchHorizontal tile1 tile2 = (rightBorder tile1) == (leftBorder tile2)
+matchVertical tile1 tile2 = (bottomBorder tile1) == (topBorder tile2)
+
+
+topBorder :: Tile -> Border
+topBorder Tile{..} = A.listArray (0, c1) [pixels!(0, c) | c <- [0..c1] ]
+ where (_, (_, c1)) = A.bounds pixels
+
+bottomBorder :: Tile -> Border
+bottomBorder Tile{..} = A.listArray (0, c1) [pixels!(r1, c) | c <- [0..c1] ]
+ where (_, (r1, c1)) = A.bounds pixels
+
+leftBorder :: Tile -> Border
+leftBorder Tile{..} = A.listArray (0, r1) [pixels!(r, 0) | r <- [0..r1] ]
+ where (_, (r1, _)) = A.bounds pixels
+
+rightBorder :: Tile -> Border
+rightBorder Tile{..} = A.listArray (0, r1) [pixels!(r, c1) | r <- [0..r1] ]
+ where (_, (r1, c1)) = A.bounds pixels
+
+
+transforms :: Tile -> [Tile]
+transforms tile =
+ [ r $ f tile
+ | r <- [id, tRotate, tRotate . tRotate, tRotate . tRotate . tRotate]
+ , f <- [id, tFlip]
+ ]
+
+-- rotate quarter turn clockwise
+tRotate tile = tile {pixels = pixels'}
+ where bs = pixels tile
+ (_, (r1, c1)) = A.bounds bs
+ pixels' = A.ixmap ((0, 0), (c1, r1)) rotateIndex bs
+ rotateIndex (r, c) = (r1 - c, r) -- how to get to the old index from the new one
+
+tFlip tile = tile {pixels = pixels'}
+ where bs = pixels tile
+ (_, (r1, c1)) = A.bounds bs
+ pixels' = A.ixmap ((0, 0), (r1, c1)) flipIndex bs
+ flipIndex (r, c) = (r, c1 - c) -- how to get to the old index from the new one
+
+
+assembleImage :: Int -> Arrangement -> Pixels
+assembleImage arrangeRMax arrangement =
+ A.array ((0,0), (imageRMax, imageRMax)) imageElements
+ where (_, (tileRMax, _)) = A.bounds $ pixels $ arrangement M.! (0, 0)
+ tRM1 = tileRMax - 1
+ imageRMax = tRM1 * (arrangeRMax + 1) - 1
+ imageElements =
+ do ar <- [0..arrangeRMax] -- arrangement row
+ ac <- [0..arrangeRMax]
+ tr <- [1..tRM1] -- tile pixels row
+ tc <- [1..tRM1]
+ let px = (pixels $ arrangement M.! (ar, ac)) ! (tr, tc)
+ let ir = (ar * tRM1) + (tr - 1) -- assembled image row
+ let ic = (ac * tRM1) + (tc - 1)
+ return ((ir, ic), px)
+
+
+countRoughness sm image = imPixels - (smPixels * nSeaMonsters)
+ where smPixels = countPixels sm
+ imPixels = countPixels image
+ nSeaMonsters = length $ findSeaMonsters sm image
+
+countPixels :: Pixels -> Int
+countPixels = length . filter (== True) . A.elems
+
+findSeaMonsters :: Pixels -> Pixels -> [Coord]
+findSeaMonsters sm image = [ (r, c)
+ | r <- [0..(imR - smR)]
+ , c <- [0..(imC - smC)]
+ , seaMonsterPresent sm image r c
+ ]
+ where (_, (smR, smC)) = A.bounds sm
+ (_, (imR, imC)) = A.bounds image
+
+seaMonsterPresent sm image dr dc = all bothPresent $ A.indices sm
+ where bothPresent (r, c) = if (sm!(r, c))
+ then (image!(r + dr, c + dc))
+ else True
+
+
+showTile Tile{..} = show tId ++ "\n" ++ (showP pixels)
+
+showP ps = unlines [[bool ' ' '\x2588' (ps!(r, c)) | c <- [0..cMax] ] | r <- [0..rMax]]
+ where (_, (rMax, cMax)) = A.bounds ps
+ -- sb b = bool '.' '#' b
+
+-- -- Parse the input file
+
+tilesP = tileP `sepBy` blankLines
+
+blankLines = many endOfLine
+
+tileP = Tile <$> ("Tile " *> decimal) <* ":" <* endOfLine <*> pixelsP
+
+pixelsP = pixify <$> (pixelsRowP `sepBy` endOfLine)
+pixelsRowP = many1 (satisfy (inClass " .#"))
+
+pixify :: [String] -> Pixels
+pixify rows = A.array ((0, 0), (nRows, nCols))
+ [ ((r, c), (rows!!r)!!c == '#')
+ | r <- [0..nRows]
+ , c <- [0..nCols]
+ ]
+ where nRows = length rows - 1
+ nCols = (length $ head rows) - 1
+
+
+-- successfulParse :: Text -> (Integer, [Maybe Integer])
+successfulParse input =
+ case parseOnly tilesP input of
+ Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
+ Right tiles -> tiles
--- /dev/null
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 20 - Advent of Code 2020</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?25"/>
+<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="/2020/about">[About]</a></li><li><a href="/2020/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2020/settings">[Settings]</a></li><li><a href="/2020/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2020/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">40*</span></div></div><div><h1 class="title-event"> <span class="title-event-wrap">$year=</span><a href="/2020">2020</a><span class="title-event-wrap">;</span></h1><nav><ul><li><a href="/2020">[Calendar]</a></li><li><a href="/2020/support">[AoC++]</a></li><li><a href="/2020/sponsors">[Sponsors]</a></li><li><a href="/2020/leaderboard">[Leaderboard]</a></li><li><a href="/2020/stats">[Stats]</a></li></ul></nav></div></header>
+
+<div id="sidebar">
+<div id="sponsor"><div class="quiet">Our <a href="/2020/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://medium.com/building-trayio" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">tray.io</a> - Building a platform that connects people with technology.</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 20: Jurassic Jigsaw ---</h2><p>The high-speed train leaves the forest and quickly carries you south. You can even see a desert in the distance! Since you have some spare time, you <span title="Just in case. Maybe they missed something.">might as well</span> see if there was anything interesting in the image the Mythical Information Bureau satellite captured.</p>
+<p>After decoding the satellite messages, you discover that the data actually contains many small images created by the satellite's <em>camera array</em>. The camera array consists of many cameras; rather than produce a single square image, they produce many smaller square image <em>tiles</em> that need to be <em>reassembled back into a single image</em>.</p>
+<p>Each camera in the camera array returns a single monochrome <em>image tile</em> with a random unique <em>ID number</em>. The tiles (your puzzle input) arrived in a random order.</p>
+<p>Worse yet, the camera array appears to be malfunctioning: each image tile has been <em>rotated and flipped to a random orientation</em>. Your first task is to reassemble the original image by orienting the tiles so they fit together.</p>
+<p>To show how the tiles should be reassembled, each tile's image data includes a border that should line up exactly with its adjacent tiles. All tiles have this border, and the border lines up exactly when the tiles are both oriented correctly. Tiles at the edge of the image also have this border, but the outermost edges won't line up with any other tiles.</p>
+<p>For example, suppose you have the following nine tiles:</p>
+<pre><code>Tile 2311:
+..##.#..#.
+##..#.....
+#...##..#.
+####.#...#
+##.##.###.
+##...#.###
+.#.#.#..##
+..#....#..
+###...#.#.
+..###..###
+
+Tile 1951:
+#.##...##.
+#.####...#
+.....#..##
+#...######
+.##.#....#
+.###.#####
+###.##.##.
+.###....#.
+..#.#..#.#
+#...##.#..
+
+Tile 1171:
+####...##.
+#..##.#..#
+##.#..#.#.
+.###.####.
+..###.####
+.##....##.
+.#...####.
+#.##.####.
+####..#...
+.....##...
+
+Tile 1427:
+###.##.#..
+.#..#.##..
+.#.##.#..#
+#.#.#.##.#
+....#...##
+...##..##.
+...#.#####
+.#.####.#.
+..#..###.#
+..##.#..#.
+
+Tile 1489:
+##.#.#....
+..##...#..
+.##..##...
+..#...#...
+#####...#.
+#..#.#.#.#
+...#.#.#..
+##.#...##.
+..##.##.##
+###.##.#..
+
+Tile 2473:
+#....####.
+#..#.##...
+#.##..#...
+######.#.#
+.#...#.#.#
+.#########
+.###.#..#.
+########.#
+##...##.#.
+..###.#.#.
+
+Tile 2971:
+..#.#....#
+#...###...
+#.#.###...
+##.##..#..
+.#####..##
+.#..####.#
+#..#.#..#.
+..####.###
+..#.#.###.
+...#.#.#.#
+
+Tile 2729:
+...#.#.#.#
+####.#....
+..#.#.....
+....#..#.#
+.##..##.#.
+.#.####...
+####.#.#..
+##.####...
+##..#.##..
+#.##...##.
+
+Tile 3079:
+#.#.#####.
+.#..######
+..#.......
+######....
+####.#..#.
+.#...#.##.
+#.#####.##
+..#.###...
+..#.......
+..#.###...
+</code></pre>
+<p>By rotating, flipping, and rearranging them, you can find a square arrangement that causes all adjacent borders to line up:</p>
+<pre><code>#...##.#.. ..###..### #.#.#####.
+..#.#..#.# ###...#.#. .#..######
+.###....#. ..#....#.. ..#.......
+###.##.##. .#.#.#..## ######....
+.###.##### ##...#.### ####.#..#.
+.##.#....# ##.##.###. .#...#.##.
+#...###### ####.#...# #.#####.##
+.....#..## #...##..#. ..#.###...
+#.####...# ##..#..... ..#.......
+#.##...##. ..##.#..#. ..#.###...
+
+#.##...##. ..##.#..#. ..#.###...
+##..#.##.. ..#..###.# ##.##....#
+##.####... .#.####.#. ..#.###..#
+####.#.#.. ...#.##### ###.#..###
+.#.####... ...##..##. .######.##
+.##..##.#. ....#...## #.#.#.#...
+....#..#.# #.#.#.##.# #.###.###.
+..#.#..... .#.##.#..# #.###.##..
+####.#.... .#..#.##.. .######...
+...#.#.#.# ###.##.#.. .##...####
+
+...#.#.#.# ###.##.#.. .##...####
+..#.#.###. ..##.##.## #..#.##..#
+..####.### ##.#...##. .#.#..#.##
+#..#.#..#. ...#.#.#.. .####.###.
+.#..####.# #..#.#.#.# ####.###..
+.#####..## #####...#. .##....##.
+##.##..#.. ..#...#... .####...#.
+#.#.###... .##..##... .####.##.#
+#...###... ..##...#.. ...#..####
+..#.#....# ##.#.#.... ...##.....
+</code></pre>
+<p>For reference, the IDs of the above tiles are:</p>
+<pre><code><em>1951</em> 2311 <em>3079</em>
+2729 1427 2473
+<em>2971</em> 1489 <em>1171</em>
+</code></pre>
+<p>To check that you've assembled the image correctly, multiply the IDs of the four corner tiles together. If you do this with the assembled tiles from the example above, you get <code>1951 * 3079 * 2971 * 1171</code> = <em><code>20899048083289</code></em>.</p>
+<p>Assemble the tiles into an image. <em>What do you get if you multiply together the IDs of the four corner tiles?</em></p>
+</article>
+<p>Your puzzle answer was <code>30425930368573</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Now, you're ready to <em>check the image for sea monsters</em>.</p>
+<p>The borders of each tile are not part of the actual image; start by removing them.</p>
+<p>In the example above, the tiles become:</p>
+<pre><code>.#.#..#. ##...#.# #..#####
+###....# .#....#. .#......
+##.##.## #.#.#..# #####...
+###.#### #...#.## ###.#..#
+##.#.... #.##.### #...#.##
+...##### ###.#... .#####.#
+....#..# ...##..# .#.###..
+.####... #..#.... .#......
+
+#..#.##. .#..###. #.##....
+#.####.. #.####.# .#.###..
+###.#.#. ..#.#### ##.#..##
+#.####.. ..##..## ######.#
+##..##.# ...#...# .#.#.#..
+...#..#. .#.#.##. .###.###
+.#.#.... #.##.#.. .###.##.
+###.#... #..#.##. ######..
+
+.#.#.### .##.##.# ..#.##..
+.####.## #.#...## #.#..#.#
+..#.#..# ..#.#.#. ####.###
+#..####. ..#.#.#. ###.###.
+#####..# ####...# ##....##
+#.##..#. .#...#.. ####...#
+.#.###.. ##..##.. ####.##.
+...###.. .##...#. ..#..###
+</code></pre>
+<p>Remove the gaps to form the actual image:</p>
+<pre><code>.#.#..#.##...#.##..#####
+###....#.#....#..#......
+##.##.###.#.#..######...
+###.#####...#.#####.#..#
+##.#....#.##.####...#.##
+...########.#....#####.#
+....#..#...##..#.#.###..
+.####...#..#.....#......
+#..#.##..#..###.#.##....
+#.####..#.####.#.#.###..
+###.#.#...#.######.#..##
+#.####....##..########.#
+##..##.#...#...#.#.#.#..
+...#..#..#.#.##..###.###
+.#.#....#.##.#...###.##.
+###.#...#..#.##.######..
+.#.#.###.##.##.#..#.##..
+.####.###.#...###.#..#.#
+..#.#..#..#.#.#.####.###
+#..####...#.#.#.###.###.
+#####..#####...###....##
+#.##..#..#...#..####...#
+.#.###..##..##..####.##.
+...###...##...#...#..###
+</code></pre>
+<p>Now, you're ready to search for sea monsters! Because your image is monochrome, a sea monster will look like this:</p>
+<pre><code> #
+# ## ## ###
+ # # # # # #
+</code></pre>
+<p>When looking for this pattern in the image, <em>the spaces can be anything</em>; only the <code>#</code> need to match. Also, you might need to rotate or flip your image before it's oriented correctly to find sea monsters. In the above image, <em>after flipping and rotating it</em> to the appropriate orientation, there are <em>two</em> sea monsters (marked with <code><em>O</em></code>):</p>
+<pre><code>.####...#####..#...###..
+#####..#..#.#.####..#.#.
+.#.#...#.###...#.##.<em>O</em>#..
+#.<em>O</em>.##.<em>O</em><em>O</em>#.#.<em>O</em><em>O</em>.##.<em>O</em><em>O</em><em>O</em>##
+..#<em>O</em>.#<em>O</em>#.<em>O</em>##<em>O</em>..<em>O</em>.#<em>O</em>##.##
+...#.#..##.##...#..#..##
+#.##.#..#.#..#..##.#.#..
+.###.##.....#...###.#...
+#.####.#.#....##.#..#.#.
+##...#..#....#..#...####
+..#.##...###..#.#####..#
+....#.##.#.#####....#...
+..##.##.###.....#.##..#.
+#...#...###..####....##.
+.#.##...#.##.#.#.###...#
+#.###.#..####...##..#...
+#.###...#.##...#.##<em>O</em>###.
+.<em>O</em>##.#<em>O</em><em>O</em>.###<em>O</em><em>O</em>##..<em>O</em><em>O</em><em>O</em>##.
+..<em>O</em>#.<em>O</em>..<em>O</em>..<em>O</em>.#<em>O</em>##<em>O</em>##.###
+#.#..##.########..#..##.
+#.#####..#.#...##..#....
+#....##..#.#########..##
+#...#.....#..##...###.##
+#..###....##.#...##.##.#
+</code></pre>
+<p>Determine how rough the waters are in the sea monsters' habitat by counting the number of <code>#</code> that are <em>not</em> part of a sea monster. In the above example, the habitat's water roughness is <em><code>273</code></em>.</p>
+<p><em>How many <code>#</code> are not part of a sea monster?</em></p>
+</article>
+<p>Your puzzle answer was <code>2453</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="/2020">return to your Advent calendar</a> and try another puzzle.</p>
+<p>If you still want to see it, you can <a href="20/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+%22Jurassic+Jigsaw%22+%2D+Day+20+%2D+Advent+of+Code+2020&url=https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F20&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+%22Jurassic+Jigsaw%22+%2D+Day+20+%2D+Advent+of+Code+2020+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F20'}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