From d7d6a1ba093b4ae8535bcd72bfca94be5b66ec7a Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 13 Dec 2021 15:15:30 +0000 Subject: [PATCH] Done day 13 --- advent-of-code21.cabal | 5 + advent13/Main.hs | 83 ++++ data/advent13.txt | 921 +++++++++++++++++++++++++++++++++++++++++ problems/day13.html | 218 ++++++++++ 4 files changed, 1227 insertions(+) create mode 100644 advent13/Main.hs create mode 100644 data/advent13.txt create mode 100644 problems/day13.html diff --git a/advent-of-code21.cabal b/advent-of-code21.cabal index ac846cf..90d342a 100644 --- a/advent-of-code21.cabal +++ b/advent-of-code21.cabal @@ -144,3 +144,8 @@ executable advent12 import: common-extensions, build-directives main-is: advent12/Main.hs build-depends: text, attoparsec, containers + +executable advent13 + import: common-extensions, build-directives + main-is: advent13/Main.hs + build-depends: text, attoparsec, containers, linear diff --git a/advent13/Main.hs b/advent13/Main.hs new file mode 100644 index 0000000..69afc49 --- /dev/null +++ b/advent13/Main.hs @@ -0,0 +1,83 @@ +-- Writeup at https://work.njae.me.uk/2021/12/09/advent-of-code-2021-day-8/ + + +import Data.Text () +import qualified Data.Text.IO as TIO + +import Data.Attoparsec.Text +import Control.Applicative + +import qualified Data.Set as S +-- import Data.Set ((\\)) +import Linear (V2(..), (^+^)) +import Data.List + + +type Coord = V2 Int +type Sheet = S.Set Coord + +data Axis = X | Y + deriving (Eq, Ord, Show) + +data Fold = Fold Axis Int + deriving (Eq, Ord, Show) + + + +main :: IO () +main = + do text <- TIO.readFile "data/advent13.txt" + let (sheet, folds) = successfulParse text + print $ part1 sheet folds + putStrLn $ part2 sheet folds + +part1 :: Sheet -> [Fold] -> Int +part1 sheet folds = S.size $ doFold sheet $ head folds + +part2 :: Sheet -> [Fold] -> String +part2 sheet folds = showSheet foldedSheet + where foldedSheet = foldl' doFold sheet folds + +doFold :: Sheet -> Fold -> Sheet +doFold sheet (Fold X i) = S.union left foldedRight + where (left, right) = S.partition ((<= i) . getX) sheet + foldedRight = S.map foldDot right + foldDot (V2 x y) = V2 (i - (x - i)) y + +doFold sheet (Fold Y i) = S.union top foldedBottom + where (top, bottom) = S.partition ((<= i) . getY) sheet + foldedBottom = S.map foldDot bottom + foldDot (V2 x y) = V2 x (i - (y - i)) + +getX :: Coord -> Int +getX (V2 x _) = x + +getY :: Coord -> Int +getY (V2 _ y) = y + + +showSheet :: Sheet -> String +showSheet sheet = unlines [ concat [showPoint (V2 x y) | x <- ([0..maxX] :: [Int]) + ] | y <- ([0..maxY] :: [Int]) ] + where showPoint here = if here `S.member` sheet then "█" else " " + maxX = S.findMax $ S.map getX sheet + maxY = S.findMax $ S.map getY sheet + + +-- Parse the input file + +inputP = (,) <$> sheetP <* (many1 endOfLine) <*> foldsP + +sheetP = S.fromList <$> (dotP `sepBy` endOfLine) +dotP = V2 <$> decimal <* "," <*> decimal + +foldsP = foldP `sepBy` endOfLine +foldP = Fold <$> ("fold along " *> axisP) <* "=" <*> decimal + +axisP = ("x" *> pure X) <|> ("y" *> pure Y) + +-- successfulParse :: Text -> (Integer, [Maybe Integer]) +successfulParse input = + case parseOnly inputP input of + Left _err -> (S.empty, []) -- TIO.putStr $ T.pack $ parseErrorPretty err + Right indata -> indata diff --git a/data/advent13.txt b/data/advent13.txt new file mode 100644 index 0000000..3de5d53 --- /dev/null +++ b/data/advent13.txt @@ -0,0 +1,921 @@ +994,18 +1014,343 +84,406 +219,849 +570,488 +575,395 +387,213 +904,382 +676,479 +1028,437 +500,123 +202,735 +716,315 +1153,822 +197,397 +678,154 +1104,285 +1041,799 +262,159 +925,543 +1193,299 +918,159 +1170,826 +1181,889 +938,9 +843,481 +5,480 +95,416 +944,831 +853,285 +276,108 +435,717 +616,383 +766,893 +863,611 +442,521 +5,446 +272,26 +1146,876 +244,845 +117,747 +457,397 +336,235 +249,169 +612,443 +251,693 +67,333 +376,840 +16,719 +1190,617 +95,857 +955,511 +984,180 +134,613 +413,145 +1141,416 +681,830 +162,308 +411,70 +1041,239 +184,382 +460,791 +798,79 +1161,429 +238,450 +649,296 +445,53 +616,495 +244,519 +669,781 +15,105 +157,296 +411,824 +713,450 +922,311 +373,173 +5,190 +1156,347 +1262,392 +441,656 +984,744 +810,123 +216,1 +395,185 +930,893 +669,864 +934,600 +160,121 +1170,628 +713,562 +512,290 +1058,618 +1294,719 +910,683 +338,661 +910,298 +172,547 +694,495 +1041,95 +875,717 +269,95 +902,385 +599,737 +825,693 +1232,848 +1138,235 +887,640 +1066,845 +13,47 +1272,511 +622,289 +1190,277 +1148,586 +776,3 +780,271 +713,239 +649,25 +107,513 +35,799 +825,84 +363,565 +609,621 +1250,708 +1121,605 +401,668 +306,810 +880,635 +628,98 +885,504 +952,694 +1116,254 +80,98 +947,351 +1067,152 +406,270 +1059,693 +1102,238 +785,30 +232,311 +1232,64 +994,876 +1133,429 +1260,330 +524,386 +167,324 +28,393 +132,516 +1305,296 +72,698 +385,254 +475,477 +587,287 +1049,168 +142,64 +172,235 +1002,679 +430,515 +741,593 +528,289 +768,266 +716,260 +910,235 +1305,704 +870,399 +1146,18 +1077,751 +281,53 +830,164 +306,35 +142,127 +610,880 +480,705 +1113,397 +257,51 +734,46 +649,598 +300,322 +1058,52 +913,843 +638,175 +1200,893 +1158,276 +1131,346 +1066,729 +669,47 +55,269 +1141,362 +774,208 +387,661 +842,555 +1181,617 +84,488 +85,301 +356,277 +196,297 +152,637 +274,221 +423,640 +1267,283 +1116,444 +412,854 +256,690 +656,240 +1278,737 +90,65 +522,516 +639,789 +527,351 +445,343 +576,848 +408,660 +1124,339 +641,113 +890,354 +239,611 +141,768 +1131,122 +413,749 +276,142 +937,534 +1133,1 +1148,296 +1086,483 +5,704 +706,142 +311,339 +915,185 +80,576 +103,294 +840,767 +928,607 +45,595 +710,714 +902,770 +236,325 +947,187 +1180,386 +1156,464 +251,201 +452,436 +1056,7 +214,292 +162,596 +415,425 +1297,864 +858,21 +922,235 +448,436 +632,292 +1156,99 +1158,637 +425,106 +785,478 +595,9 +1275,203 +971,28 +1255,269 +813,598 +1034,752 +974,222 +773,501 +1186,873 +788,378 +246,621 +1036,221 +50,330 +1118,225 +944,887 +843,654 +892,688 +10,170 +624,327 +59,325 +796,404 +560,583 +634,158 +430,820 +361,771 +1004,84 +848,732 +1020,577 +570,630 +951,255 +231,702 +186,555 +239,835 +177,270 +626,617 +597,2 +1068,488 +70,879 +939,507 +997,781 +202,383 +1174,124 +142,290 +1086,820 +763,269 +1246,383 +276,451 +308,215 +1232,512 +594,442 +1068,157 +1240,15 +22,451 +1017,729 +788,516 +35,95 +897,609 +189,605 +267,824 +701,299 +0,395 +447,163 +900,434 +691,691 +1062,270 +758,288 +1148,308 +902,124 +880,596 +796,490 +492,596 +470,319 +874,343 +535,275 +110,449 +894,2 +798,736 +1278,157 +932,786 +1226,712 +216,893 +186,714 +457,53 +1265,299 +1251,549 +959,688 +974,235 +631,861 +1161,653 +895,835 +59,9 +686,648 +1200,449 +956,602 +149,494 +234,92 +328,296 +872,757 +910,659 +353,136 +206,609 +43,171 +470,799 +246,385 +610,14 +669,113 +527,95 +1074,617 +1110,737 +726,879 +1069,749 +701,621 +773,119 +835,865 +1049,280 +567,374 +447,843 +1287,879 +1233,501 +743,822 +1124,180 +621,165 +1047,49 +681,136 +171,276 +753,240 +1064,621 +108,294 +688,157 +557,218 +149,429 +1230,268 +576,325 +616,368 +996,784 +534,3 +1092,175 +850,444 +731,28 +1081,838 +1022,78 +392,63 +383,849 +728,401 +669,847 +441,238 +415,835 +937,165 +154,99 +618,868 +764,294 +1012,893 +853,53 +569,581 +1146,856 +261,589 +1071,59 +994,170 +619,169 +20,747 +291,259 +864,238 +505,848 +177,449 +311,789 +97,228 +1091,717 +691,203 +65,502 +242,488 +1043,233 +1156,547 +1266,213 +525,49 +54,42 +974,683 +887,752 +1280,556 +1260,623 +775,619 +984,296 +256,204 +296,255 +1230,57 +397,101 +186,443 +1066,273 +246,273 +1064,837 +316,101 +569,761 +341,702 +570,264 +780,67 +341,192 +759,121 +536,208 +582,151 +1086,411 +512,736 +430,635 +78,288 +80,823 +1230,161 +1071,649 +1044,659 +1243,633 +90,887 +773,53 +1104,443 +443,609 +753,228 +1168,255 +218,175 +579,649 +564,719 +393,838 +304,292 +954,277 +1004,35 +200,157 +1034,108 +622,597 +246,572 +1299,527 +1094,893 +874,712 +108,495 +552,288 +1168,527 +1173,512 +634,606 +1056,289 +910,605 +713,892 +1092,742 +629,830 +678,227 +514,404 +1207,600 +1287,127 +186,180 +559,702 +213,600 +818,596 +33,640 +542,266 +13,296 +1290,411 +736,268 +339,82 +1011,511 +363,777 +1036,1 +129,617 +11,866 +436,103 +1146,756 +1052,448 +587,561 +194,254 +1302,211 +1076,92 +1250,583 +22,555 +955,383 +728,679 +393,56 +574,268 +171,724 +1138,224 +1056,887 +982,843 +470,751 +219,717 +649,89 +567,163 +957,382 +184,738 +574,71 +1047,497 +1153,192 +863,843 +318,211 +669,270 +348,35 +366,7 +949,837 +875,177 +269,640 +452,10 +338,618 +735,395 +713,655 +55,848 +1220,569 +693,476 +880,515 +609,273 +306,84 +199,298 +440,602 +137,605 +1267,723 +38,383 +208,238 +457,192 +140,107 +810,99 +371,507 +995,759 +157,192 +300,154 +607,621 +721,502 +1238,698 +1290,288 +50,527 +59,549 +335,618 +154,795 +261,676 +1170,787 +452,603 +291,351 +164,876 +1049,589 +366,455 +964,343 +1052,446 +567,420 +944,63 +90,271 +818,242 +1290,362 +110,1 +624,567 +243,152 +137,289 +528,605 +925,450 +544,585 +0,499 +1113,702 +430,298 +691,169 +740,45 +55,401 +107,625 +336,211 +530,827 +676,736 +400,298 +1064,273 +140,266 +199,596 +276,752 +843,240 +1288,443 +1168,830 +537,327 +1091,625 +172,323 +77,841 +738,823 +728,493 +1233,841 +1074,773 +569,593 +78,64 +1161,150 +474,831 +475,865 +904,737 +416,2 +688,289 +547,401 +80,621 +785,497 +746,7 +1069,637 +1297,296 +932,183 +783,635 +977,805 +241,749 +162,296 +594,260 +678,718 +979,324 +261,305 +782,93 +1092,623 +1049,726 +1216,311 +1020,317 +574,318 +1113,192 +194,327 +791,698 +773,327 +880,35 +259,261 +743,693 +1299,28 +1011,383 +525,254 +1305,414 +120,891 +962,604 +1280,567 +514,490 +11,28 +266,191 +1299,418 +892,211 +1173,323 +599,158 +1074,586 +678,740 +1180,829 +1092,719 +557,228 +214,714 +423,254 +1153,744 +692,868 +569,285 +726,463 +537,53 +1302,670 +1250,442 +723,561 +184,512 +520,613 +808,637 +72,316 +599,718 +796,68 +54,266 +952,639 +1278,297 +853,217 +1161,465 +567,245 +546,600 +932,527 +129,169 +574,519 +400,45 +743,474 +683,63 +219,625 +654,240 +402,394 +202,159 +1285,512 +885,390 +1019,19 +157,744 +927,176 +500,464 +177,429 +686,327 +830,730 +257,101 +997,593 +1056,148 +853,609 +828,771 +1078,311 +517,323 +567,72 +1074,596 +70,239 +372,661 +753,676 +1225,761 +8,352 +1133,744 +785,49 +1118,221 +1184,829 +23,575 +254,849 +632,624 +254,887 +641,623 +269,444 +351,688 +293,729 +254,148 +734,569 +574,621 +621,254 +410,210 +631,879 +72,663 +184,95 +8,318 +923,661 +1203,269 +482,99 +938,681 +1285,288 +600,714 +868,521 +691,317 +1272,495 +1043,824 +741,285 +820,74 +1233,53 +728,148 +984,598 +987,772 +326,296 +734,288 +1089,136 +587,261 +1240,879 +734,325 +90,623 +467,15 +1183,707 +80,733 +124,67 +514,826 +1235,245 +400,235 +1310,395 +1148,596 +1181,497 +971,866 +392,159 +713,444 +1299,866 +254,743 +32,597 +644,276 +1074,271 +885,106 +629,382 +87,245 +616,526 +363,187 +795,464 +740,264 +252,52 +191,420 +328,322 +520,501 +326,843 +869,238 +1215,857 +887,254 +274,1 +528,93 +927,830 +1202,399 +452,291 +865,343 +1041,640 +120,667 +132,416 +750,535 +845,289 +406,737 +1058,276 +557,464 +52,893 +604,142 +1081,676 +887,37 +272,868 +805,17 +1126,319 +564,623 +482,771 +956,322 +731,649 +1034,443 +811,394 +569,313 +412,830 +962,479 +904,624 +656,372 +710,490 +1255,401 +679,861 +551,121 +632,227 +1178,192 +977,744 +992,211 +1086,158 +378,527 +1297,47 +410,729 +438,85 +298,445 +582,764 +1251,773 +1000,719 +507,794 +909,668 +969,478 +843,413 +1308,101 +1230,716 +129,254 +458,745 +346,551 +313,593 +761,127 +1053,101 +304,602 +900,281 +1104,3 +736,57 +336,672 +134,137 +43,283 +746,439 +609,299 +1232,606 +689,254 +124,21 +1280,191 +715,9 +25,288 +869,656 +848,757 +870,752 +224,483 +915,136 +132,444 +1153,254 +552,606 +617,628 +177,445 +341,478 +348,290 +547,269 +682,98 +661,805 +743,374 +984,843 +236,569 +452,425 +646,542 +388,659 +1006,292 +339,28 + +fold along x=655 +fold along y=447 +fold along x=327 +fold along y=223 +fold along x=163 +fold along y=111 +fold along x=81 +fold along y=55 +fold along x=40 +fold along y=27 +fold along y=13 +fold along y=6 diff --git a/problems/day13.html b/problems/day13.html new file mode 100644 index 0000000..180477f --- /dev/null +++ b/problems/day13.html @@ -0,0 +1,218 @@ + + + + +Day 13 - Advent of Code 2021 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 26*

      /^2021$/

