From: Mart Lubbers Date: Tue, 21 Dec 2021 09:52:09 +0000 (+0100) Subject: add day 21 X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=fd75dde606a6f6a3d93b9b971fe1dfc78db1113b;p=advent21.git add day 21 --- diff --git a/19.txt b/19.txt new file mode 100644 index 0000000..4dec4fb --- /dev/null +++ b/19.txt @@ -0,0 +1,717 @@ +--- scanner 0 --- +-610,495,658 +619,646,813 +824,672,-795 +46,-39,48 +417,571,821 +-805,453,-303 +-888,396,-338 +-476,-659,-760 +-565,451,589 +-360,-695,-736 +747,682,-698 +-640,-788,852 +-456,499,642 +-639,-860,768 +755,-706,572 +-510,-763,-684 +326,-630,-599 +486,672,902 +426,-582,-537 +773,654,-588 +-484,-887,839 +395,-595,-768 +-735,509,-330 +735,-629,606 +810,-851,608 + +--- scanner 1 --- +691,-615,-596 +459,-820,345 +727,421,-583 +-504,778,-728 +-304,-931,507 +680,257,604 +105,-55,-128 +625,275,720 +-490,654,-884 +547,-713,263 +722,381,-665 +587,-500,-582 +723,-543,-526 +-324,402,601 +-706,-573,-643 +566,303,707 +687,485,-677 +-382,-788,510 +-8,-168,-10 +408,-812,285 +-852,-572,-742 +-395,727,-808 +-378,372,416 +-793,-676,-748 +-364,-872,401 +-455,385,572 + +--- scanner 2 --- +-630,539,-832 +453,-736,716 +-620,-486,518 +409,-817,634 +340,-827,665 +390,847,-459 +343,713,-531 +-730,-493,648 +449,614,601 +-862,-862,-324 +-33,-19,15 +507,583,604 +-562,629,-717 +-396,674,378 +656,-758,-378 +-775,-556,549 +386,595,-469 +619,-844,-379 +-529,509,-821 +-331,644,348 +-906,-785,-305 +-877,-919,-415 +584,584,578 +-444,785,330 +587,-926,-418 + +--- scanner 3 --- +789,696,-627 +-434,-942,427 +-333,613,529 +758,626,-782 +-323,716,-425 +907,-683,654 +591,446,446 +766,-789,-704 +673,365,402 +34,11,-117 +-347,-799,419 +154,-87,57 +-309,569,-489 +773,-813,-798 +-287,606,-505 +-601,-856,-467 +659,689,-737 +895,-719,864 +-666,-833,-413 +944,-718,635 +-445,-937,366 +-731,-759,-465 +-491,676,498 +-470,608,672 +642,355,333 +794,-861,-887 + +--- scanner 4 --- +-594,-270,766 +-18,91,75 +-408,-554,-590 +-496,427,605 +787,-666,849 +-541,-326,669 +590,914,-478 +-607,-251,615 +819,-259,-460 +-613,-589,-554 +675,-271,-532 +566,411,590 +680,457,487 +699,-559,931 +578,911,-741 +-506,-436,-520 +838,-658,909 +815,-307,-557 +621,429,474 +-794,879,-625 +-440,554,550 +-660,890,-528 +-791,864,-664 +-556,410,584 +543,956,-616 + +--- scanner 5 --- +-175,44,-29 +800,604,736 +-600,591,-761 +-665,-613,322 +349,440,-453 +-818,-516,-844 +699,-529,-374 +-755,-545,437 +708,-480,-508 +-499,490,-693 +-843,-572,-909 +284,512,-486 +526,-505,-423 +-805,-538,-751 +-20,-22,55 +-839,360,623 +632,673,776 +793,-860,739 +659,-889,849 +-973,407,695 +-721,403,685 +736,-845,830 +718,636,775 +319,535,-369 +-606,618,-669 +-635,-512,333 + +--- scanner 6 --- +-316,579,-312 +-297,-790,-711 +-348,-730,-719 +435,-474,503 +607,833,578 +-557,860,858 +-436,-812,-612 +415,-568,-830 +53,112,121 +698,887,-370 +657,774,487 +566,-503,486 +-455,-368,404 +816,926,-466 +-569,-471,358 +-545,597,861 +741,896,506 +-320,676,-360 +821,807,-428 +-514,-355,376 +-460,651,-324 +-592,735,903 +151,164,-31 +475,-674,-771 +489,-659,473 +493,-565,-687 + +--- scanner 7 --- +435,557,-655 +-770,550,-327 +-699,515,-271 +-577,-446,442 +451,-860,586 +-808,647,655 +637,-442,-503 +-567,-543,519 +280,262,615 +319,360,662 +-937,-885,-798 +-733,621,601 +-650,645,652 +801,-410,-429 +-66,-68,143 +537,711,-636 +297,271,557 +-595,-653,493 +773,-436,-359 +341,-912,565 +478,737,-709 +-696,352,-330 +411,-845,593 +-849,-848,-800 +-698,-875,-797 + +--- scanner 8 --- +-664,-627,704 +-841,494,-595 +652,-801,406 +-862,425,-585 +637,459,554 +530,-629,-385 +-515,697,751 +691,487,683 +-552,-681,-615 +686,-801,451 +-552,720,692 +-634,-725,-696 +-603,-794,-706 +298,374,-483 +-690,-550,705 +509,-455,-453 +316,454,-362 +326,504,-379 +372,-598,-448 +-583,833,701 +540,-833,430 +-59,-49,80 +645,513,567 +-667,-685,735 +-848,556,-701 + +--- scanner 9 --- +414,487,-749 +-680,-624,-479 +-67,-57,-44 +484,-790,-397 +583,522,-818 +816,-659,919 +-706,-549,-533 +621,282,721 +826,-420,920 +-714,346,-731 +-704,-707,-530 +-665,637,422 +668,240,583 +594,-762,-371 +-574,-658,393 +645,474,-718 +633,285,458 +3,-121,131 +387,-780,-320 +-829,281,-756 +-581,656,544 +-531,565,485 +789,-508,922 +-609,-587,449 +-411,-661,452 +-890,366,-746 + +--- scanner 10 --- +-627,-609,-816 +-591,-495,-690 +-585,-502,631 +-650,814,782 +-858,529,-758 +380,305,-577 +-647,671,704 +444,-523,-703 +751,-594,590 +-31,-131,-124 +-756,755,722 +-745,551,-609 +319,-487,-616 +452,488,-605 +725,412,376 +844,450,381 +-826,591,-589 +692,-503,540 +-575,-442,-752 +428,394,-598 +361,-474,-773 +-741,-489,708 +805,354,499 +-556,-519,762 +814,-522,456 + +--- scanner 11 --- +-838,-676,-466 +511,284,707 +52,-49,-52 +-742,-794,-427 +-504,423,318 +416,368,677 +612,-860,308 +-127,-13,-146 +-848,342,-677 +542,-744,-631 +-884,-824,233 +-913,-934,367 +645,433,-888 +-884,482,-702 +649,-861,394 +-780,-799,-546 +558,-658,-628 +-896,338,-774 +-869,-915,290 +488,401,742 +-529,338,500 +771,421,-767 +620,490,-741 +580,-821,226 +479,-690,-459 +-553,289,327 + +--- scanner 12 --- +-717,579,534 +766,749,-954 +177,64,26 +666,686,530 +-672,728,-741 +-604,548,543 +632,-727,-468 +949,-544,805 +-684,-574,-532 +-371,-695,552 +671,-668,-552 +801,663,500 +-343,-641,681 +661,-697,-616 +-643,670,-662 +99,-14,-127 +852,697,-857 +917,-690,784 +-397,-649,668 +883,593,-939 +-5,128,-65 +-639,-550,-476 +-581,511,484 +743,660,531 +917,-656,829 +-686,703,-802 +-637,-530,-607 + +--- scanner 13 --- +-326,-746,-526 +753,-765,472 +-329,-483,-514 +710,500,848 +-284,-608,-479 +570,-829,453 +-518,693,-502 +19,28,139 +-361,-599,858 +615,-944,-331 +-566,793,-564 +-350,-696,724 +-642,364,607 +-787,309,643 +746,372,-663 +-531,724,-733 +712,-877,524 +736,295,-760 +109,-158,-31 +635,702,856 +718,346,-553 +376,-945,-321 +736,633,937 +-376,-560,855 +-565,384,641 +464,-947,-358 + +--- scanner 14 --- +287,538,833 +456,-658,-332 +649,-664,559 +-499,651,634 +437,470,781 +566,373,-671 +338,-640,-388 +-676,-568,-309 +-682,-426,-329 +-855,-673,936 +-682,-697,887 +567,445,-755 +687,-630,401 +662,-686,466 +411,346,-742 +-57,-87,160 +-828,527,-475 +4,86,-14 +350,-743,-361 +-399,603,696 +-808,604,-509 +-468,638,865 +-772,-547,-416 +403,574,857 +-883,382,-511 +-864,-722,802 + +--- scanner 15 --- +469,-858,324 +693,669,-643 +-397,-551,-564 +478,-668,303 +157,-16,-98 +-447,698,716 +-377,729,561 +439,-707,-392 +692,643,-566 +-2,74,-8 +753,899,272 +-467,-347,439 +723,646,-508 +-504,-369,423 +-708,613,-468 +461,-644,388 +894,917,385 +783,869,337 +-460,-393,-595 +-384,599,671 +-465,-530,-738 +-718,582,-399 +-627,531,-512 +-357,-383,359 +394,-673,-392 +418,-561,-360 + +--- scanner 16 --- +663,520,581 +-790,410,759 +429,-660,365 +-840,596,-622 +700,-660,-851 +-379,-459,547 +333,614,-851 +-816,546,628 +550,-570,452 +-524,-514,511 +-648,-843,-598 +66,-103,36 +-601,-534,545 +571,-666,345 +-600,-943,-660 +345,542,-707 +628,484,376 +-882,458,-575 +-635,-874,-829 +-98,-25,-89 +381,517,-847 +510,554,474 +-837,333,-610 +676,-487,-900 +725,-478,-766 +-805,449,762 + +--- scanner 17 --- +574,-491,651 +537,816,632 +521,665,-272 +-630,560,-520 +-370,-583,-408 +444,822,542 +675,-414,640 +-631,382,-571 +636,-494,-690 +662,670,-394 +-47,-16,78 +-653,356,-528 +-367,-635,410 +-351,-438,-486 +669,670,-300 +-324,-507,464 +585,-397,-567 +593,-418,678 +-658,603,839 +-416,-570,-482 +-368,-478,428 +562,-544,-547 +411,832,763 +-721,643,761 +-672,451,760 + +--- scanner 18 --- +581,674,-678 +-850,707,-718 +814,-386,-737 +-877,-306,-508 +550,677,-620 +-718,-253,783 +-860,738,-787 +538,789,727 +-739,479,432 +409,-219,436 +597,826,704 +-712,-444,690 +562,-396,-741 +438,-264,382 +-791,-305,-471 +680,-458,-784 +-959,-345,-540 +-90,118,131 +-769,789,-674 +-855,493,563 +-18,-7,-47 +551,651,-543 +547,758,840 +-632,-391,788 +-749,500,501 +447,-419,482 + +--- scanner 19 --- +-476,-439,905 +633,600,635 +-583,-527,987 +52,-52,78 +-365,324,743 +492,621,-719 +498,-427,608 +766,657,691 +784,-490,-411 +571,589,-550 +-883,-407,-404 +-484,-388,965 +555,675,-547 +776,598,490 +379,-429,438 +-871,-551,-476 +-835,-411,-501 +-729,725,-751 +-825,607,-712 +-767,730,-632 +658,-502,-290 +633,-559,-437 +334,-462,592 +-424,384,849 +-508,402,740 + +--- scanner 20 --- +590,-944,-534 +475,296,714 +-735,-479,-428 +456,418,630 +-355,-394,819 +746,-872,939 +-326,-528,929 +-343,613,-343 +-459,385,794 +-391,516,-347 +830,595,-572 +625,-875,-365 +766,-857,763 +-370,-451,910 +-355,541,-487 +638,-844,-596 +827,332,-584 +872,457,-619 +-401,305,671 +-497,266,789 +-686,-488,-294 +80,-56,98 +547,359,580 +-60,-91,-46 +831,-882,878 +-710,-661,-343 + +--- scanner 21 --- +374,727,-469 +508,655,-422 +504,-877,510 +-350,604,-754 +747,-729,-653 +-258,573,-801 +-712,-467,-629 +-715,-591,-772 +592,469,483 +-757,-666,590 +-683,-594,537 +682,354,551 +741,-637,-680 +848,-661,-552 +-616,716,435 +106,90,-59 +712,365,450 +-9,-37,39 +-549,657,492 +-641,-565,597 +-407,652,-862 +-679,-594,-517 +529,-812,454 +626,717,-465 +-587,738,431 +542,-719,476 + +--- scanner 22 --- +-434,572,693 +-527,-427,869 +697,410,-675 +583,-761,494 +812,308,570 +-655,-470,923 +-498,-471,905 +-511,471,682 +-552,260,-484 +-564,-942,-373 +513,-641,-413 +560,-506,-520 +682,491,-679 +623,-824,572 +796,434,701 +-570,-751,-466 +54,-67,182 +-520,448,-474 +-665,368,-500 +802,341,791 +717,558,-804 +-438,490,655 +546,-727,-464 +-650,-804,-368 +-85,-160,55 +431,-775,573 + +--- scanner 23 --- +466,697,-742 +-453,-516,611 +288,626,597 +573,-781,-775 +438,769,-925 +-54,-38,-158 +459,721,-836 +-520,305,-780 +-515,424,-818 +-638,344,-876 +-380,-489,-951 +-570,509,504 +443,-788,-665 +325,591,616 +-624,337,441 +641,-538,408 +559,-457,364 +-656,506,520 +337,627,826 +-454,-430,574 +-493,-348,542 +474,-718,-820 +-490,-355,-922 +590,-447,295 +-399,-412,-907 + +--- scanner 24 --- +-705,508,-364 +483,-792,588 +-521,-543,-626 +-554,-629,-575 +-370,-621,758 +-539,-589,679 +-568,-408,-519 +315,-752,657 +-652,559,617 +562,-653,-573 +-690,589,-360 +26,-59,116 +831,470,749 +512,546,-565 +657,466,-539 +-678,490,466 +-799,502,592 +766,437,821 +796,353,726 +-542,-675,695 +556,-844,-666 +544,358,-555 +-725,656,-235 +397,-791,625 +642,-706,-671 + +--- scanner 25 --- +-549,544,757 +24,-9,-115 +-652,560,-521 +-326,-827,581 +-746,553,-497 +-611,713,789 +706,419,-627 +-728,-421,-794 +-385,-767,687 +-835,532,-449 +-690,-426,-913 +805,-494,529 +-589,564,676 +594,-476,-709 +-469,-749,578 +551,890,246 +687,382,-592 +527,881,472 +-786,-544,-874 +610,-562,-712 +916,431,-591 +531,-530,-881 +815,-553,328 +497,886,320 +672,-551,473 diff --git a/19a.c b/19a.c new file mode 100644 index 0000000..e468798 --- /dev/null +++ b/19a.c @@ -0,0 +1,88 @@ +#include +#include +#include + +struct point {int x; int y; int z; }; +struct scanner { + int num; + int npoints; + struct point points[100]; +}; + +bool parse_scanner(struct scanner *s) +{ + char *b = NULL; + size_t len; + if (-1 == getline(&b, &len, stdin)) + return false; + sscanf(b, "--- scanner %d ---\n", &s->num); + while (getline(&b, &len, stdin) != -1 && strcmp(b, "\n") != 0) { + sscanf(b, "%d,%d,%d\n" + , &s->points[s->npoints].x + , &s->points[s->npoints].y + , &s->points[s->npoints].z); + s->npoints++; + } + return true; +} + +struct point orient(struct point p, struct point r, int orientation) +{ + if (orientation == 0) + return (struct point){.x=r.x*p.x, .y=r.y*p.y, .z=r.z*p.z}; + else if (orientation == 1) + return (struct point){.x=r.x*p.x, .y=r.y*p.z, .z=r.z*p.y}; + else if (orientation == 2) + return (struct point){.x=r.x*p.y, .y=r.y*p.x, .z=r.z*p.z}; + else if (orientation == 3) + return (struct point){.x=r.x*p.y, .y=r.y*p.z, .z=r.z*p.x}; + else if (orientation == 4) + return (struct point){.x=r.x*p.z, .y=r.y*p.x, .z=r.z*p.y}; + else if (orientation == 5) + return (struct point){.x=r.x*p.z, .y=r.y*p.y, .z=r.z*p.x}; + else + fprintf(stderr, "unknown orientation\n"); +} +struct point rotations[] = + { {-1, -1, -1} + , {-1, -1, 1} + , {-1, 1, -1} + , {-1, 1, 1} + , { 1, -1, -1} + , { 1, -1, 1} + , { 1, 1, -1} + , { 1, 1, 1} + }; + +void match_scanners(struct scanner *l, struct scanner *r, int rot, int or) +{ + for (int dx = -1000; dx<1000; dx++) { + for (int dy = -1000; dy<1000; dy++) { + for (int dz = -1000; dz<1000; dz++) { + + + } + } + } +} + +int main() +{ + int nscanner = 0; + struct scanner scanners[25]; + while(parse_scanner(&scanners[nscanner])) + nscanner++; + + for (int i = 0; i + +void move (int roll, int *p, int *score) +{ + *p = (*p+roll*9+6)%10; + if (*p == 0) + *p = 10; + *score += *p; +} + +int main() +{ + int p1, p2, p1score = 0, p2score = 0; + if (2 != scanf( + "Player 1 starting position: %d\n" + "Player 2 starting position: %d\n", &p1, &p2)) + return 1; + + int roll = 0; + while (p1score < 1000 && p2score < 1000) { + if (roll%2==0) + move(roll, &p1, &p1score); + else + move(roll, &p2, &p2score); + roll++; + } + printf("%d\n", (roll%2==0 ? p1score : p2score)*roll*3); +} diff --git a/21b.c b/21b.c new file mode 100644 index 0000000..96153cc --- /dev/null +++ b/21b.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include + +struct result { unsigned long p1wins; unsigned long p2wins; }; +struct player { int pos; int score; }; +struct cache { + struct key { bool p1turn; struct player p1; struct player p2; } key; + struct result res; + UT_hash_handle hh; +}; + +struct player move (struct player p, int steps) +{ + p.pos = (p.pos+steps)%10; + if (p.pos == 0) + p.pos = 10; + p.score += p.pos; + return p; +} + +struct cache *cache = NULL; + +struct result playd (bool p1turn, struct player p1, struct player p2); +struct result play(bool p1turn, struct player p1, struct player p2) +{ + struct cache *e; + struct key p; + memset(&p, 0, sizeof(p)); + p = (struct key){.p1turn=p1turn, .p1=p1, .p2=p2}; + HASH_FIND(hh, cache, &p, sizeof(struct key), e); + if (e == NULL) { + struct result r = playd(p1turn, p1, p2); + e = calloc(1, sizeof(struct cache)); + *e = (struct cache){.key=p, .res=r}; + HASH_ADD(hh, cache, key, sizeof(struct key), e); + return r; + } else { + return e->res; + } +} + +void add_result(struct result *l, struct result r) +{ + l->p1wins += r.p1wins; + l->p2wins += r.p2wins; +} + +struct result playd (bool p1turn, struct player p1, struct player p2) +{ + struct result r = {.p1wins=0, .p2wins=0}; + if (p1.score >= 21) { + r.p1wins++; + } else if (p2.score >= 21) { + r.p2wins++; + } else if (p1turn) { + for (int i = 1; i<=3; i++) + for (int j = 1; j<=3; j++) + for (int k = 1; k<=3; k++) + add_result(&r, play(!p1turn, move(p1, i+j+k), p2)); + } else { + for (int i = 1; i<=3; i++) + for (int j = 1; j<=3; j++) + for (int k = 1; k<=3; k++) + add_result(&r, play(!p1turn, p1, move(p2, i+j+k))); + } + return r; +} + +int main() +{ + struct player p1 = {.score=0}, p2 = {.score=0}; + if (2 != scanf( + "Player 1 starting position: %d\n" + "Player 2 starting position: %d\n", &p1.pos, &p2.pos)) + return 1; + + struct result r = play(true, p1, p2); + printf("%lu\n", r.p1wins > r.p2wins ? r.p1wins : r.p2wins); +} diff --git a/21e.txt b/21e.txt new file mode 100644 index 0000000..3f69194 --- /dev/null +++ b/21e.txt @@ -0,0 +1,2 @@ +Player 1 starting position: 4 +Player 2 starting position: 8 diff --git a/Makefile b/Makefile index b6f6074..b03dbc8 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS:=-Wall -Wextra -O3 LFLAGS:=-f -BINARIES:=$(filter-out 19a 19b,$(foreach num,$(shell seq -f '%02.0f' 1 20),$(num)a $(num)b)) +BINARIES:=$(filter-out 19a 19b,$(foreach num,$(shell seq -f '%02.0f' 1 21),$(num)a $(num)b)) all: $(BINARIES)