From 942c62ae5ca1e1c8f59f7c3ea6dd3f1e5e2b17e2 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Mon, 13 Dec 2021 21:57:04 +0000 Subject: [PATCH] Day 13 --- day13/Makefile | 10 + day13/example.txt | 21 + day13/realinput.txt | 936 +++++++++++++++++++++++++++++++++++++++++ day13/solution-part1.c | 95 +++++ day13/solution-part2.c | 102 +++++ 5 files changed, 1164 insertions(+) create mode 100644 day13/Makefile create mode 100644 day13/example.txt create mode 100644 day13/realinput.txt create mode 100644 day13/solution-part1.c create mode 100644 day13/solution-part2.c diff --git a/day13/Makefile b/day13/Makefile new file mode 100644 index 0000000..da2e708 --- /dev/null +++ b/day13/Makefile @@ -0,0 +1,10 @@ +solution-part%: solution-part%.c + gcc $< -o $@ + +test-%: solution-% example.txt + $< example.txt + +run-%: solution-% realinput.txt + $< realinput.txt + +.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2) 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/realinput.txt b/day13/realinput.txt new file mode 100644 index 0000000..21702d0 --- /dev/null +++ b/day13/realinput.txt @@ -0,0 +1,936 @@ +1148,688 +1020,159 +857,707 +1176,415 +388,275 +50,849 +544,520 +1,418 +1119,280 +217,26 +358,110 +1302,684 +910,791 +45,287 +544,806 +423,859 +529,866 +1150,686 +977,668 +666,245 +380,603 +892,343 +437,483 +1109,411 +296,534 +763,676 +711,218 +139,774 +923,397 +1202,341 +664,12 +416,89 +522,54 +524,401 +541,796 +1153,523 +1101,427 +977,308 +102,856 +769,546 +966,523 +1218,820 +833,682 +872,806 +457,542 +333,586 +238,537 +528,103 +169,840 +321,44 +102,408 +825,346 +1220,791 +1223,584 +907,597 +785,766 +147,654 +201,707 +775,674 +746,651 +221,354 +213,460 +843,105 +59,110 +224,773 +654,368 +666,21 +1134,736 +475,121 +445,799 +1141,411 +835,688 +810,12 +810,882 +1064,51 +1240,805 +266,723 +125,812 +1048,299 +213,68 +129,738 +100,256 +666,551 +1004,632 +1250,505 +119,674 +565,264 +937,220 +387,497 +1235,259 +8,658 +902,294 +159,884 +222,159 +184,645 +700,646 +1251,110 +53,469 +1228,282 +157,779 +862,759 +694,31 +10,876 +652,110 +1104,505 +554,582 +574,742 +985,642 +410,830 +873,420 +623,42 +70,89 +922,395 +627,446 +724,848 +610,534 +1042,283 +852,731 +370,193 +109,420 +1275,239 +582,546 +88,537 +1133,682 +584,847 +835,654 +299,649 +735,82 +467,547 +408,51 +1278,233 +758,325 +1044,171 +233,364 +490,16 +326,187 +455,114 +1231,504 +1300,876 +403,830 +1185,418 +731,187 +256,354 +1200,725 +1101,338 +781,399 +1207,147 +1153,502 +335,752 +716,389 +811,747 +619,311 +865,95 +221,443 +567,310 +503,112 +646,658 +296,617 +314,40 +813,483 +1223,252 +813,502 +566,724 +445,95 +832,437 +191,614 +268,611 +162,78 +303,5 +865,114 +373,108 +688,9 +692,511 +403,696 +1071,724 +786,401 +544,164 +150,457 +75,198 +1006,607 +441,68 +300,873 +160,686 +1059,646 +53,47 +850,847 +458,182 +1265,259 +869,826 +571,241 +408,395 +1007,889 +515,95 +458,163 +154,761 +642,774 +571,653 +199,266 +364,719 +109,474 +700,360 +160,14 +671,345 +448,583 +711,666 +296,277 +984,187 +1096,824 +381,469 +649,624 +1310,635 +1206,607 +392,35 +381,698 +520,479 +482,145 +498,444 +239,170 +601,21 +522,840 +1067,770 +415,259 +827,595 +52,725 +251,597 +0,10 +150,736 +194,840 +504,630 +586,275 +140,387 +716,284 +619,127 +1066,178 +827,299 +858,634 +652,336 +738,612 +1016,233 +431,595 +760,656 +909,803 +960,679 +171,376 +45,194 +687,852 +923,385 +1116,840 +112,696 +1193,575 +119,786 +290,159 +401,803 +515,662 +708,110 +1054,354 +1019,351 +1103,408 +1250,610 +979,430 +213,434 +574,395 +1153,859 +654,635 +671,526 +912,873 +664,236 +1126,697 +157,819 +1129,271 +460,46 +494,196 +604,849 +1131,206 +975,80 +1111,655 +798,473 +716,262 +493,644 +378,607 +1087,285 +490,526 +914,400 +485,444 +938,584 +735,812 +639,549 +408,724 +244,178 +934,166 +739,241 +325,700 +139,120 +1067,124 +950,681 +356,863 +639,345 +618,735 +1242,270 +848,302 +222,107 +663,142 +1302,658 +441,98 +850,495 +759,733 +386,516 +488,250 +892,691 +869,684 +1136,886 +1067,572 +671,318 +890,560 +82,53 +692,735 +1061,234 +1097,460 +922,275 +1006,679 +631,341 +100,424 +1032,166 +959,553 +1046,22 +422,64 +537,266 +340,830 +945,562 +190,357 +1206,516 +541,546 +656,78 +756,190 +146,411 +723,15 +1148,816 +602,744 +602,560 +1171,351 +262,859 +761,578 +410,206 +923,110 +223,285 +554,246 +420,425 +251,248 +1287,229 +90,651 +691,311 +879,161 +445,418 +32,9 +97,681 +1032,280 +782,551 +1228,53 +479,334 +869,98 +1221,264 +959,889 +57,120 +795,120 +435,452 +750,830 +529,674 +666,721 +996,152 +596,740 +1072,203 +1141,252 +937,200 +825,444 +192,262 +117,266 +969,10 +45,700 +202,634 +909,539 +930,291 +852,289 +1113,459 +447,668 +1174,145 +766,806 +234,493 +497,168 +725,479 +243,572 +36,308 +231,35 +653,700 +1155,742 +967,352 +90,567 +1210,256 +26,133 +164,430 +852,182 +550,835 +575,278 +1096,518 +408,618 +1126,645 +291,203 +843,547 +393,589 +251,646 +403,597 +956,873 +504,158 +438,730 +1042,611 +36,383 +622,233 +954,255 +686,336 +1072,89 +882,312 +1012,184 +1056,231 +1240,89 +639,368 +1185,226 +37,530 +1240,133 +1033,619 +1116,847 +624,336 +1227,770 +298,38 +418,243 +60,877 +817,112 +554,638 +1148,766 +495,94 +363,239 +171,518 +167,884 +192,632 +1227,124 +119,332 +1279,42 +1275,655 +387,385 +843,211 +145,271 +416,203 +1042,560 +560,275 +378,597 +199,239 +1242,723 +1200,54 +684,255 +649,270 +709,425 +234,137 +52,560 +36,159 +1298,264 +1201,420 +1143,570 +769,796 +773,19 +1181,128 +474,712 +535,184 +713,120 +1273,635 +267,385 +768,849 +848,592 +94,718 +1101,767 +1054,21 +156,819 +827,518 +1258,560 +243,770 +1043,497 +1278,9 +234,849 +962,262 +602,150 +720,35 +1054,522 +758,38 +1287,341 +1207,733 +1114,849 +254,572 +1114,493 +836,712 +951,751 +410,64 +1282,255 +818,414 +776,759 +813,392 +1198,824 +150,158 +850,757 +489,334 +1272,415 +23,344 +1043,833 +110,840 +104,656 +750,512 +766,730 +1116,137 +594,610 +358,534 +474,182 +408,276 +282,870 +172,749 +619,799 +418,19 +653,418 +795,792 +1034,119 +1191,786 +1255,228 +562,220 +410,688 +139,214 +806,630 +683,446 +57,827 +508,147 +1193,266 +766,520 +147,78 +186,835 +441,826 +756,638 +80,499 +1141,483 +1089,443 +932,597 +1044,569 +1185,28 +937,108 +915,687 +165,334 +954,863 +468,835 +246,499 +671,121 +89,264 +745,30 +542,824 +311,352 +395,207 +878,159 +981,252 +104,387 +623,303 +333,859 +972,469 +1225,155 +364,287 +1216,830 +668,264 +560,718 +902,276 +766,390 +528,551 +445,875 +606,54 +1242,171 +1305,668 +268,507 +1118,144 +1076,493 +112,198 +773,394 +403,259 +1203,228 +1148,78 +276,735 +22,378 +1029,694 +1124,835 +162,206 +169,483 +467,120 +900,830 +668,64 +49,367 +1037,308 +760,59 +736,742 +820,480 +411,770 +663,752 +500,425 +500,469 +104,159 +756,582 +103,147 +68,270 +873,698 +1146,318 +1310,240 +105,764 +642,64 +102,38 +1148,592 +890,21 +497,194 +145,308 +266,171 +873,411 +258,714 +870,719 +500,882 +1253,120 +261,81 +744,651 +1258,520 +542,70 +1298,885 +1115,518 +813,75 +852,163 +587,15 +460,399 +552,653 +89,712 +806,158 +88,651 +381,420 +726,47 +547,676 +925,67 +1205,92 +716,610 +281,786 +537,319 +1268,247 +1208,856 +497,726 +460,847 +181,271 +981,194 +1171,571 +592,200 +154,133 +594,505 +687,303 +1200,358 +1255,666 +159,772 +535,108 +453,476 +1170,112 +542,268 +281,694 +705,815 +117,866 +746,203 +1253,67 +1191,75 +119,75 +328,411 +850,46 +1088,607 +44,646 +682,761 +1231,726 +1200,838 +445,114 +176,158 +335,682 +129,290 +1265,194 +639,318 +1059,597 +1116,54 +45,259 +701,252 +644,873 +1037,831 +832,736 +738,393 +131,70 +1205,316 +239,259 +828,761 +529,259 +1086,241 +1179,824 +1011,245 +1295,325 +791,425 +282,24 +792,313 +879,285 +441,684 +1150,219 +140,283 +49,115 +256,873 +504,264 +668,824 +552,508 +858,260 +1042,287 +1146,32 +594,465 +431,285 +1266,646 +278,166 +1280,336 +447,226 +704,840 +438,806 +1298,233 +687,591 +1114,737 +294,885 +1179,70 +776,135 +459,518 +1144,219 +666,873 +1014,617 +60,389 +602,179 +1160,437 +1153,115 +915,207 +100,190 +1148,480 +920,263 +199,655 +10,245 +485,848 +1071,635 +290,271 +1054,746 +387,397 +177,682 +586,270 +658,336 +1163,654 +282,758 +1094,515 +94,830 +623,852 +38,522 +549,92 +564,327 +744,243 +1200,851 +495,800 +1240,668 +775,786 +1298,70 +1200,374 +835,121 +186,465 +415,892 +851,518 +564,203 +1056,663 +709,873 +837,623 +246,395 +756,424 +398,204 +587,187 +1266,310 +628,133 +924,334 +455,655 +398,873 +320,222 +1170,334 +83,124 +739,124 +959,540 +1220,679 +390,631 +544,88 +290,607 +102,632 +820,576 +278,280 +1310,187 +80,395 +105,443 +70,668 +1044,395 +251,382 +68,848 +267,61 +1111,332 +127,556 +1258,179 +1044,505 +738,282 +82,612 +934,614 +1054,204 +72,348 +254,663 +1043,397 +85,291 +877,155 +417,91 +460,137 +565,579 +564,567 +1041,550 +565,30 +1000,859 +1056,791 +688,233 +595,336 +1012,479 +140,334 +256,148 +1220,651 +542,178 +209,767 +534,759 +1287,547 +1213,681 +1076,849 +508,747 +273,831 +276,149 +256,298 +194,54 +872,88 +441,656 +214,175 +266,569 +865,875 +12,630 +1000,523 +1242,494 +873,196 +157,35 +420,469 +445,780 +551,733 +760,835 +1118,750 +551,161 +709,831 +448,311 +1275,319 +1057,192 +136,749 +628,761 +365,332 +1153,75 +846,117 +546,133 +1288,378 +900,512 +223,609 +350,159 +1287,344 +1103,668 +125,418 +440,719 +832,826 +36,586 +776,57 +1171,214 +480,456 +668,120 +646,12 +947,95 +378,159 +31,852 +1170,387 +535,786 +572,612 +619,583 +1086,101 +432,159 +560,619 +2,320 +1143,10 +53,847 +256,220 +894,805 +152,186 +136,817 +32,233 +1101,786 +579,288 +117,351 +1043,845 +731,707 +666,18 +1208,408 +32,885 +268,283 +929,698 +326,570 +590,35 +104,607 +654,436 +139,571 +619,655 +403,417 +1076,401 +1220,243 +990,672 +1308,574 +1109,707 +1032,614 +499,152 + +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/day13/solution-part1.c b/day13/solution-part1.c new file mode 100644 index 0000000..27a1d04 --- /dev/null +++ b/day13/solution-part1.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include + +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_RESET "\x1b[0m" + +#define MAX(a, b) ((a) < (b) ? (b) : (a)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +void printPaper( unsigned int width, unsigned int height, bool paper[width][height] ){ + for ( unsigned int y = 0; y < height; y++ ){ + printf("Line %i: ", y); + for ( unsigned int x = 0; x < width; x++ ){ + if ( paper[x][y] == true ) printf("#"); + else printf("."); + } + printf("\n"); + } +} + + +void foldPaperX( unsigned int width, unsigned int height, unsigned int col, bool paper[width][height], bool newPaper[col][height]){ + for ( unsigned int x = 1; x < width - col; x++ ) + for ( unsigned int y=0; y < height; y++ ) + newPaper[col - x][y] = paper[col - x][y] || paper[col + x][y]; +} + +void foldPaperY( unsigned int width, unsigned int height, unsigned int row, bool paper[width][height], bool newPaper[width][row]){ + for ( unsigned int y=1; y < height - row; y++ ) + for ( unsigned int x = 0; x < width; x++ ) + newPaper[x][row - y] = ( paper[x][row - y] || paper[x][row + y] ); +} + +unsigned int countDots( unsigned int width, unsigned int height, bool paper[width][height] ){ + unsigned int count=0; + for ( unsigned int y = 0; y < height; y++ ) + for ( unsigned int x = 0; x < width; x++ ) + if ( paper[x][y] == true ) count++; + return count; +} + +int main( int argc, char *argv[] ){ + if( argc == 2 ) { + + + FILE *fp=fopen(argv[1], "r"); + + unsigned int x=0,y=0,maxx=0,maxy=0; + while ( fscanf( fp, "%i,%i\n",&x,&y )){ + maxx = MAX(maxx,x); + maxy = MAX(maxy,y); + } + + bool paper[maxx+1][maxy+1]; + memset(paper,0,sizeof(bool) * (maxx+1) * (maxy+1) ); + + rewind(fp); + while ( fscanf( fp, "%i,%i\n",&x,&y )){ + paper[x][y] = true; + } + + printPaper( maxx+1, maxy+1, paper ); + printf("\n"); + + char direction = '\0'; + unsigned int position = 0, dots = 0; + fscanf(fp, "fold along %c=%i\n", &direction, &position); + if ( direction == 'x' ){ + bool newPaper[position][maxy+1]; + foldPaperX(maxx+1,maxy+1,position, paper, newPaper); + printPaper( position, maxy+1, newPaper ); + dots=countDots( position, maxy+1, newPaper ); + } else { + bool newPaper[maxx+1][position]; + foldPaperY(maxx+1,maxy+1,position, paper, newPaper); + printPaper( maxx+1, position, newPaper ); + dots=countDots( maxx+1, position, newPaper ); + } + + printf("\nThere are %i dots\n", dots); + + + fclose(fp); + + + + return 0; + } else { + printf("You need to provide a file\n"); + return 1; + } +} diff --git a/day13/solution-part2.c b/day13/solution-part2.c new file mode 100644 index 0000000..acc001f --- /dev/null +++ b/day13/solution-part2.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include + +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_RESET "\x1b[0m" + +#define MAX(a, b) ((a) < (b) ? (b) : (a)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +void printPaper( unsigned int width, unsigned int height, bool **paper ){ + for ( unsigned int y = 0; y < height; y++ ){ + printf("Line %i: ", y); + for ( unsigned int x = 0; x < width; x++ ){ + if ( paper[x][y] == true ) printf("#"); + else printf("."); + } + printf("\n"); + } +} + + +bool** foldPaperX( unsigned int width, unsigned int height, unsigned int col, bool **paper){ + bool **newPaper=(bool**)malloc( col * sizeof(bool *)); + for(unsigned int x=0; x < col;x++) { + newPaper[x]=(bool*)malloc( (height) *sizeof(bool)); + memset(newPaper[x], 0, sizeof(bool) * ( height )); + } + for ( unsigned int x = 1; x < width - col; x++ ) + for ( unsigned int y=0; y < height; y++ ) + newPaper[col - x][y] = paper[col - x][y] || paper[col + x][y]; + return newPaper; +} + +bool** foldPaperY( unsigned int width, unsigned int height, unsigned int row, bool **paper){ + bool **newPaper=(bool**)malloc( (width) * sizeof(bool *)); + for(unsigned int x=0; x < width; x++) { + newPaper[x]=(bool*)malloc( (row) *sizeof(bool)); + memset(newPaper[x], 0, sizeof(bool) * ( row )); + } + for ( unsigned int y=1; y < height - row; y++ ) + for ( unsigned int x = 0; x < width; x++ ) + newPaper[x][row - y] = ( paper[x][row - y] || paper[x][row + y] ); + return newPaper; +} + +int main( int argc, char *argv[] ){ + if( argc == 2 ) { + + FILE *fp=fopen(argv[1], "r"); + + unsigned int x=0,y=0,maxx=0,maxy=0; + while ( fscanf( fp, "%i,%i\n",&x,&y )){ + maxx = MAX(maxx,x); + maxy = MAX(maxy,y); + } + + printf("Max x: %i Max y: %i\n", maxx, maxy); + + bool **paper=(bool**)malloc( (maxx+1) * sizeof(bool *)); + for(unsigned int i=0; i<=maxx;i++) + { + paper[i]=(bool*)malloc( (maxy+1) *sizeof(bool)); + memset(paper[i], 0, sizeof(bool) * ( maxy+1 )); + } + + rewind(fp); + while ( fscanf( fp, "%i,%i\n",&x,&y )){ + paper[x][y] = true; + } + + printPaper( maxx+1, maxy+1, paper ); + printf("\n"); + + + char direction = '\0'; + unsigned int position = 0; + while (!feof(fp) && fscanf(fp, "fold along %c=%i\n", &direction, &position)){ + printf("Trying to fold in %c direction at position %i\n", direction, position); + if ( direction == 'x' ){ + paper = foldPaperX(maxx+1, maxy+1, position, paper); + maxx = position - 1; + printPaper( maxx + 1, maxy + 1, paper ); + } else { + paper = foldPaperY(maxx+1, maxy+1, position, paper); + maxy = position - 1; + printPaper( maxx+1, maxy+1, paper ); + } + + } + + + fclose(fp); + + return 0; + } else { + printf("You need to provide a file\n"); + return 1; + } +}