+ + + +
+ +

--- Day 13: Transparent Origami ---

You reach another volcanically active part of the cave. It would be nice if you could do some kind of thermal imaging so you could tell ahead of time which caves are too hot to safely enter.

+

Fortunately, the submarine seems to be equipped with a thermal camera! When you activate it, you are greeted with:

+
Congratulations on your purchase! To activate this infrared thermal imaging
+camera system, please enter the code found on page 1 of the manual.
+
+

Apparently, the Elves have never used this feature. To your surprise, you manage to find the manual; as you go to open it, page 1 falls out. It's a large sheet of transparent paper! The transparent paper is marked with random dots and includes instructions on how to fold it up (your puzzle input). For example:

+
6,10
+0,14
+9,10
+0,3
+10,4
+4,11
+6,0
+6,12
+4,1
+0,13
+10,12
+3,4
+3,0
+8,4
+1,10
+2,14
+8,10
+9,0
+
+fold along y=7
+fold along x=5
+
+

The first section is a list of dots on the transparent paper. 0,0 represents the top-left coordinate. The first value, x, increases to the right. The second value, y, increases downward. So, the coordinate 3,0 is to the right of 0,0, and the coordinate 0,7 is below 0,0. The coordinates in this example form the following pattern, where # is a dot on the paper and . is an empty, unmarked position:

