From 230f8fde364d05f31df481c711f6e7eb086259a5 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 9 Dec 2019 09:05:22 +0000 Subject: [PATCH] Day 8 done --- advent08/package.yaml | 60 +++++++++++++++ advent08/src/advent08.hs | 49 ++++++++++++ data/advent08.txt | 1 + problems/day08.html | 162 +++++++++++++++++++++++++++++++++++++++ stack.yaml | 1 + 5 files changed, 273 insertions(+) create mode 100644 advent08/package.yaml create mode 100644 advent08/src/advent08.hs create mode 100644 data/advent08.txt create mode 100644 problems/day08.html diff --git a/advent08/package.yaml b/advent08/package.yaml new file mode 100644 index 0000000..1405e77 --- /dev/null +++ b/advent08/package.yaml @@ -0,0 +1,60 @@ +# This YAML file describes your package. Stack will automatically generate a +# Cabal file when you run `stack build`. See the hpack website for help with +# this file: . + +name: advent08 +synopsis: Advent of Code +version: '0.0.1' + +default-extensions: +- AllowAmbiguousTypes +- ApplicativeDo +- BangPatterns +- BlockArguments +- DataKinds +- DeriveFoldable +- DeriveFunctor +- DeriveGeneric +- DeriveTraversable +- EmptyCase +- FlexibleContexts +- FlexibleInstances +- FunctionalDependencies +- GADTs +- GeneralizedNewtypeDeriving +- ImplicitParams +- KindSignatures +- LambdaCase +- MonadComprehensions +- MonoLocalBinds +- MultiParamTypeClasses +- MultiWayIf +- NegativeLiterals +- NumDecimals +- OverloadedLists +- OverloadedStrings +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeInType +- TypeOperators +- ViewPatterns + + +executables: + advent08: + main: advent08.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - text + - megaparsec + - split diff --git a/advent08/src/advent08.hs b/advent08/src/advent08.hs new file mode 100644 index 0000000..fe00af5 --- /dev/null +++ b/advent08/src/advent08.hs @@ -0,0 +1,49 @@ +import Data.List +import Data.List.Split +import Data.Char +import Data.Ord +import Data.Function + +main :: IO () +main = do + text <- readFile "data/advent08.txt" + let digits = successfulParse text + let layers = chunksOf (imageWidth * imageHeight) digits + print $ part1 layers + putStrLn $ part2 layers + + +imageWidth = 25 +imageHeight = 6 + +part1 layers = (count 1 target) * (count 2 target) + where target = minimumBy (comparing (count 0)) layers + +part2 layers = unlines rows + where pixelLayers = transpose layers + pixels = map firstVisible pixelLayers + dPixels = map showPixel pixels + pixelRows = chunksOf imageWidth dPixels + rows = map concat pixelRows + + +firstVisible = head . dropWhile (== 2) + +showPixel 0 = " " +showPixel 1 = "\x2588" + + +count n = length . filter (== n) + +-- Count the number of times a predicate is true +-- (Taken from GHC API utility functions) + +-- count :: (a -> Bool) -> [a] -> Int +-- count p = go 0 +-- where go !n [] = n +-- go !n (x:xs) | p x = go (n+1) xs +-- | otherwise = go n xs + + +successfulParse :: String -> [Int] +successfulParse input = map digitToInt input \ No newline at end of file diff --git a/data/advent08.txt b/data/advent08.txt new file mode 100644 index 0000000..d1b78dc --- /dev/null +++ b/data/advent08.txt @@ -0,0 +1 @@ +202201222022222222222222210222222222221222220202222222022222222222222221222200221022222222222220222222220220202221222222222222222221222221222212022202202220222222222222222222221220222222220222222212222222022222222222222222222220220122222222222220222222221220212221222222222222222221222221222202122222212211222022222222222222212220222222222222221212222222022222222222222222222221221122222222222221222222202220222220222222222222222221222220222212022222222220222022222222222202221220222222222222220202222222022222222222222221222202222022222222222222222222200222222221222222222222221222222222222222222202212220222022222222222212220220222222221222221202222222222222222222222222222202221222222222222221222222202221202221222222222222222220222220222212122222212212222222202222222200220222222222221222222222222222122222222222222222222210221022222222202221222222211220202222222222222222220222222222222222222202222200222022212222222212202221222222222222221212222222022222222222222220222211220222222222212220222222212222212220222222222222222220222220222222022202202211222022222222222221221221202222222222222202222222222222222222222220222211222122222222222221222212220222212221222222222222222221222211222212122222202210222122202222222212211220202222220222221202222222122222222222222220222221221222222222222222222212200220222222222222222222221220222202222212122202202221222222202222222222211222222222220222222202222222122222222222222221222212221122222222202220222202201222212220222222222222221220222212222222022222222200222122212222222200212220212222220222220222222222022222222022222220222202220222222222212221222212211220202220222222222222220221222200222222022222212200222022210222222211222221212222220222220212022222022222222222222222222220220022222222222220222212221221212220222222222222222221222212222212222212202211222222211222222200202220212222221222221202022222222222222022222221222212222022222222202220222202221222212220222222222222222222222200222212222212212220222222221222222210220222222222221222221202022222022222222222222222222220222222222222212222222212220220212222222222222222221222222221222212222202222221122122211222222211221220222222222222221202122222222222222122222221222222221122222222212222222222212221212222222222222222221220222221222222022222212211122222220222222210212222202222220222221222022222022222222022222222222220222122222222212220222212201220222222222222222222222220222202222202122212212221222122201222222220222220202222220222222212122222122222222122222220222211221022212222222221222202222222202220222222222222220221222200222212222222212211222222221222222202200222222222222202220222022222122222222022222221222202222122222222222221222202212222222220222222222222220222222211222222222202222210222122200222222212201220202222220202221212122222222222222222222221222222220222202222222220222222212222222221222222222222222222222220222222222222212210022022202222222221222220202222221202221202222222122222222122222220222201221122222222202222222222221220212221222222222222222220222200222202222222212201122222220222222221222222212222222212220222022222022222222022222222222201221122212222202221222212202221212222222222222222221222222211222202122212222201222222202222222212200222200222221202222222022222022222222222022221222220222222212222222222222202222221212222222222222222222222222212222212022212222222122022221222222210221200220222220222220202222222222222222122122222222220222022222222202222222222200222212222222222222222210221222221222222222222202221022122220222222211212201220222222022220212022222022222220222022220222210221022202222222221222212212220212221220222222222212221222200222202122222212211222122210222222200221212201221220112220212022202222222221022022220222210221122202222212220222212211220202222220222222222201221222221222212222202222221122222222222222210220201220222221112222212022222022222220222222222222201221122212222202222222222211221222222221222222222221222222200222202022222222202022122202222222221221202202221220202221222222212122222221222022221220222221022202222202221222222221220202221220222222222202222222221222222122202212202122022220222222210200202202222220122222212122202022222221220222220221201222122212222222222222212211220202221222222222222200220222211222222222210222202022122201222222220211220201220221202220222022202222222220020122222222220220222212222222222222202212221212221222222222222222222222220220222122222212212122122222222222222201202200221221002221212222222222222221221122220222221221122202222202221222222222221222222220222222222200220222211222222222221202200222222211222222222212212222221221112221102222222122222222020122220221221221022222222202220222222210221212220221222222222210222222221220202022211202222222022202222222222202221220221221122222012022202222222221222222222220220220122202222222220222222202222202222221222222222210222222221221202122200222201022222201222222202201221221221222012220002122222122222221121122220220222221122212222222222222202221221222221222222222222221221222212221202022222222211022022202222222220222220211222221202222002222212022222220021122222221201221222212222212220222222201222212221221222022222220222222221221202222222222220022122222222222211200211200220220202220002222212122222222022222222222210221022202222202220222212212221222220222222022222210221222200221202122212222202122122211222222202211221221222221102211222222212122222221120122221222200220122202222202221222202221222222222220222222222220220222201221202022220222222022222222022222221201210200222220212201212122212122222221121022222220210222222212222212222222212210220222220221222222222211221222211120202222220212202222122202222222200220202200022221222202122022212022222221222222222221210221022212222202221222202211222202221021222222222220222222201120212022210212212022222201022222200221221221020220212210212022212022222221221022222222222221122222222222221222212210221202220220222022222221220222210222212122201212200222122210222222201211212222021222002221222222212122222220022122220221221220222212220222221222202200120222222022222222222201220222210220222222220212200022122202222222202201201200122221022212202112212022222220020022220221222222122212220212222222202200021202220121222022222212222212221121212022202222212022222201222222212210201222220221122211012022222122222221121222220220201221122202222202222222222200021222220021222222222220222212212120202022201212200222122202122222210221210210220222002201012022202122222220021022222220200222022212222202221222212200121222220022222022222222221222210022212122222212212122222211222222212200201200021220012211202022202022222222122122222220220222222222220212221222202212020202222222222222222200222212222120222022201222221022022201221222200210220211220220122200202022212122220221022222222222222222022202221212221222222201020202202121222022222210221222200022202222212222212122022220122222210210200212020222012222102002212022221220122222222220222221022202221212222222212202220212220122222022222200221222211022212222221222221022022200222222200220220221122221102222022222212122220220122022221222222222222202220212221222202221021222212221222222222211221212212122202022201202201022022201121222021200220200220222222212102212222022222222220222220222201220222212220202222222222220221212221220222222221221221222202121212122222222211022021222020222212212201211222221022220002102212122222222021022222222210220022202220222220222212220121202220022222222222221222202211122222122202222212122221221122222111212222211122220212222112222202122221221222022220221212222022222222222222222222222022212201220222022220110220212200121202122222212220022021201122222022212212222021222012210222212222122221220020222222220221222122222220222221222212221220212212121222022220000221212212021222222202222221122020200120222010202202211220220202210202202202122220220100022222222221222122222221222222222222212022222202122222222220221221212210221212122221200221122221202020222022200200211121220022212222202202122222220020022222220200221122212222212222222202201222212200022222222222102222202220020222122202220211122222220222222121202211220021220222201202122222022222221000122221220201222022222220222220222210222220222221120222022220201222112221021212022202221201022122201121222202212220221220222212201212012212122221220122022222221221222222202220202221222221222222202221220222122222120220102202122222222222201221122022200122222220211222210220222022210112112212212222221102122221220212221122202220222222222212222121202201022222022221101222122202120200122212202211022121210022222200221221200022221222220012022202112220222120022222220220220222200222222222222220201120222221021222022222000221222201222201222221222201222220211020222220202211220201220212200202202212122220221200222222221202221222202221212221222212202021222211020222122220101221012212222201222202212022122121220022222221110212211210221112222202002222012220222002222220220222220222202220212220222212200121202211121222222222220222222221121200022222210210222222201221222122002211201121220112211022202212122221222102122222220220221022212220202222222222220122222220222222222221221220202210220200022212211121222122212222222020221221200100220222212002102212122222220012022221221211220122210220222220222220221121222202020222222220011222022200020200022212122211222221210120222002222220202220222002212102002222202220222210022222221201221202221220202221222202201021222201021222022220210221002221121221122222100212122121211122222021011221221201220002201002002212202221220111022222220201221012202221202222222210221122212201220222022220110220202222122221222211112200222120212221222222202212200220221022201012102222212221222202022220211210221110221222202221222200211121222212121222022222021222102201222202222211010220222221212021222020012212221201222122222102002212212222221120022220201201220110202222212220222212202220222222021222021220210222122200121200122220010122012222210222222110112210202002200112222022112222112222220202122220010222221201200222222222222211210122202202120222221221101221022201220202022220112110002021211222222100021221201201220212212122002222012220221211022221200210220012200220212222220200221022212201022222020220210222102211021212022221200012212021201221222101202221212011222222222100112212112221221221022220210202221021221220202220222212222220222201111222220220200220212210120201122220012012022120210121222212100210222122202102211121122222202221222101222221212200222210202220202120222202221122222211102222122222220220002200221210022210220001112120202020222122012201212010222202212110202202112220220222022221001221220000220222202220220200211022202211211222222220120222112222222222022210101210211020200021222120122222211001202102202100102222102221220101022222120221221120212220222020222200211122202210001222121222222220022210121212222212012012122022222221222121200201220222201002211020102202222221222012121221211211222110212221012222220212201222212201000222022222122222102201220222122201111221020021201120222102112220221002222222201200102222222222222012221220210222200200212220112122222211211021212220210222220222212222112021222221222201211010001022210021222022120202202200201002221120002222022222221220120220110221200022200221212121222201211222212201222222022222021222002011021200122201100112110220211222222010122211222020221112210112122202202220221211121220201210212222220221222021221221220121202221202222022220101222122101221110122212000220002022221020222201012212110022221202211122012212112220221210020220210211211110200220202021222211220021222201200222122221100221012001120022022221212000222221202120222100010211021012011022212110012202202222220210122221210202202011222220012022222212201220202202010222220222211221222011220221022200000001222020222120222202010210010000211022202001012202102222220001120220220212210020220221012202220201211022202222012222022221112222202111220200222221012000012022210222222022222211020101222222002201202222122222220222220221120222222220202222112012222210202120222221220222122222001221012210122122220200120010100220222022222000120220102112002222222010202222222220220020221220020201201222221220012112222200221221212221211222022222121221102002022202022121110012010221212021222212001221202220000202210210122212022221221001122221120220200212201220202220221212222122212220222222221221001221222021020111221020011202111221220120222200201220010000020002012121022202001222222001020221101201022021201220202000222202212020212220211222221222210221212202021222121112022211201220222220221021111221211010122022202020012212121221221020120222220220220222200210002101221222221221202201122222121221111222102122121220021211001210021222202101220011102210112122021012020200112202201220222111121220222200122210200222002001220201210222212201020222221221020220122110021222000022122001120221212210220101210221120120010202020222121202102202222120020222022220102101020200012111220220221120212212000222120221010222212001020211210200020110012222221121220222002222122001121210201100212222020202221010220220000220110101002202212011220210200121202202021222222222120222222220221212201022110101202021212202220210012210112112012102121122102202222222221002221222112210112122022221112110221200210121202212111222020220002221222022021211111002022111110120222221221220212201121122101022211120000222022211222101022220102221222210210202212000021212221220212210101222122222001222222120022021111022222101120220222020220212001221201022102002022020110212021201220112122222002211112002101200202222122220202022222200222222021221101221002121020111122011021210200221202200220102111202120211201100111120100202000212221112021220200210211120201121112210021210222120222210110222202022012220122011220120200202011112012220202200122212001221121122002110102002001202221212222212022220200222122022102100212110121200201021222221111222221120002222212221221011010001001012111121221100221001200202001100121121120120200012000200222110021221111222011221010110112102120211210122212202022222101121122221112000022002202121121010111120200200221001011212220212122010122001222002001201220011022222101220212111000002002120122211210021202212112222110122001221222022222220010011002222100221202121220121212210011211202100111022022102110221220101100221200201022121210011222101221220220221212220000222001021100220222010022122202220220211101122201211222212000201211112112220202020222112212200220220201220012202000210120012022121220201200021212200211222110022010210212112222121201220221110100221212100220122101221001101001020200111121112122201221212110221112200022211111020022222220201212020222222121222000021211211212012221202210201012022101121211102121002211200202221222202020111101112201212222010100221022202020022001112022022022221202022202222122222212121021220102010120200221020221112120122212210122011221220101220000021012021102122002200220002110221221212100220000010112020120212211020212220121222200021212202112200121201002121221102001122221200121001002202020022102111210222221022101222222012220222110220222211210201102120122212221121202210021222222021002202012212021112120111020011111201112102102212202020011021111000202212221000200121102110022000220101221122110102000022212020012102001021012010002202202002010222201201112100 \ No newline at end of file diff --git a/problems/day08.html b/problems/day08.html new file mode 100644 index 0000000..9d52874 --- /dev/null +++ b/problems/day08.html @@ -0,0 +1,162 @@ + + + + +Day 8 - Advent of Code 2019 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 16*

   0x0000|2019

