From 572ecbc94df870c065c9afc7f43db258c43b5d0a Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Mon, 13 Dec 2021 08:43:06 +0100 Subject: [PATCH] Added day 13! --- source/app.d | 4 +- source/day13/input.txt | 833 ++++++++++++++++++++++++++++++++++++ source/day13/part1.d | 91 ++++ source/day13/test_input.txt | 21 + 4 files changed, 947 insertions(+), 2 deletions(-) create mode 100644 source/day13/input.txt create mode 100644 source/day13/part1.d create mode 100644 source/day13/test_input.txt diff --git a/source/app.d b/source/app.d index 7da990b..2298917 100644 --- a/source/app.d +++ b/source/app.d @@ -1,7 +1,7 @@ import std.stdio; -import day12.part1; +import day13.part1; void main() { - passagePathing(); + transparentOrigami(); } diff --git a/source/day13/input.txt b/source/day13/input.txt new file mode 100644 index 0000000..5006d20 --- /dev/null +++ b/source/day13/input.txt @@ -0,0 +1,833 @@ +296,446 +559,232 +754,329 +1238,577 +1155,754 +378,674 +1064,567 +1310,206 +171,82 +1228,892 +890,114 +806,565 +408,558 +1210,347 +53,845 +636,836 +907,133 +1299,852 +967,333 +956,815 +1238,471 +1136,577 +853,849 +15,9 +522,392 +176,887 +877,67 +228,565 +952,861 +595,712 +715,264 +1043,854 +1014,476 +952,555 +94,199 +1019,297 +915,259 +242,338 +1014,787 +1236,770 +654,16 +453,562 +964,892 +1037,341 +964,129 +15,602 +546,367 +1089,422 +1099,477 +905,490 +279,477 +1078,404 +1298,439 +493,100 +566,12 +616,476 +1110,668 +1203,698 +1022,810 +1128,648 +1128,849 +967,490 +70,114 +1078,247 +624,28 +867,318 +529,546 +756,500 +1046,690 +1084,824 +642,96 +585,719 +209,88 +119,140 +771,29 +296,418 +1146,520 +1049,246 +1141,861 +1119,285 +554,652 +15,210 +478,7 +164,101 +788,616 +554,294 +954,77 +716,161 +703,831 +223,376 +1139,812 +771,128 +131,478 +895,649 +300,79 +1031,252 +1014,642 +668,439 +550,228 +904,450 +937,277 +867,352 +1266,780 +80,0 +1208,325 +1064,0 +406,682 +430,556 +1195,29 +1076,452 +1178,616 +527,570 +320,31 +373,165 +358,339 +453,105 +668,217 +788,866 +232,404 +862,502 +929,553 +813,47 +982,838 +1242,740 +952,221 +766,39 +100,500 +907,593 +223,294 +464,91 +981,336 +997,155 +326,56 +131,864 +100,347 +283,194 +1067,548 +1210,276 +544,631 +624,413 +744,124 +1222,124 +1002,577 +935,418 +1248,690 +964,450 +808,505 +1201,733 +107,644 +296,476 +131,416 +1002,836 +222,196 +1175,654 +567,369 +661,163 +1293,61 +1146,374 +576,58 +493,794 +699,656 +530,840 +662,863 +624,448 +309,509 +1109,777 +420,666 +482,414 +992,136 +296,787 +87,21 +1153,470 +1283,357 +354,367 +276,680 +1145,346 +510,648 +12,679 +552,674 +381,341 +300,815 +452,320 +54,712 +817,100 +271,298 +989,800 +857,49 +303,831 +688,108 +1149,234 +604,892 +274,71 +753,826 +258,359 +382,406 +87,873 +350,442 +68,154 +445,88 +750,266 +328,654 +185,740 +247,604 +554,515 +691,673 +782,338 +453,497 +1133,484 +264,690 +1218,218 +177,410 +365,343 +239,873 +431,600 +85,180 +371,327 +507,716 +962,214 +1092,367 +244,782 +801,509 +601,845 +1022,252 +882,376 +266,791 +164,569 +1243,187 +1027,700 +1148,392 +1216,880 +1056,541 +1195,542 +408,448 +977,94 +358,787 +554,276 +475,82 +12,889 +554,379 +1029,725 +264,196 +1101,88 +83,492 +202,616 +758,674 +299,509 +788,115 +1044,103 +234,4 +443,418 +858,320 +864,607 +756,518 +95,35 +648,863 +952,107 +267,854 +200,714 +80,205 +828,256 +366,252 +42,782 +1159,495 +1218,795 +600,264 +172,256 +969,199 +314,663 +857,789 +217,117 +1280,555 +1082,565 +1297,199 +634,2 +249,490 +132,616 +1228,444 +999,358 +765,733 +1156,190 +813,847 +311,358 +907,73 +1155,364 +1173,329 +289,343 +944,642 +922,469 +256,53 +251,593 +306,662 +666,375 +488,894 +1265,165 +642,217 +820,464 +393,745 +674,892 +761,248 +1210,547 +343,378 +294,562 +853,368 +74,770 +371,460 +105,24 +1113,849 +1039,746 +264,756 +507,194 +656,166 +289,551 +984,464 +1275,53 +381,553 +1,234 +209,806 +453,332 +624,866 +358,861 +567,525 +492,297 +619,225 +781,777 +1160,777 +174,374 +234,562 +557,182 +760,442 +30,866 +1205,472 +850,115 +544,756 +375,476 +97,826 +1139,292 +654,81 +296,225 +796,452 +880,338 +407,290 +68,311 +329,558 +42,334 +562,228 +1088,521 +999,404 +10,744 +654,688 +1071,873 +231,707 +264,652 +0,793 +1131,781 +1053,810 +959,831 +1200,301 +1185,49 +296,252 +308,836 +967,516 +1029,729 +320,353 +308,220 +154,190 +1081,516 +780,840 +1136,317 +1138,480 +443,318 +587,74 +475,44 +482,256 +639,716 +581,308 +1027,154 +1,660 +776,252 +35,112 +135,240 +403,73 +1231,458 +1006,890 +157,731 +271,746 +67,187 +1134,455 +927,596 +36,271 +343,113 +408,446 +890,452 +425,760 +765,777 +709,397 +642,455 +939,327 +446,607 +1223,817 +244,821 +1223,425 +1081,378 +229,378 +803,700 +877,198 +1231,436 +745,460 +550,754 +1149,112 +1066,112 +320,703 +1108,581 +165,560 +781,497 +1266,114 +249,154 +5,267 +1021,562 +35,745 +577,180 +1019,149 +87,369 +454,108 +428,376 +1139,28 +800,269 +715,182 +715,630 +750,647 +62,204 +632,676 +962,680 +758,220 +453,786 +582,245 +272,859 +38,476 +489,346 +488,450 +766,756 +161,212 +94,714 +465,117 +1056,247 +944,252 +222,890 +1287,511 +492,597 +283,740 +1250,14 +211,495 +686,339 +1241,308 +694,476 +1031,417 +490,430 +216,183 +157,175 +1146,430 +410,334 +1250,873 +313,259 +807,511 +686,28 +781,546 +556,329 +1022,84 +970,215 +251,749 +320,191 +1300,519 +1125,154 +529,777 +218,866 +595,630 +222,756 +341,199 +1108,313 +1288,442 +1228,17 +1230,894 +751,232 +643,607 +785,393 +884,693 +18,836 +1184,556 +311,404 +452,726 +1225,852 +750,247 +807,383 +477,483 +1014,252 +346,129 +433,67 +967,113 +37,252 +1251,182 +1054,332 +596,330 +867,399 +375,253 +443,399 +382,187 +594,161 +1116,115 +1248,204 +1218,556 +892,151 +1128,829 +387,712 +510,269 +594,385 +283,567 +366,642 +155,754 +88,770 +882,294 +639,516 +592,810 +373,185 +562,666 +69,623 +594,598 +199,169 +477,35 +482,480 +1049,872 +151,399 +1029,281 +211,477 +341,186 +1257,49 +502,505 +893,460 +967,714 +1238,201 +1222,322 +100,52 +1019,0 +944,532 +15,885 +1131,113 +309,385 +1002,220 +1213,68 +920,885 +281,725 +311,561 +398,562 +1191,551 +437,833 +62,662 +1149,794 +497,175 +428,204 +72,423 +1084,518 +653,212 +607,253 +1066,821 +89,460 +530,574 +1225,42 +952,787 +643,287 +488,24 +200,180 +699,208 +1185,565 +967,358 +1218,99 +497,399 +82,450 +1223,873 +1022,686 +1064,327 +976,353 +15,233 +601,117 +509,509 +405,852 +405,404 +1061,154 +489,782 +642,663 +611,602 +291,297 +1046,756 +733,180 +110,711 +291,8 +1029,169 +92,218 +821,782 +1205,291 +283,292 +882,204 +656,365 +822,0 +243,548 +443,417 +22,442 +288,686 +1086,14 +999,561 +974,758 +534,642 +407,436 +418,151 +1029,633 +351,831 +669,247 +1156,704 +60,780 +197,45 +759,331 +1295,806 +490,414 +997,259 +22,676 +989,94 +246,121 +161,334 +279,417 +668,5 +1179,416 +281,729 +15,806 +361,32 +126,338 +365,114 +365,623 +1099,417 +44,114 +756,276 +724,154 +373,281 +1257,845 +796,442 +1226,450 +592,612 +162,840 +530,54 +1287,383 +1242,647 +827,82 +221,422 +426,201 +756,618 +723,74 +234,452 +211,417 +857,105 +1027,140 +383,596 +283,700 +929,341 +306,641 +1310,345 +94,266 +1206,698 +667,287 +1029,261 +835,44 +631,63 +488,870 +433,696 +728,649 +454,786 +709,845 +366,810 +316,317 +1006,4 +246,327 +363,893 +676,450 +346,892 +457,401 +654,878 +313,35 +412,444 +1002,317 +104,666 +1029,617 +42,560 +1146,101 +355,206 +1295,602 +549,248 +676,2 +1111,469 +1093,565 +390,84 +161,660 +0,345 +1149,346 +723,820 +1036,599 +544,855 +1088,4 +530,392 +1275,112 +1019,393 +1166,625 +283,754 +542,81 +821,112 +862,392 +333,94 +420,452 +554,824 +699,292 +1298,889 +436,866 +788,278 +12,96 +1275,73 +912,562 +599,698 +586,602 +1238,319 +560,647 +1046,652 +902,448 +80,689 +242,444 +554,376 +281,261 +436,815 +1297,291 +201,49 +234,332 +796,890 +232,490 +679,319 +624,481 +288,84 +232,647 +11,287 +231,187 +244,429 +125,329 +0,206 +298,817 +1299,735 +1216,714 +994,317 +291,0 +221,472 +72,649 +171,866 +619,669 +248,506 +1149,334 +499,364 +490,325 +272,411 +607,831 +313,739 +318,136 +453,796 +1111,357 +609,729 +1064,110 +739,492 +817,794 +217,565 +218,367 +830,72 +1280,866 +418,743 +329,784 +60,598 +161,682 +932,276 +161,234 +600,859 +656,878 +82,892 +663,317 +904,444 +1228,450 +917,297 +503,383 +472,648 +88,124 +37,642 +1210,742 +248,836 +879,742 +1044,791 +1081,488 +768,81 +363,448 +872,711 +562,676 +1004,641 +144,625 +653,682 +756,416 +1010,815 +390,242 +191,14 +194,563 +1082,329 +229,516 +485,771 +162,480 +1191,343 +171,812 +10,822 +1138,256 +710,182 +1297,647 +182,648 +1014,446 +165,346 +748,228 + +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/source/day13/part1.d b/source/day13/part1.d new file mode 100644 index 0000000..3c36b83 --- /dev/null +++ b/source/day13/part1.d @@ -0,0 +1,91 @@ +module day13.part1; + +import std.file; +import std.stdio; +import std.string; +import std.conv; +import std.algorithm; +import std.array; + +struct Point { + int x; + int y; +} + +enum LineType { + HORIZONTAL, + VERTICAL +} + +struct Line { + LineType type; + int value; +} + +Point[] foldOver(ref Point[] points, Line line) { + Point[] newPoints = []; + void add(ref Point[] pts, Point p) { + if (!canFind(pts, p)) pts ~= p; + } + foreach (i, point; points) { + if (line.type == LineType.HORIZONTAL) { + if (point.y < line.value) { + add(newPoints, point); + } else { + int reflectedY = line.value - (point.y - line.value); + if (reflectedY < line.value && reflectedY >= 0) { + add(newPoints, Point(point.x, reflectedY)); + } + } + } else if (line.type == LineType.VERTICAL) { + if (point.x < line.value) { + add(newPoints, point); + } else { + int reflectedX = line.value - (point.x - line.value); + if (reflectedX < line.value && reflectedX >= 0) { + add(newPoints, Point(reflectedX, point.y)); + } + } + } + } + return newPoints; +} + +void print(Point[] points) { + int maxX = points.map!(p => p.x).maxElement; + int maxY = points.map!(p => p.y).maxElement; + foreach (y; 0..maxY + 1) { + foreach (x; 0..maxX + 1) { + bool pointExists = points.any!(p => p.x == x && p.y == y); + writef("%c", pointExists ? '#' : '.'); + } + writeln; + } +} + +void transparentOrigami() { + string[] textBlocks = readText("source/day13/input.txt").strip.split("\r\n\r\n"); + writeln(textBlocks); + Point[] points = textBlocks[0].split("\n") + .filter!(line => line.strip.length > 0) + .map!((line) { + string[] parts = line.strip.split(","); + return Point(parts[0].to!int, parts[1].to!int); + }).array; + Line[] lines = textBlocks[1].split("\n") + .map!((line) { + string[] parts = line.strip.split("="); + LineType type = parts[0][$ - 1] == 'x' ? LineType.VERTICAL : LineType.HORIZONTAL; + int value = parts[1].to!int; + return Line(type, value); + }).array; + + points.writeln; + lines.writeln; + + foreach (i, line; lines) { + points = foldOver(points, line); + writefln("After fold %d, %d dots are visible.", i + 1, points.length); + } + print(points); +} \ No newline at end of file diff --git a/source/day13/test_input.txt b/source/day13/test_input.txt new file mode 100644 index 0000000..282114c --- /dev/null +++ b/source/day13/test_input.txt @@ -0,0 +1,21 @@ +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