advent2021

Advent of Code 2021 Solutions
git clone git://bsandro.tech/advent2021
Log | Files | Refs

commit 68ba4ca5e9020a2064f94f6bbfaa711ad0a1a935
parent 734396f1e1a6ae6e7c9560fa3232e7baf3601f49
Author: bsandro <[email protected]>
Date:   Sun, 19 Dec 2021 23:36:42 +0200

Day 19, puzzle 1 WIP (scared to lose progress lol)

Diffstat:
Aday19/Makefile | 25+++++++++++++++++++++++++
Aday19/input.txt | 804+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday19/main.c | 29+++++++++++++++++++++++++++++
Aday19/puzzle.c | 158+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday19/test.txt | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 1152 insertions(+), 0 deletions(-)

diff --git a/day19/Makefile b/day19/Makefile @@ -0,0 +1,25 @@ +NAME=$(shell basename ${PWD}) +SRC=$(wildcard *.c ../common/*.c) +DEPS:=$(wildcard *.h ../common/*.h) +OBJ:=$(SRC:.c=.o) +CFLAGS=-O2 -std=c99 -Werror -Wall -Wextra -I. -I../common +LDFLAGS=-lc + +all: $(NAME) + +.PHONY: clean run + +clean: + rm -f $(OBJ) $(NAME) + +%.o : %.c $(DEPS) + @$(CC) $(CFLAGS) -c $< -o $@ + +$(NAME): $(OBJ) + @$(CC) $(OBJ) -o $@ $(LDFLAGS) + +run: $(NAME) + @./$(NAME) input.txt + +test: $(NAME) + @./$(NAME) test.txt diff --git a/day19/input.txt b/day19/input.txt @@ -0,0 +1,804 @@ +--- scanner 0 --- +328,-702,-460 +-786,-700,353 +416,571,609 +487,-724,-505 +593,-619,491 +-526,474,399 +247,-727,-523 +-750,-624,-790 +796,689,-767 +766,688,-823 +741,764,-835 +-916,713,-783 +-742,-762,-708 +629,-657,378 +-540,521,304 +568,-599,459 +441,481,636 +-457,540,428 +-910,550,-662 +-784,-650,-694 +-874,-840,421 +-3,-12,-71 +-800,-738,421 +-844,654,-686 +477,598,648 + +--- scanner 1 --- +135,1,-54 +895,551,601 +-393,818,-781 +-278,809,-841 +-409,-507,427 +699,705,-637 +732,522,-710 +-331,753,-812 +-510,-596,-424 +593,-279,757 +785,-655,-330 +766,-738,-454 +-1,124,94 +863,511,718 +-579,-621,-358 +832,-669,-425 +756,-285,636 +-487,-483,539 +-582,513,809 +677,658,-649 +652,-351,660 +-684,-548,-420 +-693,573,775 +-573,-397,416 +871,666,635 +-521,573,821 + +--- scanner 2 --- +420,706,-547 +-463,-451,382 +592,-360,835 +565,-579,836 +542,709,759 +-555,-446,456 +-763,656,-604 +-385,-534,419 +-695,716,-551 +-769,-416,-710 +99,56,-37 +-742,412,442 +629,813,693 +-613,-475,-640 +424,690,-512 +-62,-58,56 +-594,433,458 +-737,597,-642 +-567,444,499 +-664,-447,-778 +736,683,716 +865,-748,-836 +897,-799,-819 +845,-863,-748 +591,-483,766 +381,557,-439 + +--- scanner 3 --- +345,459,753 +-613,621,-540 +464,478,691 +601,-692,717 +-642,-779,-665 +470,-697,-820 +570,261,-770 +77,-114,37 +726,-736,687 +-507,-674,539 +553,-821,-736 +-611,426,665 +-463,-783,-641 +-618,569,-574 +-558,-811,-732 +-682,599,-515 +-319,-627,490 +-55,-17,-58 +444,-739,-785 +382,288,-702 +-384,-726,460 +356,615,698 +-397,459,628 +654,-596,605 +-621,396,618 +452,311,-673 + +--- scanner 4 --- +332,-333,-452 +-487,-800,717 +-592,843,876 +595,-810,841 +304,556,-441 +460,797,974 +-64,-30,10 +638,698,966 +-611,-702,-364 +-607,-805,-360 +-652,661,872 +-616,944,-495 +-504,696,939 +440,-482,-470 +582,920,947 +628,-709,865 +-574,-801,582 +451,-794,849 +90,52,114 +-655,-814,-359 +330,538,-496 +-540,-833,787 +-738,887,-501 +327,-359,-454 +436,610,-517 +-547,857,-467 + +--- scanner 5 --- +622,-797,923 +522,552,-282 +701,-659,955 +629,-698,-500 +676,-686,-478 +-684,-379,-392 +487,-694,959 +637,586,-306 +482,669,-314 +-30,37,138 +-440,619,779 +-424,-496,867 +-350,-496,969 +-482,633,891 +-308,708,-584 +-767,-503,-411 +727,768,865 +-438,-428,829 +-314,587,-421 +493,-759,-445 +720,622,751 +-380,611,-525 +-698,-474,-345 +-474,715,753 +123,-117,40 +658,715,848 + +--- scanner 6 --- +-717,498,-633 +610,701,547 +595,624,725 +-161,80,-68 +-817,-346,834 +-864,451,-668 +613,-829,533 +466,476,-568 +622,-588,-766 +-815,-405,-717 +21,-34,-63 +-754,-482,-740 +-741,343,476 +-712,385,605 +301,522,-500 +-838,-418,-811 +-761,-375,781 +-687,409,-737 +738,611,608 +588,-763,602 +636,-527,-709 +-826,-406,719 +-754,324,575 +284,374,-561 +675,-446,-708 +540,-767,428 + +--- scanner 7 --- +-752,839,276 +317,510,-449 +-668,-536,-581 +-721,-648,-677 +-977,-684,474 +-852,889,-682 +231,555,-478 +347,-426,375 +467,544,734 +294,492,-637 +-870,742,326 +268,-779,-749 +-858,-622,493 +253,-726,-767 +-966,-629,630 +-65,80,-89 +225,-699,-686 +458,-366,341 +631,654,723 +519,-426,329 +553,630,611 +-940,771,303 +-769,805,-806 +-596,-618,-721 +-878,854,-724 + +--- scanner 8 --- +727,-725,925 +-688,403,-506 +-703,-424,442 +775,686,858 +702,-656,859 +852,-650,-515 +-627,-782,-247 +-676,303,-373 +844,620,829 +-731,319,-351 +-598,-653,-289 +-416,586,573 +-712,-539,502 +-552,531,634 +-640,-683,-228 +761,639,953 +709,-533,-528 +564,760,-677 +-647,-477,471 +724,-711,-605 +549,642,-710 +28,-36,105 +651,702,-765 +654,-830,863 +-599,553,646 + +--- scanner 9 --- +764,334,-701 +799,357,-509 +-435,446,-654 +-102,-105,-51 +743,489,602 +437,-387,486 +710,548,770 +-614,-599,-897 +572,-895,-506 +-516,546,785 +-582,485,-643 +-546,581,-659 +493,-892,-446 +679,503,771 +-702,-801,586 +-756,-933,510 +795,352,-642 +-535,437,679 +34,27,-113 +-392,-655,-878 +-515,-777,-905 +432,-848,-409 +-774,-907,565 +-467,533,747 +371,-347,305 +506,-362,424 + +--- scanner 10 --- +710,734,-533 +604,-860,729 +729,616,738 +703,422,695 +658,-926,-484 +-46,-63,-6 +-779,261,-840 +726,738,-568 +-678,-645,-733 +634,-771,625 +577,777,-460 +-384,-747,492 +-843,313,-769 +671,-945,-583 +-367,-895,553 +-821,-658,-616 +-693,-568,-555 +-559,593,815 +-549,-793,560 +629,-884,-613 +781,-818,660 +833,529,672 +-728,293,-857 +-463,567,745 +-340,566,814 + +--- scanner 11 --- +447,-533,-477 +442,-411,-424 +739,600,-576 +676,-753,620 +-132,65,16 +554,-810,626 +-392,-404,625 +680,555,-758 +-502,616,651 +-498,-476,604 +545,939,761 +-749,-530,-408 +-394,467,669 +-549,635,-716 +-366,-422,680 +-389,493,667 +485,-723,654 +-455,659,-737 +-530,843,-757 +453,-457,-318 +725,602,-790 +-821,-606,-392 +549,836,757 +593,948,762 +-765,-689,-459 + +--- scanner 12 --- +645,-718,-548 +690,581,410 +604,435,-341 +-631,-261,708 +-312,616,737 +-346,-634,-376 +-502,578,-394 +32,104,4 +594,416,-524 +-624,573,-341 +580,-781,787 +535,515,484 +-505,-618,-325 +524,-688,-633 +-613,-235,763 +-429,-690,-309 +649,-806,849 +651,523,534 +514,-821,968 +-578,584,-235 +-225,685,709 +-282,807,703 +182,159,189 +687,-614,-617 +629,411,-558 +-559,-367,687 + +--- scanner 13 --- +-339,-816,-841 +368,775,595 +452,692,719 +-823,-543,796 +627,-630,-698 +-415,814,-529 +-436,768,-542 +-362,-604,-830 +713,-544,-821 +-904,-350,775 +765,-371,550 +-875,-354,841 +-386,759,-683 +-381,-661,-791 +603,-708,-810 +418,675,-363 +486,863,-390 +702,-308,391 +34,108,-71 +-642,530,427 +-519,527,330 +-508,620,464 +515,793,-413 +403,801,637 +747,-394,362 + +--- scanner 14 --- +655,464,-418 +-735,-531,-975 +-450,-751,228 +35,155,-30 +609,419,-432 +-510,-629,331 +639,776,592 +839,-428,403 +-723,-539,-910 +563,-468,-514 +-412,659,-837 +650,923,631 +-473,821,-805 +-405,744,-868 +601,-476,-487 +60,10,-171 +655,812,496 +-789,842,531 +704,-488,-515 +-683,823,640 +-486,-667,266 +891,-493,421 +898,-628,374 +-743,-624,-863 +-751,891,544 +547,533,-503 + +--- scanner 15 --- +271,396,695 +-693,664,-773 +307,531,798 +365,636,-767 +479,629,-621 +477,-565,-714 +363,-515,497 +-941,-590,703 +382,567,-714 +-838,-533,715 +-853,526,744 +-786,568,-749 +-831,684,640 +-717,-492,-640 +-916,-449,741 +339,-573,-632 +451,-531,-618 +-817,503,-772 +261,-441,497 +275,610,660 +-945,699,710 +0,-150,110 +-766,-430,-636 +-100,40,80 +401,-529,429 +-751,-439,-466 + +--- scanner 16 --- +-456,-288,521 +-522,594,-368 +-555,-296,699 +552,-747,406 +-333,-315,643 +-522,469,374 +629,505,630 +782,-458,-808 +533,617,683 +631,-687,403 +491,-784,450 +804,-614,-707 +-34,45,146 +-541,531,493 +663,762,-355 +-678,524,385 +-557,-323,-443 +-592,-390,-591 +87,6,-3 +564,732,-290 +611,683,-478 +-489,557,-582 +-410,-386,-531 +858,-471,-804 +-459,604,-592 +673,535,755 + +--- scanner 17 --- +424,-503,-572 +653,616,662 +508,688,-780 +681,544,596 +-629,422,734 +624,542,-766 +-781,-500,829 +-808,-432,641 +-341,348,-638 +-758,-529,759 +-380,367,-695 +-473,-641,-664 +726,-480,670 +-330,325,-850 +-361,-617,-742 +490,-528,-641 +607,544,485 +486,-576,-467 +665,-494,727 +807,-441,669 +-350,-727,-578 +-488,372,725 +702,710,-807 +-642,401,780 +27,86,-26 +151,-78,10 + +--- scanner 18 --- +708,-520,-693 +-609,-443,720 +830,-512,758 +817,-418,781 +647,-463,-679 +140,-7,38 +-466,634,-511 +-488,703,-486 +663,621,-513 +-628,-324,782 +-661,632,694 +693,606,-381 +682,649,732 +-776,-431,-644 +739,516,-396 +789,-521,-732 +753,-489,697 +660,591,720 +-799,-361,-673 +-691,642,647 +-826,-549,-741 +-628,418,657 +-518,639,-545 +-29,90,-28 +-472,-335,727 +584,639,616 + +--- scanner 19 --- +555,448,-664 +489,644,897 +807,-617,-330 +-628,-812,-740 +531,453,-431 +21,-61,175 +-817,-409,768 +751,-432,616 +-169,-98,27 +-581,-799,-619 +-725,416,-249 +764,-554,-250 +420,821,861 +-580,-767,-567 +779,-536,-303 +-433,337,571 +616,391,-510 +-802,315,-338 +728,-397,808 +-796,478,-212 +-391,272,679 +-968,-364,676 +-819,-397,703 +733,-401,675 +-423,445,650 +446,822,883 + +--- scanner 20 --- +685,-619,765 +-750,363,-301 +708,-746,-819 +-537,524,651 +855,617,-843 +-816,367,-330 +-402,-517,-563 +-305,-514,-686 +-524,478,561 +-825,-428,728 +866,829,-866 +-393,564,579 +-701,-459,774 +94,-74,108 +696,-592,772 +716,-698,671 +878,751,438 +-771,556,-382 +567,-755,-769 +816,674,-824 +-439,-607,-697 +162,44,-40 +554,-801,-790 +901,746,499 +944,639,369 +-773,-456,889 + +--- scanner 21 --- +-487,635,505 +-634,670,552 +744,-554,527 +-597,563,-428 +-536,586,-351 +378,680,-268 +-513,492,-364 +727,599,534 +-678,-565,-597 +432,-455,-245 +-562,-575,-499 +-558,-232,924 +715,-733,568 +-617,-391,875 +-664,566,463 +739,545,393 +-610,-268,910 +710,422,495 +-30,114,54 +370,536,-284 +637,-489,-277 +-574,-517,-521 +354,713,-369 +502,-402,-278 +737,-618,629 + +--- scanner 22 --- +-689,-622,794 +-749,591,-627 +730,-593,-587 +-280,-577,-899 +119,-103,-156 +863,-594,790 +-451,700,392 +-838,-595,827 +769,-582,-440 +-365,-685,-830 +815,684,-487 +937,411,574 +894,331,637 +-741,746,-544 +-450,648,424 +-498,821,379 +-294,-623,-944 +881,-535,759 +643,684,-441 +858,-727,794 +-687,-687,782 +909,384,655 +-28,-43,-19 +-773,706,-661 +634,-577,-551 +704,744,-453 + +--- scanner 23 --- +-782,-645,-730 +-836,-788,-827 +-543,358,913 +49,-174,-59 +665,520,474 +355,-944,620 +737,499,368 +432,-873,-554 +418,470,-509 +331,498,-578 +-530,-512,383 +-408,239,928 +-474,420,888 +-609,-572,380 +719,444,342 +-469,-475,427 +-545,673,-416 +-429,723,-408 +614,-869,-614 +388,504,-700 +-60,-30,48 +-760,-801,-831 +-584,797,-474 +577,-963,676 +478,-971,498 +579,-936,-466 + +--- scanner 24 --- +654,-590,415 +513,-610,416 +567,801,-730 +326,-615,-520 +378,-550,-502 +670,411,404 +-614,-670,594 +578,654,-691 +616,-598,388 +-699,800,-604 +-699,754,-776 +319,-750,-473 +562,690,-595 +-463,-862,-361 +-759,788,-665 +-750,-731,649 +-761,697,780 +-567,-658,616 +-519,-833,-414 +-606,614,788 +-67,-96,74 +608,297,333 +562,342,319 +53,-10,-98 +-454,-802,-476 +-721,580,812 + +--- scanner 25 --- +-546,411,381 +362,-611,742 +-14,15,69 +387,-542,680 +-518,-591,-565 +-489,-848,564 +133,-129,-34 +-785,630,-488 +907,669,601 +450,-607,-655 +-548,-815,396 +933,651,656 +-570,-735,-595 +-533,362,419 +-437,430,487 +-851,635,-473 +347,-476,861 +550,543,-432 +480,-486,-740 +534,384,-483 +-487,-670,-668 +801,597,580 +-726,656,-552 +554,-649,-724 +-503,-784,395 +478,467,-416 + +--- scanner 26 --- +568,363,552 +-733,755,905 +-789,784,958 +-726,648,-616 +-137,-77,58 +-811,-733,-781 +-810,712,-687 +-553,-878,715 +-787,725,-593 +-798,724,843 +664,603,-585 +644,-904,-744 +712,-931,-556 +-10,-145,190 +703,684,-584 +-493,-866,654 +503,-596,749 +650,542,-642 +-756,-577,-770 +641,301,657 +-749,-714,-662 +616,367,726 +527,-484,827 +615,-881,-674 +417,-473,663 +-553,-832,543 + +--- scanner 27 --- +672,770,794 +846,925,-583 +492,-796,-624 +842,-495,695 +927,-672,679 +-423,-819,-611 +-744,560,553 +-664,-663,804 +-405,-793,-666 +-787,-541,777 +808,860,-607 +-7,96,-43 +807,817,801 +-597,599,542 +-712,647,-848 +-633,695,558 +-565,-485,796 +607,788,742 +615,-683,-640 +943,-528,671 +-477,-687,-687 +466,-791,-622 +-663,660,-886 +168,23,-107 +-644,551,-780 +930,963,-547 + +--- scanner 28 --- +-271,438,-614 +606,-544,732 +583,536,639 +-628,508,883 +545,-567,809 +31,-106,-47 +576,-390,-493 +656,587,-712 +-338,586,-579 +-244,-704,-616 +521,-379,-649 +522,-473,796 +646,363,-649 +583,-498,-630 +-263,-648,-838 +-493,421,882 +-236,-526,-715 +-334,526,-761 +687,584,563 +-345,-694,791 +180,17,17 +-369,-661,892 +-423,474,915 +-361,-517,876 +609,577,-682 +571,594,661 diff --git a/day19/main.c b/day19/main.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <time.h> +#include <string.h> + +void puzzle(const char *filename, long long *res1, long long *res2); + +int main(int argc, char *argv[]) { + printf("Advent of Code: day 19\n"); + double time_start = clock(); + + if (argc <= 1) { + printf("Usage: %s inputfile.txt\n", argv[0]); + return -1; + } + + const char *filename = argv[1]; + long long counter1 = -1; + long long counter2 = -1; + + puzzle(filename, &counter1, &counter2); + + printf("Puzzle #1: %lld\n", counter1); + printf("Puzzle #2: %lld\n", counter2); + + double elapsed = clock() - time_start; + printf("Elapsed: %f\n", elapsed / CLOCKS_PER_SEC); + + return 0; +} diff --git a/day19/puzzle.c b/day19/puzzle.c @@ -0,0 +1,158 @@ +#define _DEFAULT_SOURCE + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <strings.h> +#include <assert.h> +#include <ctype.h> + +#include "util.h" + +#define STR_LEN 1024 +#define BEACONS_VARIANTS 24 + +struct beacon_t { + int x, y, z; +}; + +struct scanner_t { + int id; + struct beacon_t *beacons[BEACONS_VARIANTS]; + int beacons_count; +}; + +struct scanner_t parse_scanner(const char *str); +void parse_beacon(struct scanner_t *scanner, const char *str); +void rotate_beacon(struct beacon_t *orig, struct beacon_t *beacon, int variant); + +void puzzle(const char *filename, long long *result1, long long *result2) { + FILE *infile = fopen(filename, "r"); + if (infile == NULL) { + fprintf(stderr, "fopen() error: %s\n", strerror(errno)); + return; + } + + char buf[STR_LEN] = {0}; + unsigned int line_num = 0; + + *result1 = 0; + *result2 = 0; + + struct array_t scanners = { .data = NULL }; + array_init(&scanners, sizeof(struct scanner_t), 10); + + while (fgets(buf, STR_LEN, infile) != NULL) { + char *scan = NULL; + if ((scan = strstr(buf, "scanner")) != NULL) { + if (scanners.count >= scanners.cap) array_expand(&scanners); + struct scanner_t *scanners_data = scanners.data; + scanners_data[scanners.count++] = parse_scanner(scan); + } else if (strlen(buf) > 2) { + struct scanner_t *scanner = &((struct scanner_t *)scanners.data)[scanners.count-1]; + parse_beacon(scanner, buf); + } + ++line_num; + bzero(buf, STR_LEN); + } + + struct scanner_t *data = scanners.data; + for (size_t i = 0; i < scanners.count; ++i) { + //printf("scanner %2d, beacons %2d:\n", data[i].id, data[i].beacons_count); + for (int j = 1; j < BEACONS_VARIANTS; ++j) { + data[i].beacons[j] = calloc(data[i].beacons_count, sizeof(struct beacon_t)); + } + for (int k = 0; k < data[i].beacons_count; ++k) { + for (int j = 0; j < BEACONS_VARIANTS; ++j) { + rotate_beacon(&data[i].beacons[0][k], &data[i].beacons[j][k], j); + /*printf("scanner %2d beacon %2d rotation variant %2d: %5d %5d %5d\n", + data[i].id, k, j, + data[i].beacons[j][k].x, + data[i].beacons[j][k].y, + data[i].beacons[j][k].z);*/ + } + } + } + + // mutiny! ignoring feof/ferror. + fclose(infile); +} + +void rotate_beacon(struct beacon_t *orig, struct beacon_t *beacon, int variant) { + assert(orig != NULL); + assert(beacon != NULL); + assert(variant >= 0 && variant < BEACONS_VARIANTS); + switch (variant) { + // case 0: beacon->x = orig->x; beacon->y = orig->y; beacon->z = orig->z; return; + case 1: beacon->x = orig->x; beacon->y = -orig->z; beacon->z = orig->y; return; + case 2: beacon->x = orig->x; beacon->y = -orig->y; beacon->z = -orig->z; return; + case 3: beacon->x = orig->x; beacon->y = orig->z; beacon->z = -orig->y; return; + + case 4: beacon->x = -orig->y; beacon->y = orig->x; beacon->z = orig->z; return; + case 5: beacon->x = orig->z; beacon->y = orig->x; beacon->z = orig->y; return; + case 6: beacon->x = orig->y; beacon->y = orig->x; beacon->z = -orig->z; return; + case 7: beacon->x = -orig->z; beacon->y = orig->x; beacon->z = -orig->y; return; + + case 8: beacon->x = -orig->x; beacon->y = -orig->y; beacon->z = orig->z; return; + case 9: beacon->x = -orig->x; beacon->y = -orig->z; beacon->z = -orig->y; return; + case 10: beacon->x = -orig->x; beacon->y = orig->y; beacon->z = -orig->z; return; + case 11: beacon->x = -orig->x; beacon->y = orig->z; beacon->z = orig->y; return; + + case 12: beacon->x = orig->y; beacon->y = -orig->x; beacon->z = orig->z; return; + case 13: beacon->x = orig->z; beacon->y = -orig->x; beacon->z = -orig->y; return; + case 14: beacon->x = -orig->y; beacon->y = -orig->x; beacon->z = -orig->z; return; + case 15: beacon->x = -orig->z; beacon->y = -orig->x; beacon->z = orig->y; return; + + case 16: beacon->x = -orig->z; beacon->y = orig->y; beacon->z = orig->x; return; + case 17: beacon->x = orig->y; beacon->y = orig->z; beacon->z = orig->x; return; + case 18: beacon->x = orig->z; beacon->y = -orig->y; beacon->z = orig->x; return; + case 19: beacon->x = -orig->y; beacon->y = -orig->z; beacon->z = orig->x; return; + + case 20: beacon->x = -orig->z; beacon->y = -orig->y; beacon->z = -orig->x; return; + case 21: beacon->x = -orig->y; beacon->y = orig->z; beacon->z = -orig->x; return; + case 22: beacon->x = orig->z; beacon->y = orig->y; beacon->z = -orig->x; return; + case 23: beacon->x = orig->y; beacon->y = -orig->z; beacon->z = -orig->x; return; + } +} + +void parse_beacon(struct scanner_t *scanner, const char *str) { + assert(scanner != NULL); + assert(str != NULL); + struct array_t coordinates = { .data=NULL }; + array_init(&coordinates, sizeof(int), 3); + parse_numbers_array(&coordinates, str, ","); + assert(coordinates.count == 3); + int *coordinates_data = coordinates.data; + scanner->beacons_count++; + scanner->beacons[0] = realloc(scanner->beacons[0], scanner->beacons_count * sizeof(struct beacon_t)); + assert(scanner->beacons[0] != NULL); + + scanner->beacons[0][scanner->beacons_count-1] = (struct beacon_t){ + .x = coordinates_data[0], + .y = coordinates_data[1], + .z = coordinates_data[2] + }; +} + +struct scanner_t parse_scanner(const char *str) { + assert(str != NULL); + struct scanner_t scanner = {0}; + char *tmp = strndup(str, STR_LEN); + char *token = NULL; + assert(tmp != NULL); + int scanner_id = -1; + while ((token = strsep(&tmp, "- \n")) != NULL) { + if (*token == '\0') continue; + if (isdigit(token[0])) { + //printf("token=%s\n", token); + scanner_id = atoi(token); + break; + } + } + assert(scanner_id != -1); + //printf("scanner_id=%d\n", scanner_id); + scanner.id = scanner_id; + return scanner; +} + diff --git a/day19/test.txt b/day19/test.txt @@ -0,0 +1,136 @@ +--- scanner 0 --- +404,-588,-901 +528,-643,409 +-838,591,734 +390,-675,-793 +-537,-823,-458 +-485,-357,347 +-345,-311,381 +-661,-816,-575 +-876,649,763 +-618,-824,-621 +553,345,-567 +474,580,667 +-447,-329,318 +-584,868,-557 +544,-627,-890 +564,392,-477 +455,729,728 +-892,524,684 +-689,845,-530 +423,-701,434 +7,-33,-71 +630,319,-379 +443,580,662 +-789,900,-551 +459,-707,401 + +--- scanner 1 --- +686,422,578 +605,423,415 +515,917,-361 +-336,658,858 +95,138,22 +-476,619,847 +-340,-569,-846 +567,-361,727 +-460,603,-452 +669,-402,600 +729,430,532 +-500,-761,534 +-322,571,750 +-466,-666,-811 +-429,-592,574 +-355,545,-477 +703,-491,-529 +-328,-685,520 +413,935,-424 +-391,539,-444 +586,-435,557 +-364,-763,-893 +807,-499,-711 +755,-354,-619 +553,889,-390 + +--- scanner 2 --- +649,640,665 +682,-795,504 +-784,533,-524 +-644,584,-595 +-588,-843,648 +-30,6,44 +-674,560,763 +500,723,-460 +609,671,-379 +-555,-800,653 +-675,-892,-343 +697,-426,-610 +578,704,681 +493,664,-388 +-671,-858,530 +-667,343,800 +571,-461,-707 +-138,-166,112 +-889,563,-600 +646,-828,498 +640,759,510 +-630,509,768 +-681,-892,-333 +673,-379,-804 +-742,-814,-386 +577,-820,562 + +--- scanner 3 --- +-589,542,597 +605,-692,669 +-500,565,-823 +-660,373,557 +-458,-679,-417 +-488,449,543 +-626,468,-788 +338,-750,-386 +528,-832,-391 +562,-778,733 +-938,-730,414 +543,643,-506 +-524,371,-870 +407,773,750 +-104,29,83 +378,-903,-323 +-778,-728,485 +426,699,580 +-438,-605,-362 +-469,-447,-387 +509,732,623 +647,635,-688 +-868,-804,481 +614,-800,639 +595,780,-596 + +--- scanner 4 --- +727,592,562 +-293,-554,779 +441,611,-461 +-714,465,-776 +-743,427,-804 +-660,-479,-426 +832,-632,460 +927,-485,-438 +408,393,-506 +466,436,-512 +110,16,151 +-258,-428,682 +-393,719,612 +-211,-452,876 +808,-476,-593 +-575,615,604 +-485,667,467 +-680,325,-822 +-627,-443,-432 +872,-547,-609 +833,512,582 +807,604,487 +839,-516,451 +891,-625,532 +-652,-548,-490 +30,-46,-14