+ + + +
+ +

--- Day 8: Space Image Format ---

The Elves' spirits are lifted when they realize you have an opportunity to reboot one of their Mars rovers, and so they are curious if you would spend a brief sojourn on Mars. You land your ship near the rover.

+

When you reach the rover, you discover that it's already in the process of rebooting! It's just waiting for someone to enter a BIOS password. The Elf responsible for the rover takes a picture of the password (your puzzle input) and sends it to you via the Digital Sending Network.

+

Unfortunately, images sent via the Digital Sending Network aren't encoded with any normal encoding; instead, they're encoded in a special Space Image Format. None of the Elves seem to remember why this is the case. They send you the instructions to decode it.

+

Images are sent as a series of digits that each represent the color of a single pixel. The digits fill each row of the image left-to-right, then move downward to the next row, filling rows top-to-bottom until every pixel of the image is filled.

+

Each image actually consists of a series of identically-sized layers that are filled in this way. So, the first digit corresponds to the top-left pixel of the first layer, the second digit corresponds to the pixel to the right of that on the same layer, and so on until the last digit, which corresponds to the bottom-right pixel of the last layer.

+

For example, given an image 3 pixels wide and 2 pixels tall, the image data 123456789012 corresponds to the following image layers:

+
Layer 1: 123
+         456
+
+Layer 2: 789
+         012
+
+

