From: Brett Parker Date: Tue, 14 Dec 2021 03:19:06 +0000 (+0000) Subject: Day 13 (start) X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2021.git/commitdiff_plain/16c6bf02b28e2d22ce893acdd4de2e4094316e8d?ds=sidebyside Day 13 (start) --- diff --git a/day13/example.txt b/day13/example.txt new file mode 100644 index 0000000..282114c --- /dev/null +++ b/day13/example.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 diff --git a/day13/fold.sh b/day13/fold.sh new file mode 100755 index 0000000..83e1585 --- /dev/null +++ b/day13/fold.sh @@ -0,0 +1,157 @@ +#!/bin/bash + +set -u + +declare -a points=() +declare -a folds=() + +filename="${1:-example.txt}" +exec 3<"$filename" + +max_x=0 +max_y=0 + +while read -u 3 line; do + if [ "$line" == "" ]; then + break + fi + x=${line%,*} + y=${line#*,} + if [ $x -gt $max_x ]; then + max_x=$x + fi + if [ $y -gt $max_y ]; then + max_y=$y + fi + points+=($line) +done + +((max_x+=1)) +((max_y+=1)) + +while read -u 3 line; do + folds+=(${line#fold along }) +done + +display_map() { + for (( y=0; y<$max_y; y++ )); do + for (( x=0; x<$max_x; x++ )); do + offset=$(((y*$max_x) + $x)) + case "${map[$offset]}" in + 0) + echo -n "." + ;; + 1) + echo -n "#" + ;; + esac + done + echo + done +} + +declare -a map + +# now build a map +for (( a=0; a<$(($max_x * $max_y)); a++ )); do + map[$a]=0 +done + +for point in "${points[@]}"; do + x=${point%,*} + y=${point#*,} + offset=$((($y*$max_x)+$x)) + map[$offset]=1 +done + +fold() { + local line="$1" + local axis=${line%=*} + local point=${line#*=} + local dot="." + + declare -a new_map=() + + case $axis in + x) + local new_max_x=$(($max_x - $point - 1)) + if [ $point -gt $new_max_x ]; then + new_max_x=$point + fi + local adj=$((max_x % 2)) + for (( y=0; y<$max_y; y++ )); do + for (( x=0; x<$new_max_x; x++ )); do + x_1=$(($point - $new_max_x + $x)) + x_2=$(($max_x - ($new_max_x - $point) - $x - $adj)) + offset_1=$((($y * $max_x) + $x_1)) + offset_2=$((($y * $max_x) + $x_2)) + if [ $x_1 -lt 0 ]; then + offset_1=$offset_2 + fi + if [ $x_2 -ge $max_x ]; then + offset_2=$offset_1 + fi + new_map+=("$dot") + new_offset=$((($y * $new_max_x) + $x)) + new_map[$new_offset]=$((${map[$offset_1]} | ${map[$offset_2]})) + done + done + map=("${new_map[@]}") + max_x=$new_max_x + ;; + y) + local new_max_y=$(($max_y - $point - 1)) + if [ $point -gt $new_max_y ]; then + new_max_y=$point + fi + local adj=$((max_y % 2)) + for (( y=0; y<$new_max_y; y++ )); do + # do the y offsets here + y_1=$(($point - $new_max_y + $y)) + y_2=$(($max_y - ($new_max_y - $point) - $y - $adj)) + offset_1=$(($y_1 * $max_x)) + offset_2=$(($y_2 * $max_x)) + if [ $y_1 -lt 0 ]; then + offset_1=$offset_2 + fi + if [ $y_2 -ge $max_y ]; then + offset_2=$offset_1 + fi + for (( x=0; x<$max_x; x++ )); do + off_1=$(($offset_1+$x)) + off_2=$(($offset_2+$x)) + new_map+=("$dot") + new_offset=$((($y * $max_x) + $x)) + new_map[$new_offset]=$((${map[$off_1]} | ${map[$off_2]})) + done + done + map=("${new_map[@]}") + max_y=$new_max_y + ;; + esac +} + +get_points_count() { + local count=0 + for point in "${map[@]}"; do + ((count+=$point)) + done + + echo $count +} + +#display_map + +# do first fold +fold "${folds[0]}" +echo +#display_map +echo +echo "There are $(get_points_count) dots visible after first fold" +for (( f=1; f<${#folds[@]}; f++ )); do + echo "Doing fold $((f+1)) of ${#folds[@]}" + fold "${folds[$f]}" +done +echo +echo "After all folds:" +display_map diff --git a/day13/input.txt b/day13/input.txt new file mode 100644 index 0000000..4007274 --- /dev/null +++ b/day13/input.txt @@ -0,0 +1,892 @@ +604,512 +8,675 +621,465 +172,851 +1205,212 +443,366 +817,728 +795,155 +788,334 +89,257 +693,56 +1091,459 +363,145 +1029,268 +1230,89 +692,418 +485,584 +299,138 +1113,630 +817,166 +142,123 +1238,226 +1295,74 +689,877 +1133,880 +37,504 +925,274 +1178,169 +493,728 +949,689 +708,810 +698,697 +187,662 +348,619 +537,794 +161,511 +773,324 +177,686 +282,695 +756,778 +440,856 +1093,530 +1039,376 +313,350 +1184,807 +78,383 +701,514 +303,802 +1222,707 +363,141 +1163,631 +575,854 +452,305 +900,726 +272,786 +1061,771 +167,26 +249,771 +403,248 +1257,82 +557,380 +1149,847 +402,103 +694,254 +832,390 +1285,467 +154,695 +689,316 +894,441 +273,586 +382,786 +408,469 +385,521 +443,814 +765,810 +435,459 +433,802 +788,281 +308,374 +746,759 +411,406 +392,553 +882,648 +679,92 +947,476 +537,793 +771,617 +858,645 +343,372 +383,690 +416,707 +477,583 +1202,264 +396,679 +1235,646 +353,431 +1211,75 +54,238 +992,675 +341,574 +1059,807 +27,138 +11,791 +8,786 +632,588 +756,38 +741,235 +97,319 +281,290 +643,103 +636,183 +189,157 +870,38 +426,761 +1202,854 +828,190 +460,564 +1304,665 +18,183 +525,712 +564,114 +1087,457 +721,290 +1064,693 +577,859 +20,99 +393,36 +1089,802 +1084,200 +303,214 +1,399 +68,439 +527,710 +177,208 +689,416 +351,354 +515,155 +117,705 +977,561 +177,462 +90,716 +877,92 +318,227 +631,792 +679,568 +418,262 +974,312 +395,794 +537,522 +756,784 +810,100 +1039,829 +671,871 +435,539 +564,850 +600,826 +994,274 +765,234 +771,737 +1061,123 +771,119 +771,605 +177,120 +130,402 +765,754 +714,115 +903,406 +609,514 +557,575 +617,838 +1240,620 +940,645 +928,70 +375,437 +1081,519 +894,187 +820,420 +648,229 +500,213 +564,626 +1195,169 +616,640 +607,462 +820,644 +691,74 +1193,548 +967,372 +45,185 +1043,213 +662,229 +728,668 +964,520 +1211,523 +850,184 +1287,715 +924,700 +843,662 +244,89 +1022,253 +758,568 +169,158 +23,49 +797,863 +426,329 +686,387 +817,831 +117,548 +1309,47 +996,747 +850,782 +137,796 +867,80 +1059,695 +100,486 +731,199 +1109,135 +306,296 +595,121 +236,632 +771,289 +18,711 +1066,168 +1156,695 +407,401 +959,456 +730,117 +306,828 +1004,63 +90,402 +83,488 +1289,714 +23,802 +267,681 +631,179 +992,358 +820,386 +646,716 +1079,682 +1203,646 +1133,871 +952,281 +711,1 +177,774 +679,886 +15,820 +318,667 +1178,729 +475,484 +1066,217 +102,344 +402,859 +679,267 +1285,543 +137,768 +1287,845 +383,513 +1,847 +534,408 +358,723 +305,292 +997,234 +1245,858 +907,471 +1119,140 +874,812 +341,225 +55,893 +1309,847 +855,152 +1143,868 +1126,66 +277,24 +1001,259 +1056,868 +694,813 +935,742 +247,849 +74,565 +744,483 +396,7 +407,717 +1048,553 +525,182 +316,274 +867,432 +1236,777 +546,712 +1046,520 +812,217 +1231,168 +343,350 +13,772 +303,92 +517,332 +676,175 +296,169 +843,403 +567,322 +873,140 +246,425 +639,871 +689,116 +460,819 +587,87 +107,406 +572,756 +403,871 +115,92 +1133,774 +1017,465 +437,140 +907,23 +1220,492 +177,14 +1059,848 +1058,791 +1125,191 +596,563 +385,274 +843,120 +632,306 +1299,679 +952,723 +137,870 +1031,152 +783,184 +1091,248 +482,444 +132,784 +1228,280 +1071,238 +974,536 +1019,866 +805,96 +910,359 +1255,289 +701,185 +1153,512 +301,432 +1236,663 +261,156 +1004,598 +734,626 +1,280 +161,502 +786,380 +184,828 +1041,274 +1236,329 +254,754 +547,400 +339,791 +115,14 +995,103 +1303,749 +42,697 +743,535 +1099,316 +261,431 +185,191 +402,707 +528,306 +681,880 +679,438 +832,504 +884,565 +1113,354 +744,341 +566,859 +559,794 +907,572 +554,784 +488,532 +935,152 +65,858 +856,87 +505,798 +710,602 +694,640 +1255,513 +1037,308 +559,100 +353,15 +600,656 +744,553 +1285,796 +850,778 +870,158 +838,296 +828,704 +928,516 +202,697 +472,598 +710,656 +279,876 +421,439 +293,192 +567,238 +1,737 +1173,781 +761,130 +1123,344 +1120,544 +833,798 +398,620 +293,689 +631,438 +676,719 +129,890 +513,415 +823,26 +1205,606 +604,382 +1178,281 +694,81 +467,232 +997,350 +1248,206 +467,344 +1014,59 +505,759 +567,359 +1183,700 +370,645 +1236,117 +1223,656 +822,532 +554,38 +703,686 +1009,124 +817,511 +78,511 +545,84 +634,245 +241,659 +1069,659 +214,38 +370,598 +1138,450 +1108,697 +1037,138 +127,700 +554,110 +579,606 +416,187 +797,479 +343,522 +956,798 +1067,854 +1141,158 +900,805 +791,82 +259,382 +47,73 +218,40 +194,75 +1121,157 +221,64 +679,845 +1011,138 +261,599 +1287,49 +619,74 +711,449 +807,455 +12,0 +572,614 +1242,439 +386,700 +955,586 +1086,438 +75,870 +189,289 +1235,855 +576,511 +691,121 +1007,802 +1017,702 +1268,197 +1033,24 +490,698 +1181,788 +952,837 +200,588 +354,544 +997,561 +336,43 +211,513 +457,92 +1004,296 +527,742 +865,570 +756,106 +748,722 +1203,488 +1243,38 +907,871 +1183,262 +403,471 +728,444 +493,495 +691,522 +728,450 +520,672 +254,868 +751,794 +788,371 +493,502 +227,371 +928,108 +1160,838 +782,535 +957,124 +50,606 +967,522 +336,704 +400,535 +537,101 +984,172 +1248,598 +914,679 +190,544 +710,82 +443,65 +1022,254 +301,837 +221,610 +833,23 +546,182 +788,262 +960,754 +423,660 +150,390 +443,829 +269,821 +201,759 +724,455 +102,120 +1011,756 +489,728 +947,418 +221,830 +554,486 +8,451 +1064,245 +100,110 +996,595 +1039,669 +1047,259 +487,868 +865,324 +293,205 +679,456 +23,214 +763,435 +306,63 +59,121 +127,38 +764,182 +313,234 +485,830 +853,452 +566,341 +172,127 +1178,277 +1208,438 +343,126 +957,15 +366,193 +547,696 +1228,614 +730,483 +293,554 +525,802 +1297,772 +1298,0 +246,21 +545,810 +894,453 +1298,371 +219,248 +1220,402 +927,445 +671,522 +853,92 +843,491 +612,25 +656,620 +196,156 +318,536 +130,514 +8,108 +1180,402 +440,606 +1049,156 +7,593 +1256,292 +735,854 +105,10 +231,682 +607,208 +884,313 +1220,403 +1063,401 +706,382 +631,326 +482,767 +539,617 +172,319 +952,169 +703,462 +469,191 +219,808 +870,856 +790,672 +177,583 +90,178 +627,756 +177,880 +1309,280 +1238,318 +74,329 +90,492 +621,877 +127,856 +773,122 +430,886 +1156,491 +1116,184 +803,183 +971,791 +1101,820 +908,859 +631,715 +825,830 +336,268 +1265,521 +728,278 +44,642 +935,16 +902,693 +825,310 +689,702 +336,88 +982,274 +364,418 +485,64 +172,892 +621,712 +261,208 +382,516 +867,428 +1287,802 +833,871 +67,807 +321,472 +1289,703 +355,586 +760,331 +748,274 +771,80 +821,728 +924,252 +75,855 +657,157 +1141,360 +959,386 +797,415 +271,376 +1049,208 +343,768 +1280,619 +385,373 +55,737 +92,722 +80,469 +1138,2 +1039,630 +172,767 +671,74 +806,523 +686,448 +346,520 +1089,64 +853,540 +251,198 +674,183 +582,26 +1109,56 +88,707 +354,798 +545,754 +865,74 +1178,784 +681,351 +607,686 +228,313 +1061,571 +624,387 +753,380 +25,427 +1183,632 +1138,127 +263,635 +1009,431 +810,213 +135,812 +35,474 +45,290 +947,28 +221,150 +1002,744 +736,469 +23,845 +679,326 +1257,812 +1058,859 +969,359 +72,226 +457,353 +786,514 +75,407 +1061,323 +460,75 +616,254 +1133,462 +201,56 +835,186 +132,617 +746,856 +714,779 +833,583 +865,77 +710,238 +1034,830 +537,324 +309,259 +373,268 +30,617 +1193,820 +25,602 +1146,386 +59,773 +1009,462 +1274,418 +492,834 +609,521 +460,782 +271,513 +718,296 +83,264 +1287,92 +129,182 +79,168 +862,645 +428,694 +478,728 +771,277 +375,152 +348,828 +428,246 +753,319 +469,703 +313,561 +903,717 +1238,588 +1260,382 +1042,252 +358,169 +539,119 +928,826 +353,770 +318,675 +358,281 +565,637 +161,390 +597,184 +587,38 +817,495 +975,301 +1039,14 +1019,476 +47,885 +1113,4 +219,459 +244,168 +18,631 +785,802 +308,520 +247,269 +992,667 +907,696 +920,330 +440,494 +547,435 +1131,708 +631,568 +271,883 +77,859 +1123,662 +288,254 +306,619 +607,14 +678,588 +191,306 +664,178 +80,362 +132,611 +1180,514 +1175,812 +20,795 +1099,80 +572,726 +1064,201 +850,564 +282,247 +1096,38 +50,512 +547,98 +1208,344 +226,591 +1289,479 +92,274 +1173,796 +1110,306 +358,725 +1232,383 +18,318 +619,522 +654,620 +664,268 +967,544 +621,778 +132,731 +803,260 +1028,112 +788,632 +82,166 +1210,486 +281,77 +75,646 +579,199 +97,823 +653,399 +580,665 +974,43 +547,494 +674,711 +1230,425 +313,544 +477,311 +753,823 +117,189 +765,140 +27,318 +599,1 +600,292 +884,761 +1138,892 +903,269 +600,82 +999,372 +621,30 +571,264 +877,130 +677,31 +89,637 +1114,156 +12,448 +108,630 +375,742 +736,89 +873,364 +88,35 +566,61 +862,249 +935,437 +788,523 +783,866 + +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