+
...#..#..#.
+....#......
+...........
+#..........
+...#....#.#
+...........
+...........
+...........
+...........
+...........
+.#....#.##.
+....#......
+......#...#
+#..........
+#.#........
+
+

Then, there is a list of fold instructions. Each instruction indicates a line on the transparent paper and wants you to fold the paper up (for horizontal y=... lines) or left (for vertical x=... lines). In this example, the first fold instruction is fold along y=7, which designates the line formed by all of the positions where y is 7 (marked here with -):

+
...#..#..#.
+....#......
+...........
+#..........
+...#....#.#
+...........
+...........
+-----------
+...........
+...........
+.#....#.##.
+....#......
+......#...#
+#..........
+#.#........
+
+

Because this is a horizontal line, fold the bottom half up. Some of the dots might end up overlapping after the fold is complete, but dots will never appear exactly on a fold line. The result of doing this fold looks like this:

+
#.##..#..#.
+#...#......
+......#...#
+#...#......
+.#.#..#.###
+...........
+...........
+
+

Now, only 17 dots are visible.

+

Notice, for example, the two dots in the bottom left corner before the transparent paper is folded; after the fold is complete, those dots appear in the top left corner (at 0,0 and 0,1). Because the paper is transparent, the dot just below them in the result (at 0,3) remains visible, as it can be seen through the transparent paper.

+

Also notice that some dots can end up overlapping; in this case, the dots merge together and become a single dot.

+

The second fold instruction is fold along x=5, which indicates this line:

+
#.##.|#..#.
+#...#|.....
+.....|#...#
+#...#|.....
+.#.#.|#.###
+.....|.....
+.....|.....
+
+

Because this is a vertical line, fold left:

+
#####
+#...#
+#...#
+#...#
+#####
+.....
+.....
+
+

The instructions made a square!

+

The transparent paper is pretty big, so for now, focus on just completing the first fold. After the first fold in the example above, 17 dots are visible - dots that end up overlapping after the fold is completed count as a single dot.

+

How many dots are visible after completing just the first fold instruction on your transparent paper?

+
+

Your puzzle answer was 763.

--- Part Two ---

Finish folding the transparent paper according to the instructions. The manual says the code is always eight capital letters.

+

What code do you use to activate the infrared thermal imaging camera system?

+
+

Your puzzle answer was RHALRCRA.

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 -- 2.34.1