The image you received is 25 pixels wide and 6 pixels tall.

+

To make sure the image wasn't corrupted during transmission, the Elves would like you to find the layer that contains the fewest 0 digits. On that layer, what is the number of 1 digits multiplied by the number of 2 digits?

+
+

Your puzzle answer was 2520.

--- Part Two ---

Now you're ready to decode the image. The image is rendered by stacking the layers and aligning the pixels with the same positions in each layer. The digits indicate the color of the corresponding pixel: 0 is black, 1 is white, and 2 is transparent.

+

The layers are rendered with the first layer in front and the last layer in back. So, if a given position has a transparent pixel in the first and second layers, a black pixel in the third layer, and a white pixel in the fourth layer, the final image would have a black pixel at that position.

+

For example, given an image 2 pixels wide and 2 pixels tall, the image data 0222112222120000 corresponds to the following image layers:

+
Layer 1: 02
+         22
+
+Layer 2: 11
+         22
+
+Layer 3: 22
+         12
+
+Layer 4: 00
+         00
+
+

Then, the full image can be found by determining the top visible pixel in each position:

+
    +
  • The top-left pixel is black because the top layer is 0.
  • +
  • The top-right pixel is white because the top layer is 2 (transparent), but the second layer is 1.
  • +
  • The bottom-left pixel is white because the top two layers are 2, but the third layer is 1.
  • +
  • The bottom-right pixel is black because the only visible pixel in that position is 0 (from layer 4).
  • +
+

So, the final image looks like this:

+
01
+10
+
+

What message is produced after decoding your image?

+
+

Your puzzle answer was LEGJY.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index b087b0a..b855254 100644 --- a/stack.yaml +++ b/stack.yaml @@ -44,6 +44,7 @@ packages: - advent05 - advent06 - advent07 +- advent08 # Dependency packages to be pulled from upstream that are not in the resolver. -- 2.34.1