From 5adf034e6ea7e99e668174d6db227d1da01798bb Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 13 Dec 2021 01:20:33 -0500 Subject: day 13 --- aoc2021.test/DayTests.cs | 19 + aoc2021/Day13.cs | 82 ++++ aoc2021/input/day13.in | 1030 ++++++++++++++++++++++++++++++++++++++++++++++ aoc2021/input/test13.in | 21 + 4 files changed, 1152 insertions(+) create mode 100644 aoc2021/Day13.cs create mode 100644 aoc2021/input/day13.in create mode 100644 aoc2021/input/test13.in diff --git a/aoc2021.test/DayTests.cs b/aoc2021.test/DayTests.cs index 04b9d4f..4b27888 100644 --- a/aoc2021.test/DayTests.cs +++ b/aoc2021.test/DayTests.cs @@ -3,6 +3,23 @@ namespace aoc2021.test; [TestClass] public class DayTests { + private const string Day13Actual = @" +████▒███▒▒████▒▒██▒▒█▒▒█▒▒██▒▒█▒▒█▒█▒▒█ +█▒▒▒▒█▒▒█▒▒▒▒█▒█▒▒█▒█▒█▒▒█▒▒█▒█▒▒█▒█▒▒█ +███▒▒█▒▒█▒▒▒█▒▒█▒▒▒▒██▒▒▒█▒▒▒▒████▒█▒▒█ +█▒▒▒▒███▒▒▒█▒▒▒█▒██▒█▒█▒▒█▒▒▒▒█▒▒█▒█▒▒█ +█▒▒▒▒█▒▒▒▒█▒▒▒▒█▒▒█▒█▒█▒▒█▒▒█▒█▒▒█▒█▒▒█ +████▒█▒▒▒▒████▒▒███▒█▒▒█▒▒██▒▒█▒▒█▒▒██▒ +"; + + private const string Day13Test = @" +█████ +█▒▒▒█ +█▒▒▒█ +█▒▒▒█ +█████ +"; + [DataTestMethod] [DataRow(typeof(Day01), "1616", "1645")] [DataRow(typeof(Day02), "2272262", "2134882034")] @@ -16,6 +33,7 @@ public class DayTests [DataRow(typeof(Day10), "288291", "820045242")] [DataRow(typeof(Day11), "1613", "510")] [DataRow(typeof(Day12), "4549", "120535")] + [DataRow(typeof(Day13), "837", Day13Actual)] public void CheckAllDays(Type dayType, string part1, string part2) { var s = Stopwatch.StartNew(); @@ -58,6 +76,7 @@ public class DayTests [DataRow(typeof(Day10), "26397", "288957")] [DataRow(typeof(Day11), "1656", "195")] [DataRow(typeof(Day12), "226", "3509")] + [DataRow(typeof(Day13), "17", Day13Test)] public void CheckTestInputs(Type dayType, string part1, string part2) { Day.UseTestInput = true; diff --git a/aoc2021/Day13.cs b/aoc2021/Day13.cs new file mode 100644 index 0000000..1ad9016 --- /dev/null +++ b/aoc2021/Day13.cs @@ -0,0 +1,82 @@ +using MoreLinq; + +namespace aoc2021; + +/// +/// Day 13: +/// +public sealed class Day13 : Day +{ + private List<(int x, int y)> _grid; + private readonly List<(char axis, int index)> _folds; + + public Day13() : base(13, "Transparent Origami") + { + var s = Input.Split("").ToList(); + + _grid = s[0].Select(p => + { + var i = p.Split(',', 2).Select(int.Parse).ToList(); + return (i[0], i[1]); + }).ToList(); + + _folds = s[1].Select(p => p.Split(' ').Skip(2).First()).Select(p => + { + var i = p.Split('=', 2); + return (i[0][0], int.Parse(i[1])); + }).ToList(); + } + + private static List<(int x, int y)> DoFold(List<(int x, int y)> grid, char axis, int at) + { + List<(int, int)> result = new(); + + switch (axis) + { + case 'x': + foreach (var (x, y) in grid) + result.Add((x > at ? 2 * at - x : x, y)); + break; + case 'y': + foreach (var (x, y) in grid) + result.Add((x, y > at ? 2 * at - y : y)); + break; + default: + throw new ArgumentException("invalid fold axis", nameof(axis)); + } + + return result.Distinct().ToList(); + } + + private string PrintGrid() + { + var xMax = _grid.Max(g => g.x); + var yMax = _grid.Max(g => g.y); + var s = new StringBuilder(); + + for (var y = 0; y <= yMax; y++) + { + for (var x = 0; x <= xMax; x++) + s.Append(_grid.Contains((x, y)) ? "█" : "▒"); + + s.AppendLine(); + } + + return s.ToString(); + } + + public override object Part1() + { + var (axis, at) = _folds[0]; + var foldedOnce = DoFold(_grid, axis, at); + return foldedOnce.Count; + } + + public override object Part2() + { + foreach (var (axis, at) in _folds) + _grid = DoFold(_grid, axis, at); + + return Environment.NewLine + PrintGrid(); + } +} diff --git a/aoc2021/input/day13.in b/aoc2021/input/day13.in new file mode 100644 index 0000000..4205957 --- /dev/null +++ b/aoc2021/input/day13.in @@ -0,0 +1,1030 @@ +522,329 +174,466 +242,714 +785,686 +378,596 +335,353 +229,44 +1160,19 +5,318 +489,63 +484,275 +818,774 +214,882 +1128,519 +1143,432 +627,217 +669,544 +306,191 +878,199 +579,376 +649,121 +1004,191 +1017,838 +519,329 +62,228 +249,733 +1275,108 +569,173 +1297,686 +23,187 +5,576 +823,462 +730,395 +1049,309 +843,35 +164,364 +448,373 +50,372 +74,723 +970,539 +569,33 +579,605 +1126,750 +801,742 +525,726 +293,844 +334,686 +21,262 +514,617 +1303,541 +816,136 +211,284 +283,833 +887,119 +800,823 +559,131 +1077,695 +579,206 +764,350 +492,120 +1103,803 +229,850 +582,785 +1124,442 +676,863 +897,350 +45,32 +126,777 +1256,621 +853,862 +550,771 +1299,723 +328,728 +907,737 +296,310 +110,592 +780,850 +822,72 +1081,44 +964,649 +443,817 +681,8 +853,638 +796,617 +1148,728 +878,647 +371,385 +826,619 +820,728 +413,481 +136,145 +585,871 +549,187 +554,133 +112,166 +1092,621 +299,14 +1002,245 +1078,100 +1198,186 +104,535 +710,427 +197,549 +1067,47 +1144,136 +643,115 +361,56 +811,68 +452,486 +146,700 +822,408 +641,462 +346,801 +1232,24 +966,749 +160,584 +1151,462 +853,222 +1241,751 +457,704 +378,298 +109,341 +160,310 +324,310 +1051,651 +502,413 +103,856 +997,649 +1061,544 +515,483 +1183,359 +537,558 +401,801 +579,768 +827,93 +63,406 +0,838 +1084,245 +1001,70 +1275,862 +328,812 +984,728 +1285,438 +167,208 +1268,374 +392,609 +145,51 +793,771 +1069,798 +186,442 +716,469 +604,371 +904,439 +217,887 +661,859 +284,374 +542,855 +465,347 +393,596 +607,670 +1210,627 +887,327 +1272,856 +682,117 +832,436 +552,421 +472,519 +1299,733 +939,385 +1054,19 +560,772 +1283,238 +248,551 +1260,820 +907,157 +341,21 +853,32 +728,109 +127,550 +768,186 +129,799 +1166,381 +440,858 +1198,166 +1215,543 +509,742 +783,359 +981,696 +0,306 +1141,564 +616,40 +653,738 +1086,535 +110,32 +492,225 +1093,887 +214,12 +1237,488 +155,765 +1310,52 +764,670 +847,551 +488,150 +378,227 +1247,406 +45,823 +1113,856 +187,483 +344,749 +1303,801 +1210,425 +42,822 +60,546 +811,765 +648,686 +542,75 +222,199 +912,467 +841,780 +243,399 +1263,670 +30,758 +1048,609 +115,593 +731,518 +50,591 +345,70 +723,651 +92,770 +17,371 +957,792 +350,135 +656,56 +465,858 +378,746 +174,428 +689,56 +895,289 +485,240 +567,498 +1165,152 +497,16 +1049,522 +1198,523 +232,497 +915,686 +50,599 +791,329 +589,309 +616,380 +1198,728 +190,19 +237,68 +47,670 +621,681 +422,322 +373,303 +288,745 +1200,592 +545,679 +1088,199 +1208,85 +335,93 +326,728 +743,396 +981,472 +1144,758 +110,750 +932,390 +1119,791 +964,351 +1193,784 +503,588 +180,28 +706,295 +253,241 +853,670 +756,627 +1034,876 +20,253 +633,296 +432,199 +970,114 +584,845 +1078,397 +293,96 +288,518 +740,287 +691,456 +344,642 +194,5 +713,75 +828,855 +72,221 +845,347 +169,635 +528,49 +1113,336 +447,539 +102,546 +1146,166 +1121,771 +306,703 +1226,852 +575,495 +527,135 +1181,224 +703,170 +1108,38 +428,649 +982,364 +1159,759 +512,302 +189,795 +641,544 +853,220 +494,856 +740,735 +1121,547 +413,413 +108,350 +87,371 +314,117 +659,278 +534,179 +144,890 +768,819 +872,862 +1081,421 +257,31 +964,693 +171,801 +766,123 +478,684 +669,208 +1071,137 +1290,880 +207,61 +594,298 +758,546 +870,690 +371,539 +1057,401 +845,858 +888,497 +913,397 +776,379 +1017,112 +129,560 +1150,584 +65,137 +52,183 +378,876 +880,535 +331,432 +870,280 +447,61 +102,473 +585,845 +1168,592 +1149,383 +679,707 +731,352 +1195,560 +1046,537 +1193,558 +904,455 +803,137 +982,504 +1277,56 +600,273 +69,845 +460,889 +89,577 +3,761 +5,240 +470,359 +986,584 +813,228 +269,411 +311,828 +497,635 +726,875 +771,301 +639,757 +224,535 +197,336 +591,182 +144,101 +748,49 +1280,115 +201,453 +1265,71 +909,801 +1238,225 +299,105 +1153,635 +17,523 +69,703 +85,686 +440,12 +361,504 +57,728 +621,838 +785,168 +403,157 +395,686 +624,136 +594,596 +1078,845 +197,793 +836,364 +218,672 +710,273 +490,550 +1111,851 +674,767 +846,875 +574,849 +1181,799 +254,486 +843,187 +838,4 +325,248 +1163,533 +405,96 +288,149 +627,117 +497,666 +325,24 +65,148 +21,150 +377,388 +910,486 +1201,553 +870,217 +1225,413 +1164,82 +850,889 +1158,481 +484,843 +120,179 +1165,483 +647,801 +499,826 +1004,703 +117,558 +341,686 +795,411 +1104,155 +176,427 +826,843 +662,14 +676,686 +855,550 +68,322 +206,494 +1299,171 +969,798 +363,704 +728,673 +1245,157 +1245,605 +1086,516 +1289,184 +815,392 +870,858 +1163,361 +1130,866 +436,96 +587,651 +127,359 +1277,325 +908,686 +1226,159 +550,571 +1185,161 +206,739 +54,621 +36,497 +977,325 +174,876 +949,350 +248,870 +1253,280 +870,815 +1250,544 +253,493 +947,704 +361,95 +585,703 +820,550 +710,467 +129,480 +1017,262 +683,217 +346,127 +992,716 +820,812 +582,221 +1265,519 +1022,380 +604,36 +1017,56 +7,93 +151,759 +1240,19 +1056,486 +1299,609 +760,571 +182,65 +483,93 +464,19 +1265,319 +1039,233 +960,135 +84,735 +142,592 +979,392 +607,170 +1250,425 +484,619 +5,688 +629,8 +676,31 +1251,528 +917,737 +1292,435 +1151,840 +1139,801 +1158,635 +152,413 +1134,436 +174,18 +1251,814 +189,771 +832,684 +184,144 +1049,585 +872,592 +60,21 +969,21 +838,890 +997,432 +78,870 +525,215 +1136,466 +233,616 +1078,794 +428,245 +495,392 +1265,480 +1049,421 +801,152 +1011,789 +919,264 +1049,697 +1113,38 +176,436 +561,891 +663,577 +488,584 +378,675 +989,325 +853,856 +1210,133 +1000,866 +408,805 +689,838 +1293,371 +197,215 +949,504 +574,89 +229,137 +313,432 +1260,303 +986,310 +716,298 +1014,584 +628,117 +1029,787 +1121,562 +211,560 +701,414 +1202,350 +847,343 +1265,862 +21,184 +1225,481 +654,56 +525,168 +472,228 +366,402 +969,686 +482,407 +145,499 +448,43 +818,120 +78,714 +949,544 +725,871 +525,110 +764,224 +760,459 +575,362 +840,359 +850,558 +654,838 +917,585 +621,280 +853,674 +176,210 +582,225 +1166,513 +647,317 +816,310 +611,763 +1263,224 +750,217 +985,248 +803,316 +65,309 +634,863 +932,18 +75,542 +780,44 +492,774 +1124,452 +962,852 +75,518 +467,35 +162,669 +266,574 +1210,469 +100,425 +962,583 +50,522 +129,663 +161,86 +306,852 +515,411 +110,526 +1181,543 +897,413 +1099,112 +499,68 +992,178 +189,123 +348,486 +190,427 +1061,96 +1268,624 +478,236 +677,296 +1164,700 +821,63 +796,890 +509,152 +798,592 +746,780 +510,101 +1069,544 +354,525 +514,4 +120,379 +904,551 +1125,780 +964,245 +276,667 +579,654 +785,320 +656,52 +1155,765 +309,70 +1084,627 +1215,95 +27,432 +146,252 +114,164 +109,789 +306,759 +706,523 +1200,526 +766,750 +1009,120 +440,79 +1307,761 +1220,780 +333,325 +164,812 +1019,332 +960,311 +328,504 +38,38 +800,549 +35,414 +1002,543 +73,499 +237,317 +1154,798 +945,453 +348,529 +1215,763 +826,499 +364,497 +498,843 +1029,107 +713,166 +276,504 +388,574 +190,145 +1017,504 +795,483 +795,35 +385,838 +785,768 +825,575 +478,458 +393,585 +710,621 +554,469 +698,270 +1093,7 +651,278 +1123,859 +100,627 +321,325 +713,523 +1184,117 +1232,714 +559,95 +1004,759 +1010,369 +306,255 +710,691 +373,367 +1200,32 +823,432 +42,744 +1096,79 +782,49 +631,483 +600,691 +628,777 +485,575 +160,136 +539,558 +783,135 +224,208 +1094,32 +232,546 +79,495 +1103,733 +820,166 +1113,32 +1241,255 +985,696 +915,651 +997,245 +253,353 +843,707 +110,302 +991,353 +648,880 +1136,876 +187,35 +45,71 +808,481 +364,397 +452,402 +423,119 +42,374 +766,144 +636,127 +726,845 +30,115 +661,35 +229,473 +691,480 +917,596 +1223,742 +731,376 +801,51 +843,411 +1245,869 +494,310 +569,861 +243,847 +514,890 +820,225 +1113,793 +776,627 +822,632 +11,723 +182,345 +863,803 +1139,689 +1084,649 +989,857 +467,483 +889,891 +1206,359 +395,880 +27,677 +713,411 +465,410 +701,527 +1057,849 +169,319 +669,96 +1128,345 +949,462 +966,642 +1183,135 +432,247 +915,880 +1277,838 +448,739 +467,187 +832,210 +472,381 +785,374 +564,780 +402,686 +1155,129 +62,666 +557,186 +668,220 +436,798 +393,309 +184,141 +465,123 +783,759 +619,414 +361,544 +1155,541 +1069,721 +530,44 +910,109 +924,871 +460,558 +440,751 +621,351 +191,791 +85,693 +749,278 +164,728 +822,744 +226,627 +1292,693 +256,427 +447,355 +982,812 +1260,522 +346,245 +129,112 +1119,551 +1250,21 +1241,143 +955,441 +1272,38 +525,231 +326,364 +1181,768 +276,743 +1052,453 +169,778 +1268,822 +1277,880 +299,432 +1285,786 +472,4 +129,208 +45,862 +874,544 +933,388 +760,435 +209,336 +627,656 +214,751 +585,639 +1195,593 +1126,141 +1181,208 +773,784 +233,423 +113,388 +23,259 +1126,359 +1235,799 +1136,428 +1245,137 +33,632 +257,143 +662,238 +152,635 +1057,541 +607,224 +557,708 +1238,673 +25,438 +1067,399 +351,411 +385,411 +539,294 +964,543 +940,821 +1081,473 +493,383 +1141,857 +155,516 +982,728 +129,224 +1198,763 +816,856 +959,411 +1084,693 +1053,143 +723,243 +1292,106 +731,654 +115,301 +760,771 +171,541 +569,693 +145,395 +197,668 +554,425 +771,558 +528,117 +653,47 +59,80 +567,396 +1303,353 +782,777 +147,361 +109,679 +42,72 +7,801 +348,408 +994,578 +12,562 +701,476 +115,560 +455,43 +329,696 +1166,890 +145,843 +619,862 +721,309 +1073,68 +1181,480 +338,316 +1004,135 +72,109 +880,473 +842,442 +1280,779 +546,497 +395,651 +1053,79 +1168,190 +436,126 +519,565 +84,852 +222,834 +60,544 +728,225 +933,170 +330,366 +1064,805 +457,856 +277,388 +124,520 +1260,599 +171,93 +1068,831 +59,528 +1195,301 +155,541 +508,194 +1305,576 +1207,38 +691,414 +262,609 +962,529 +54,19 +796,4 +365,441 +913,79 +100,469 +95,763 +1053,563 +390,515 +1168,302 +142,190 +802,252 +1113,215 +490,344 +1253,483 +552,546 +517,562 +550,359 +326,166 +1166,823 +542,708 +500,850 +512,592 +817,383 +166,758 +1141,654 +166,408 +768,131 +316,316 +589,148 +490,812 +1010,777 + +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/aoc2021/input/test13.in b/aoc2021/input/test13.in new file mode 100644 index 0000000..32a8563 --- /dev/null +++ b/aoc2021/input/test13.in @@ -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 \ No newline at end of file -- cgit 1.4.1