diff --git a/challenges/2021/13.js b/challenges/2021/13.js new file mode 100644 index 0000000..949a115 --- /dev/null +++ b/challenges/2021/13.js @@ -0,0 +1,83 @@ +// Part 1 +const solvePuzzle1 = (input) => { + let [paper, instructions] = getPaperAndInstructions(parseInput(input)); + + return fold(instructions[0], paper) + .flat() + .reduce((acc, val) => acc + val); +}; + +// Part 2 +const solvePuzzle2 = (input) => { + let [paper, instructions] = getPaperAndInstructions(parseInput(input)); + instructions.forEach((ins) => (paper = fold(ins, paper))); + + return "\n" + paper.map((y) => y.reduce((acc, x) => (x ? acc + "#" : acc + " "), "")).join("\n"); +}; + +const fold = ([dir, pos], paper) => { + if (dir === "y") { + const firstHalf = paper.slice(0, pos); + const secondHalf = paper.slice(pos + 1).reverse(); + paper = []; + + while (firstHalf.length > secondHalf.length) { + secondHalf.unshift(Array(firstHalf[0].length).fill(0)); + } + while (firstHalf.length < secondHalf.length) { + firstHalf.unshift(Array(firstHalf[0].length).fill(0)); + } + + for (let i = 0; i < firstHalf.length; i++) { + paper.push([]); + for (let j = 0; j < firstHalf[i].length; j++) { + paper[i][j] = firstHalf[i][j] || secondHalf[i][j]; + } + } + } else { + const firstHalf = Array.from({ length: paper.length }, () => []); + const secondHalf = Array.from({ length: paper.length }, () => []); + + for (let i = 0; i < paper.length; i++) { + for (let j = 0; j < pos; j++) { + firstHalf[i].push(paper[i][j]); + } + for (let j = paper[i].length - 1; j > pos; j--) { + secondHalf[i].push(paper[i][j]); + } + } + + paper = Array.from({ length: paper.length }, () => []); + for (let i = 0; i < firstHalf.length; i++) { + for (let j = 0; j < Math.max(firstHalf[i].length, secondHalf[i].length); j++) { + paper[i].push(Math.max(firstHalf[i][j] || secondHalf[i][j])); + } + } + } + return paper; +}; + +const getPaperAndInstructions = (input) => { + const [dots, instructions] = [[], []]; + let [maxX, maxY] = [0, 0]; + + for (let line of input) { + if (line[0] === "f") { + const [direction, pos] = line.replace("fold along ", "").split("="); + instructions.push([direction, Number(pos)]); + } else { + const [x, y] = line.split(",").map((n) => Number(n)); + dots.push([x, y]); + [maxX, maxY] = [Math.max(maxX, x), Math.max(maxY, y)]; + } + } + + const paper = Array.from({ length: maxY + 1 }, () => Array(maxX + 1).fill(0)); + dots.forEach(([x, y]) => (paper[y][x] = 1)); + return [paper, instructions]; +}; + +const parseInput = (input) => input.split("\n").filter((x) => x !== ""); + +require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle1); +require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle2); diff --git a/challenges/2021/inputs/13.txt b/challenges/2021/inputs/13.txt new file mode 100644 index 0000000..bf8c9bb --- /dev/null +++ b/challenges/2021/inputs/13.txt @@ -0,0 +1,880 @@ +957,648 +617,33 +915,323 +760,515 +82,365 +1206,634 +959,231 +74,324 +552,785 +597,740 +33,226 +765,871 +1195,407 +1036,653 +308,789 +880,337 +550,379 +1084,71 +750,308 +914,227 +922,861 +492,569 +1300,796 +570,301 +366,379 +252,794 +340,424 +1019,550 +443,407 +1298,22 +216,179 +713,740 +492,325 +736,284 +1004,589 +716,189 +293,205 +560,751 +396,59 +1102,415 +398,396 +750,695 +584,765 +1143,849 +398,834 +28,213 +291,446 +587,327 +582,376 +798,393 +126,826 +422,527 +274,345 +877,873 +684,661 +1118,36 +348,449 +1044,45 +654,476 +552,337 +994,462 +1119,532 +1233,675 +378,432 +433,378 +1198,4 +1285,425 +490,261 +870,726 +304,267 +537,147 +808,65 +388,333 +1052,810 +995,745 +599,367 +1041,154 +315,149 +750,59 +246,297 +385,826 +711,527 +599,527 +288,693 +1009,305 +1265,110 +298,661 +961,864 +929,868 +534,26 +952,386 +872,327 +517,648 +104,515 +940,91 +380,793 +792,661 +348,305 +1280,11 +195,525 +1272,649 +912,498 +808,348 +5,513 +994,742 +396,535 +326,866 +741,794 +1041,889 +1310,526 +440,726 +174,742 +217,644 +1048,47 +930,653 +105,154 +1044,849 +1113,126 +1288,739 +586,731 +1146,793 +621,205 +681,694 +1081,350 +1123,693 +1051,226 +263,126 +796,120 +25,3 +194,834 +443,39 +164,585 +316,796 +691,670 +1240,386 +962,53 +175,647 +1059,217 +1233,302 +856,857 +37,861 +13,679 +299,731 +139,439 +112,17 +1084,519 +544,884 +855,504 +323,563 +1026,91 +1135,373 +587,444 +10,348 +512,495 +435,805 +200,261 +63,350 +806,135 +109,84 +999,768 +1081,648 +208,511 +621,375 +1014,890 +726,129 +1159,605 +981,399 +495,107 +30,883 +136,233 +443,463 +1168,393 +22,739 +1233,436 +1111,371 +870,676 +846,661 +186,633 +927,591 +457,484 +52,429 +1047,126 +359,817 +1277,689 +432,149 +638,390 +296,60 +1126,726 +629,872 +897,661 +991,292 +0,28 +545,723 +462,374 +60,33 +1076,46 +1118,520 +708,808 +216,119 +1120,233 +27,208 +1052,269 +966,54 +895,128 +565,492 +115,151 +905,884 +878,149 +749,233 +820,773 +1011,731 +714,403 +765,810 +306,305 +164,793 +373,470 +566,378 +219,441 +1201,644 +304,344 +440,238 +288,379 +1205,154 +656,418 +692,709 +1014,442 +525,264 +867,463 +313,679 +272,213 +1027,296 +1310,409 +946,93 +1006,381 +818,569 +52,9 +55,329 +666,348 +222,821 +795,455 +264,705 +200,353 +1130,726 +975,602 +1303,180 +214,280 +1206,515 +385,887 +340,246 +1052,311 +760,379 +1118,632 +750,751 +1307,115 +723,567 +232,586 +684,856 +1193,147 +20,761 +547,815 +840,600 +975,154 +802,791 +1017,205 +902,73 +618,185 +1042,491 +617,861 +21,30 +1096,4 +410,149 +1,651 +65,320 +591,408 +139,455 +88,516 +626,486 +559,343 +99,262 +35,173 +1019,78 +502,65 +1044,625 +1277,350 +445,299 +765,852 +462,632 +92,491 +480,691 +1228,521 +987,234 +1151,212 +1310,37 +1230,745 +621,71 +634,506 +329,399 +376,229 +525,176 +944,515 +1096,238 +932,432 +535,336 +1148,437 +105,826 +848,262 +1208,885 +602,884 +872,665 +821,63 +246,738 +616,827 +629,694 +216,327 +987,563 +1009,753 +556,157 +671,368 +110,212 +1006,739 +229,648 +102,33 +199,22 +1017,736 +586,362 +535,222 +738,365 +37,481 +935,687 +626,661 +1289,30 +1225,794 +26,793 +325,875 +20,58 +338,462 +77,87 +1280,403 +422,639 +882,572 +723,450 +405,891 +802,103 +199,371 +129,36 +1054,60 +361,689 +470,600 +877,378 +175,521 +468,403 +233,866 +398,498 +311,432 +457,768 +914,611 +232,367 +997,231 +1079,463 +142,847 +960,157 +1208,89 +574,738 +671,526 +498,826 +331,824 +497,837 +830,203 +692,185 +291,190 +577,807 +956,885 +525,597 +331,155 +1195,151 +698,525 +619,672 +75,5 +1151,152 +996,424 +20,847 +432,373 +151,605 +815,742 +291,470 +535,672 +445,147 +1078,586 +1083,397 +251,217 +887,791 +1173,246 +385,5 +288,515 +850,211 +815,152 +550,67 +602,885 +350,102 +159,742 +672,390 +1086,9 +1240,129 +467,693 +710,681 +1071,640 +783,782 +887,463 +515,537 +199,523 +192,858 +380,653 +979,324 +504,135 +233,325 +1235,889 +756,546 +1277,879 +979,570 +15,238 +139,21 +271,666 +30,459 +306,893 +1283,283 +617,481 +708,86 +651,331 +134,486 +497,57 +994,152 +1064,821 +1026,474 +689,544 +1077,325 +77,437 +237,626 +58,155 +970,754 +498,404 +1280,883 +934,665 +741,346 +505,231 +853,768 +867,855 +587,567 +560,835 +1240,627 +716,861 +234,403 +766,212 +676,506 +922,493 +853,544 +924,43 +594,649 +1298,306 +582,518 +1019,246 +268,43 +1200,682 +684,408 +77,436 +788,534 +584,162 +768,502 +70,765 +1287,775 +1017,17 +914,835 +36,261 +373,424 +395,220 +67,108 +1168,847 +1210,375 +716,649 +422,364 +291,704 +266,849 +1079,659 +960,715 +1245,320 +105,243 +815,331 +27,644 +656,476 +1006,715 +388,861 +999,686 +457,320 +77,219 +808,546 +315,311 +922,561 +550,373 +226,322 +1044,109 +316,152 +979,682 +900,116 +1233,359 +1263,263 +586,532 +642,140 +961,416 +1230,149 +385,68 +714,491 +1102,143 +207,560 +731,889 +733,227 +649,889 +256,60 +758,805 +232,82 +1181,544 +683,698 +581,716 +301,753 +970,470 +331,570 +1298,536 +853,320 +733,807 +1078,493 +710,233 +995,65 +194,211 +159,234 +1233,437 +33,544 +584,129 +398,95 +152,315 +1096,686 +35,246 +12,754 +726,732 +1066,745 +1006,157 +208,479 +361,544 +333,381 +569,794 +1010,437 +323,376 +396,583 +577,9 +189,304 +798,708 +323,518 +289,481 +604,386 +657,203 +363,670 +719,408 +863,840 +139,873 +619,344 +186,157 +634,445 +679,663 +723,892 +428,124 +549,826 +914,311 +790,74 +802,455 +1077,625 +835,497 +562,400 +689,877 +403,824 +107,453 +422,586 +284,539 +1110,149 +438,851 +110,436 +930,549 +775,616 +438,327 +1096,656 +587,892 +199,102 +498,516 +1198,886 +672,392 +528,602 +602,465 +922,33 +740,301 +525,108 +244,745 +162,121 +1081,397 +843,693 +1257,420 +1079,431 +425,246 +1267,72 +127,112 +855,550 +100,519 +1171,357 +542,502 +653,691 +495,331 +380,883 +256,765 +85,100 +949,241 +127,480 +880,557 +984,866 +1079,799 +542,56 +413,233 +490,773 +741,233 +388,33 +214,676 +457,544 +93,168 +600,213 +1171,439 +1159,23 +1036,241 +925,378 +904,593 +930,877 +913,278 +955,163 +1014,452 +750,199 +1083,486 +33,653 +43,72 +1110,129 +1002,789 +1073,492 +112,838 +802,887 +995,311 +661,889 +1242,45 +353,246 +1171,455 +284,420 +716,113 +971,297 +129,858 +239,766 +552,271 +440,166 +539,743 +1151,563 +1285,753 +1135,535 +562,681 +751,444 +408,73 +1236,324 +229,246 +229,397 +1004,305 +870,238 +1135,521 +888,527 +1171,775 +683,656 +775,222 +87,128 +522,326 +1111,446 +95,459 +805,231 +1038,728 +1130,231 +7,292 +863,782 +689,879 +249,3 +718,637 +398,715 +326,28 +768,56 +315,135 +1103,560 +889,625 +1071,94 +422,756 +1019,344 +520,74 +313,231 +726,162 +422,255 +470,726 +1243,567 +87,766 +956,437 +195,369 +402,714 +386,515 +1282,681 +398,50 +878,373 +1094,889 +626,233 +186,289 +470,168 +651,772 +396,269 +967,49 +1089,292 +348,589 +627,698 +842,605 +850,584 +542,840 +162,437 +1096,280 +1198,877 +167,45 +33,205 +736,738 +761,68 +1285,3 +30,403 +351,231 +764,857 +63,856 +1047,768 +1006,627 +597,154 +413,661 +569,346 +1277,544 +1170,185 +92,43 +112,504 +897,681 +1061,3 +25,872 +842,403 +681,443 +503,605 +831,23 +329,875 +730,154 +430,557 +464,474 +1243,786 +1168,168 +12,140 +542,54 +258,535 +813,837 +291,0 +1223,128 +1004,841 +842,491 +912,577 +251,592 +1295,656 +139,537 +262,847 +237,402 +1198,17 +1310,28 +64,47 +355,868 +1116,211 +214,208 +806,583 +149,231 +276,880 +199,446 +378,166 +951,817 +927,228 +1113,821 +380,345 +164,757 +200,745 +200,765 +293,877 +268,403 +915,327 +1102,751 +214,686 +1084,572 +755,49 +955,868 +1036,11 +186,737 +1208,861 +649,381 +1088,821 +795,469 +676,58 +1191,616 +710,115 +552,33 +684,38 +562,491 +1054,520 +525,630 +870,218 +237,268 +353,694 +1041,5 +1170,774 +570,593 +831,871 +187,693 +982,857 +497,889 +885,246 +755,632 +246,821 +666,546 +67,450 +1110,261 +987,518 +1078,703 +545,852 +293,736 +234,494 +1115,369 +1274,261 +402,626 +1247,150 +1230,828 +508,439 +405,884 +1274,633 +626,408 +306,1 +1233,458 +415,766 +520,820 +152,65 +1026,420 +1017,158 +239,254 +268,491 +1110,745 +159,107 +761,826 +413,681 +820,325 +438,5 +1171,119 +1051,327 +618,751 +579,217 +1151,331 +1012,661 +77,359 +1102,511 +1130,616 +1228,529 +350,715 + +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 \ No newline at end of file