diff --git a/19/CMakeLists.txt b/19/CMakeLists.txt new file mode 100644 index 0000000..d7f51a9 --- /dev/null +++ b/19/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.10) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +enable_language(CXX) + +project(AoC19) + +if(APPLE) + include_directories(/opt/homebrew/include) + link_directories(/opt/homebrew/lib) +endif() + +if(WIN32) + add_executable(${CMAKE_PROJECT_NAME} WIN32) +else() + add_executable(${CMAKE_PROJECT_NAME}) +endif() + +target_sources(${CMAKE_PROJECT_NAME} + PRIVATE main.cpp + ) diff --git a/19/input b/19/input new file mode 100644 index 0000000..c72e300 --- /dev/null +++ b/19/input @@ -0,0 +1,1056 @@ +--- scanner 0 --- +-676,-433,-499 +496,-527,-542 +560,-496,-544 +-130,59,-171 +777,740,449 +-595,482,-515 +-462,376,-485 +446,-541,-444 +816,578,360 +693,-355,497 +-547,886,246 +783,-339,523 +-580,717,270 +537,724,-439 +585,738,-546 +693,-360,344 +780,596,356 +519,766,-677 +-758,-658,596 +-633,-661,463 +-604,-681,687 +-485,838,297 +-20,94,11 +-680,-565,-520 +-609,-430,-454 +-446,478,-577 + +--- scanner 1 --- +377,828,-479 +10,-83,131 +-61,70,6 +753,-464,827 +-692,-838,-494 +-486,726,689 +585,-439,837 +-696,-786,-313 +356,764,-472 +-763,-560,688 +579,376,423 +-854,-467,690 +-702,-868,-263 +756,-775,-429 +377,410,461 +-700,519,-495 +626,-747,-534 +-829,-450,771 +331,756,-377 +665,-746,-373 +584,335,465 +-527,570,-526 +479,-473,821 +-675,727,755 +-639,388,-521 +-627,838,722 + +--- scanner 2 --- +642,-368,607 +-472,-381,667 +-888,337,858 +-784,-648,-513 +602,854,701 +48,-36,-26 +-822,335,834 +-42,106,111 +685,-341,677 +455,689,-605 +-918,484,-528 +821,-485,-443 +479,717,-505 +-876,-543,-474 +-804,426,-472 +-662,482,-542 +780,-538,-394 +503,-313,632 +-544,-569,675 +-476,-530,793 +604,785,584 +508,769,724 +-921,-682,-568 +-854,463,785 +381,727,-551 +667,-545,-409 + +--- scanner 3 --- +614,-405,619 +-306,-511,766 +460,-384,665 +518,720,-558 +833,-619,-478 +529,734,632 +-418,758,621 +616,-599,-481 +-478,-837,-281 +-320,-428,683 +555,693,439 +736,-688,-402 +-630,562,-613 +3,77,84 +-305,773,614 +642,-350,722 +-445,-822,-317 +510,742,-525 +-292,-623,672 +-606,662,-641 +520,606,610 +-264,724,561 +-704,616,-651 +572,703,-676 +-542,-821,-341 + +--- scanner 4 --- +505,-678,285 +-469,-711,-721 +765,-709,-746 +-475,520,225 +15,-36,-79 +-569,659,270 +-500,-713,-657 +793,-578,-680 +-583,758,-756 +-403,-801,448 +-595,775,-514 +847,852,-493 +-530,554,362 +-538,-735,519 +-514,-811,-607 +179,106,-149 +-406,-756,515 +745,492,390 +751,-816,-698 +403,-770,301 +639,476,356 +471,486,381 +-612,766,-781 +640,848,-418 +645,890,-459 +509,-740,434 + +--- scanner 5 --- +668,735,724 +-867,783,567 +-906,-544,816 +563,806,825 +409,-872,677 +370,-539,-533 +-405,-561,-311 +592,766,618 +-509,778,-739 +-383,682,-816 +-470,-436,-372 +362,-776,567 +607,618,-692 +-909,779,533 +597,729,-812 +-888,794,719 +-917,-605,613 +-912,-733,757 +-552,-553,-290 +-86,-72,41 +642,556,-745 +487,-663,-485 +-559,717,-730 +501,-788,569 +370,-695,-476 + +--- scanner 6 --- +-782,-545,375 +-34,191,-34 +562,-760,577 +679,-768,412 +541,845,-631 +-926,533,603 +650,-736,478 +317,-756,-621 +-105,18,-135 +-835,992,-471 +-908,503,560 +-702,-559,544 +447,491,471 +-849,490,601 +-786,864,-439 +471,901,-489 +-859,990,-405 +280,-675,-697 +-519,-366,-802 +-495,-225,-744 +527,843,-375 +446,482,595 +-505,-228,-802 +563,438,498 +-749,-488,391 +477,-661,-657 + +--- scanner 7 --- +-366,-597,586 +-409,-721,560 +609,-623,-569 +851,613,-765 +743,-654,717 +-356,537,-649 +744,-484,645 +569,-539,-504 +1,-128,77 +-321,755,292 +95,-2,-28 +-411,-889,-670 +650,-545,760 +461,732,754 +760,459,-742 +410,670,802 +-413,771,394 +-486,-840,-542 +608,-466,-595 +-351,622,398 +627,584,-745 +-290,608,-514 +434,637,633 +-458,-694,565 +-335,541,-517 +-318,-893,-552 + +--- scanner 8 --- +567,-628,-719 +687,755,-587 +-524,741,318 +512,-349,572 +606,-434,549 +851,554,626 +-519,-699,-774 +-524,-813,-600 +730,573,596 +808,673,-560 +-465,475,-710 +-402,422,-687 +439,-565,-793 +-658,795,390 +-770,-619,512 +-821,-540,441 +-525,-834,-825 +510,-418,689 +-729,-696,483 +737,518,736 +30,112,-34 +-102,-56,36 +-712,797,354 +806,650,-644 +382,-628,-672 +-617,439,-655 + +--- scanner 9 --- +-304,561,846 +720,938,762 +631,-649,-565 +623,-492,724 +602,934,669 +-296,566,732 +-638,-455,383 +-643,-457,438 +0,49,-22 +592,-741,-670 +-675,-341,-683 +730,885,594 +-549,-337,-832 +527,802,-668 +-669,-653,369 +755,-496,745 +758,-563,790 +-514,638,-900 +561,956,-728 +686,-786,-531 +-489,777,-845 +-557,-409,-644 +-312,484,757 +512,915,-675 +-559,820,-872 + +--- scanner 10 --- +739,-755,-453 +101,-126,158 +821,497,-816 +863,502,-775 +-596,-592,819 +793,-765,485 +693,401,655 +-506,-614,914 +-838,-657,-627 +736,482,575 +627,420,457 +-745,413,933 +884,-687,509 +748,-738,-720 +-738,-612,-513 +-736,622,-488 +731,-726,-478 +714,-615,525 +-843,608,-536 +-605,650,-554 +-730,319,944 +-807,261,920 +825,704,-741 +-740,-643,-577 +16,-44,21 +-464,-457,823 + +--- scanner 11 --- +-588,507,-880 +-423,-345,577 +-600,309,552 +526,-515,-421 +474,672,264 +580,711,304 +-435,531,-812 +-66,-110,-161 +647,404,-543 +480,330,-506 +-765,-596,-817 +513,-679,-486 +594,-865,485 +-589,-344,671 +-742,-644,-870 +-543,567,-791 +-675,-538,-786 +574,-834,461 +-518,430,525 +656,-602,-423 +45,-55,-18 +617,690,360 +-623,311,468 +-582,-359,497 +580,-628,530 +542,402,-515 + +--- scanner 12 --- +-550,-616,376 +457,-816,717 +594,-638,-771 +-557,-642,301 +-558,514,384 +649,-611,-764 +695,-771,-745 +-622,545,386 +653,-790,648 +-2,-95,-168 +-755,681,-502 +655,-785,703 +542,360,-845 +-508,500,286 +-421,-607,412 +-643,-562,-717 +-702,-589,-727 +99,-133,4 +-630,-718,-821 +-845,702,-681 +-755,686,-655 +618,476,362 +696,383,-754 +614,608,341 +547,611,296 +571,380,-621 + +--- scanner 13 --- +542,-821,-720 +857,-452,668 +-508,603,651 +-271,812,-483 +434,-751,-706 +626,-768,-768 +-377,872,-453 +-275,797,-538 +867,857,-654 +-276,-790,-658 +820,845,-517 +555,701,726 +-7,-86,10 +-325,-303,307 +-258,-760,-701 +941,-518,560 +927,-376,571 +-335,-803,-699 +828,884,-544 +-398,-429,341 +672,745,655 +-516,415,723 +-349,-301,373 +181,96,-72 +-390,564,719 +755,647,734 + +--- scanner 14 --- +968,-689,515 +470,615,372 +636,521,371 +-603,-581,503 +971,-699,573 +-533,662,655 +747,-516,-360 +139,-18,17 +-527,731,861 +830,-675,515 +629,695,386 +69,165,149 +565,667,-403 +-533,888,-684 +670,724,-284 +802,-656,-353 +-547,707,892 +-427,928,-645 +-780,-492,-422 +766,-546,-239 +-798,-528,486 +-776,-612,-251 +-570,832,-663 +-795,-585,-497 +647,640,-282 +-611,-488,447 + +--- scanner 15 --- +583,713,-552 +458,547,852 +655,-492,680 +368,573,868 +656,-409,-476 +705,-394,666 +-383,-790,-509 +-560,346,-311 +570,609,-396 +591,-494,711 +792,-520,-441 +663,-484,-450 +-417,460,580 +-384,415,749 +-390,-667,-540 +-61,-112,54 +511,619,-423 +-692,383,-315 +-298,367,661 +458,394,847 +-548,270,-278 +-741,-636,731 +-836,-726,709 +46,61,104 +-425,-619,-517 +-745,-768,866 + +--- scanner 16 --- +-582,-327,-846 +905,876,813 +-483,-630,605 +574,810,-769 +797,-760,688 +612,840,-757 +609,-433,-734 +720,-765,748 +-723,624,-933 +-735,-332,-846 +-833,914,394 +-809,745,427 +792,-762,584 +-616,-633,480 +-630,768,-930 +-719,606,-969 +548,-556,-638 +-556,-311,-936 +-392,-653,451 +667,-508,-621 +833,911,802 +-782,866,332 +899,791,751 +69,129,-154 +437,751,-791 + +--- scanner 17 --- +843,599,405 +-809,-488,331 +-854,650,-358 +-808,623,-434 +748,644,496 +607,-468,-847 +534,-221,443 +-440,-717,-526 +-683,584,-400 +584,-642,-902 +515,789,-735 +-864,-278,324 +-129,4,-41 +-806,-289,267 +-732,597,390 +568,-291,509 +-457,-620,-638 +604,-414,-919 +-715,500,498 +773,612,394 +593,924,-781 +13,97,37 +592,-219,308 +622,808,-782 +-744,588,578 +-374,-728,-714 + +--- scanner 18 --- +758,-562,-685 +-531,-794,741 +127,9,-157 +-394,339,-708 +-352,306,-715 +-690,713,531 +-539,-941,720 +667,602,472 +-51,-48,-1 +704,-963,215 +830,258,-876 +746,405,-863 +-801,636,474 +-414,-876,659 +-537,-922,-485 +628,-536,-584 +739,-787,301 +674,-431,-657 +636,551,645 +876,443,-864 +641,733,533 +-735,-849,-497 +-820,682,627 +24,-189,-93 +-547,-849,-398 +800,-837,292 +-281,318,-677 + +--- scanner 19 --- +-564,-230,433 +555,837,-898 +401,705,759 +891,-476,341 +561,669,664 +588,-446,-607 +-618,-569,-470 +-501,-571,-539 +49,126,-174 +823,-548,353 +-641,-475,-555 +-24,-23,-90 +-454,-344,397 +611,951,-840 +634,-348,-597 +-775,921,-987 +-702,887,-861 +733,-402,391 +-435,524,369 +-594,-351,452 +-421,524,231 +625,695,-842 +688,-422,-635 +-751,855,-989 +364,629,678 +-411,582,228 + +--- scanner 20 --- +693,516,-782 +659,450,-852 +-321,-542,711 +-249,-707,-345 +-312,-541,587 +906,-745,704 +732,-840,-605 +-363,831,579 +-315,-519,-356 +729,-912,-659 +-332,-631,-453 +27,-37,79 +717,315,-854 +162,-117,-23 +658,654,791 +-681,274,-784 +-268,-658,684 +-739,331,-795 +-320,754,581 +729,607,638 +-684,338,-585 +-333,813,622 +709,-895,-431 +930,-657,605 +715,687,807 +938,-774,770 + +--- scanner 21 --- +-791,-706,-625 +552,-348,697 +436,438,-576 +-49,86,132 +484,716,550 +658,-377,-702 +411,446,-671 +518,583,461 +-877,-882,-590 +-372,-860,653 +-433,-795,528 +-351,-713,645 +113,-28,35 +475,731,403 +755,-359,636 +-618,717,915 +506,-398,-691 +-559,814,-527 +-447,632,-522 +457,374,-569 +-561,794,804 +-620,683,-553 +472,-356,-687 +696,-353,825 +-465,763,934 +-800,-756,-686 + +--- scanner 22 --- +485,-687,713 +-601,613,888 +847,-748,-334 +-509,-449,-668 +-828,645,-417 +-742,-526,814 +-649,580,777 +779,508,-414 +-803,564,-400 +505,514,-423 +611,498,-389 +499,-794,665 +-462,-378,-682 +610,870,385 +-737,647,-510 +-33,148,157 +-51,-18,1 +605,912,426 +-731,-716,724 +745,-656,-318 +-834,-606,774 +403,874,460 +457,-556,647 +-460,646,776 +741,-718,-467 +-434,-434,-772 + +--- scanner 23 --- +452,-737,702 +-410,-410,-834 +638,-813,694 +760,826,-627 +-399,-316,-819 +345,440,471 +-834,690,795 +-524,595,-685 +753,616,-684 +527,-713,714 +401,-344,-824 +323,521,316 +358,-348,-867 +693,717,-611 +-818,618,625 +-858,768,704 +-520,460,-553 +474,-385,-885 +-688,-398,604 +-886,-417,538 +3,-42,-9 +-423,-258,-896 +-429,617,-595 +-95,67,-127 +456,472,432 +-768,-479,647 + +--- scanner 24 --- +780,697,-379 +936,663,567 +497,-664,-506 +-464,-736,630 +-765,519,741 +-778,577,-655 +-764,671,-543 +799,629,622 +12,-75,97 +133,15,-41 +819,-445,663 +-806,466,692 +-366,-746,584 +498,-503,-467 +-592,-376,-869 +805,707,-547 +677,-501,593 +771,691,-653 +933,-503,607 +-750,588,-460 +-625,-412,-854 +-652,526,726 +469,-559,-376 +-440,-708,530 +-528,-436,-853 +891,524,661 + +--- scanner 25 --- +804,592,-664 +822,-814,411 +-789,574,563 +914,-678,-907 +904,-751,-936 +798,-905,489 +-492,-542,707 +-422,-442,-890 +-359,-426,658 +393,485,562 +-381,-581,739 +-376,-428,-831 +484,443,719 +82,46,-146 +-507,484,-631 +900,-700,-781 +839,544,-687 +782,-735,561 +-579,445,-481 +-445,-331,-852 +488,575,584 +-476,372,-496 +-729,501,462 +846,421,-656 +24,-44,11 +-796,517,353 + +--- scanner 26 --- +-634,-479,591 +-674,-465,394 +-51,-67,55 +435,-372,793 +398,-595,-668 +690,666,621 +-598,-384,431 +501,-404,764 +-721,-915,-381 +803,722,496 +414,-652,-635 +-597,576,487 +588,624,-406 +609,602,-366 +567,-581,-588 +-791,659,-499 +-847,667,-592 +600,-380,676 +734,630,526 +-779,-774,-405 +-808,854,-552 +-555,457,529 +-779,-912,-391 +-578,608,616 +610,664,-256 + +--- scanner 27 --- +819,557,797 +748,-478,-469 +-454,-688,-549 +747,-648,-397 +-616,427,534 +654,-539,-354 +846,-866,615 +-687,413,487 +-317,761,-822 +-456,-826,816 +156,-21,133 +831,632,950 +-405,-715,-554 +-610,-822,839 +-362,-591,-528 +-409,777,-720 +-631,323,459 +101,-136,-28 +-601,-885,888 +465,738,-360 +438,643,-481 +923,-763,658 +-347,628,-716 +827,-779,589 +414,676,-312 +870,636,721 + +--- scanner 28 --- +-678,642,-686 +-127,-37,-154 +-830,712,557 +375,-599,421 +-947,-520,-719 +722,788,-846 +504,-353,-854 +-441,656,-731 +-962,-527,-545 +-886,720,464 +-961,-361,-639 +711,864,-765 +-472,636,-755 +-847,907,504 +359,-542,388 +-21,41,-7 +-818,-859,665 +527,-571,-898 +637,888,-813 +531,-444,-927 +570,655,690 +528,-528,468 +-760,-862,675 +549,677,650 +571,717,800 +-818,-721,651 + +--- scanner 29 --- +-603,-822,-896 +-601,-865,-819 +-385,750,-883 +821,353,-638 +471,-363,-805 +405,-365,-829 +-595,-873,476 +-498,-917,593 +-560,-852,-878 +375,-512,557 +-609,416,489 +509,620,542 +631,-510,571 +-440,735,-879 +372,654,597 +489,-454,501 +-367,399,477 +417,515,503 +-355,810,-777 +439,-491,-756 +-471,-864,477 +817,502,-764 +-634,408,460 +-24,-33,-23 +749,541,-646 + +--- scanner 30 --- +-299,646,541 +668,-881,662 +419,314,-781 +-464,674,-679 +-436,612,596 +-766,-576,-402 +385,276,-579 +76,-162,-38 +544,-872,661 +344,318,-574 +440,391,661 +-737,-520,-493 +837,-626,-781 +773,-619,-802 +682,408,649 +-430,690,-573 +-455,680,-405 +-14,6,-88 +-344,507,635 +571,-925,545 +765,-524,-827 +-738,-681,-532 +-416,-462,637 +-558,-518,567 +-302,-520,581 +707,400,678 + +--- scanner 31 --- +402,-411,-885 +571,-444,-861 +525,-768,847 +710,952,-771 +565,774,385 +-427,561,-864 +-431,-739,521 +-707,-530,-480 +-519,-692,601 +476,740,560 +405,-819,781 +450,-349,-834 +506,752,522 +-355,840,543 +-421,-807,621 +692,916,-775 +-860,-455,-468 +-378,680,-740 +-379,948,646 +-444,918,608 +-631,-451,-429 +-9,131,71 +-475,672,-896 +440,-830,853 +548,913,-682 + +--- scanner 32 --- +628,746,783 +-588,884,-667 +694,-818,344 +792,815,-799 +-549,-686,614 +-631,-324,-895 +-654,-387,-842 +622,-679,300 +652,808,-744 +446,-580,-522 +-531,765,-562 +410,-634,-520 +5,6,-88 +721,671,798 +374,-446,-576 +-574,-656,447 +-680,-693,481 +-792,523,430 +748,917,779 +-696,505,573 +-504,886,-644 +-599,-539,-878 +-752,484,536 +736,-657,263 +682,929,-763 + +--- scanner 33 --- +-714,-643,-399 +701,-594,474 +403,722,-579 +-723,-658,-252 +17,40,109 +586,-488,-777 +-692,471,842 +-762,-365,727 +-502,426,-674 +331,776,-576 +72,173,-14 +-443,432,-485 +-646,421,-546 +702,-470,465 +724,473,468 +498,-587,-753 +-787,-373,822 +585,-567,421 +-540,450,853 +-680,-510,-310 +612,570,485 +-726,429,838 +348,548,-556 +469,-653,-785 +-741,-301,720 +536,462,402 + +--- scanner 34 --- +-506,776,673 +-395,600,-245 +-790,-450,589 +-564,-590,-510 +-433,825,733 +-770,-659,-516 +561,-654,583 +462,431,537 +588,657,-248 +-753,-421,694 +-801,-635,-496 +513,564,-263 +342,684,-267 +503,399,494 +-356,560,-432 +-412,653,-440 +35,-10,-12 +517,-575,507 +-802,-305,706 +736,-856,-616 +-44,82,161 +460,381,594 +754,-760,-473 +488,-699,544 +775,-816,-510 +-372,837,735 + +--- scanner 35 --- +515,-353,714 +573,333,-663 +480,389,-655 +362,459,919 +372,401,916 +-548,625,-453 +-870,-726,-557 +476,-611,-847 +-857,-757,-810 +-940,-599,806 +-872,-671,794 +-128,37,-25 +449,-365,-828 +-3,-119,58 +-635,602,-604 +-610,546,483 +569,-536,-832 +386,401,778 +-824,-790,-682 +545,-474,695 +-450,582,388 +-782,-525,756 +-506,583,-572 +405,-467,750 +533,372,-850 +-413,506,469 + +--- scanner 36 --- +-613,-749,-610 +690,735,796 +716,-829,-557 +690,659,864 +-417,485,-398 +576,-845,-461 +-32,-38,111 +599,308,-423 +-451,425,-244 +-482,303,575 +824,-723,870 +-472,-812,592 +-402,478,-285 +-513,-777,-586 +77,-184,194 +-433,-661,530 +693,-914,-463 +640,698,925 +782,-567,778 +669,281,-380 +753,-644,819 +624,253,-562 +-441,496,549 +-455,-694,538 +-559,-787,-684 +-569,474,540 + +--- scanner 37 --- +920,-438,557 +749,-774,-709 +917,-749,-699 +-665,-763,-643 +54,-105,21 +-690,-746,-562 +-528,-375,622 +-673,413,702 +790,-466,662 +836,781,-590 +-714,401,755 +-599,-653,-560 +-765,450,706 +13,64,113 +-243,718,-539 +-372,646,-473 +643,-740,-693 +641,809,-579 +-532,-427,441 +443,559,434 +753,795,-673 +-425,801,-511 +427,623,374 +442,656,410 +-538,-345,391 +829,-467,501 diff --git a/19/main.cpp b/19/main.cpp new file mode 100644 index 0000000..199e3c4 --- /dev/null +++ b/19/main.cpp @@ -0,0 +1,297 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class BeaconPos { +public: + BeaconPos() = delete; + BeaconPos(int x, int y, int z): _x(x), _y(y), _z(z) {} + + int getX() const { + return _x; + } + int getY() const { + return _y; + } + int getZ() const { + return _z; + } + + void setX(int x) { + _x = x; + } + void setY(int y) { + _y = y; + } + void setZ(int z) { + _z = z; + } + + friend std::ostream& operator<<(std::ostream& stream, const BeaconPos& pos) { + stream << pos.getX() << ", " << pos.getY() << ", " << pos.getZ(); + return stream; + } + + friend bool operator==(const BeaconPos &pos1, const BeaconPos &pos2) { + return pos1.getX() == pos2.getX() && pos1.getY() == pos2.getY() && pos1.getZ() == pos2.getZ(); + } + + friend bool operator!=(const BeaconPos &pos1, const BeaconPos &pos2) { + return !(pos1 == pos2); + } + + BeaconPos& operator+=(const BeaconPos& pos2) { + setX(getX() + pos2.getX()); + setY(getY() + pos2.getY()); + setZ(getZ() + pos2.getZ()); + return *this; + } + + friend BeaconPos operator+(BeaconPos pos1, const BeaconPos &pos2) { + pos1 += pos2; + return pos1; + } + + BeaconPos& operator-=(const BeaconPos& pos2) { + setX(getX() - pos2.getX()); + setY(getY() - pos2.getY()); + setZ(getZ() - pos2.getZ()); + return *this; + } + + friend BeaconPos operator-(BeaconPos pos1, const BeaconPos &pos2) { + pos1 -= pos2; + return pos1; + } + +private: + // X possitive - right, negative - left + int _x; + // Y positive - top, negative - bottom + int _y; + // Z positive - further back, negative - closer + int _z; +}; + +static std::vector> transformFunctions = { + [](const BeaconPos &beacon) {return beacon;}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getZ(), beacon.getY(), -beacon.getX());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getX(), beacon.getY(), -beacon.getZ());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getZ(), beacon.getY(), beacon.getX());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getX(), -beacon.getY(), beacon.getZ());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getZ(), -beacon.getY(), -beacon.getX());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getX(), -beacon.getY(), -beacon.getZ());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getZ(), -beacon.getY(), beacon.getX());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getX(), -beacon.getZ(), beacon.getY());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getY(), -beacon.getZ(), -beacon.getX());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getX(), -beacon.getZ(), -beacon.getY());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getY(), -beacon.getZ(), beacon.getX());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getX(), beacon.getZ(), -beacon.getY());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getY(), beacon.getZ(), -beacon.getX());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getX(), beacon.getZ(), beacon.getY());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getY(), beacon.getZ(), beacon.getX());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getZ(), beacon.getX(), beacon.getY());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getY(), beacon.getX(), -beacon.getZ());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getZ(), beacon.getX(), -beacon.getY());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getY(), beacon.getX(), beacon.getZ());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getZ(), -beacon.getX(), beacon.getY());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getY(), -beacon.getX(), beacon.getZ());}, + [](const BeaconPos &beacon) {return BeaconPos(beacon.getZ(), -beacon.getX(), -beacon.getY());}, + [](const BeaconPos &beacon) {return BeaconPos(-beacon.getY(), -beacon.getX(), -beacon.getZ());}, +}; + +class BeaconMap { +public: + BeaconMap() {} + + void addBeacon(int x, int y, int z) { + addBeacon(BeaconPos(x, y, z)); + } + + void addBeacon(BeaconPos beacon) { + _map.push_back(beacon); + } + + void generateRelativeBeacons(bool transform) { + _relative_map.clear(); + _relative_map.emplace_back(); + for(size_t i = 0; i < _map.size(); i++) { + _relative_map.back().push_back(std::vector()); + for(size_t j = 0; j < _map.size(); j++) { + _relative_map.back().back().emplace_back(_map[j].getX() - _map[i].getX(), _map[j].getY() - _map[i].getY(), _map[j].getZ() - _map[i].getZ()); + } + } + if(transform) { + for(size_t i = 1; i < transformFunctions.size(); i++) { + _relative_map.emplace_back(); + for(auto &vec : _relative_map[0]) { + _relative_map.back().emplace_back(); + for(auto &value : vec) { + _relative_map.back().back().push_back(transformFunctions[i](value)); + } + } + } + } + } + + std::pair has12Common(const BeaconMap &other) { + auto otherRelativeBeacons = other._relative_map[0]; + for(size_t transformIndex = 0; transformIndex < _relative_map.size(); transformIndex++) { + for(auto &mybeaconmap : _relative_map[transformIndex]) { + int commonBeacons = 0; + for(auto &otherBeaconMap : otherRelativeBeacons) { + commonBeacons = 0; + for(auto &myBeacon : mybeaconmap) { + if(std::find(otherBeaconMap.begin(), otherBeaconMap.end(), myBeacon) != otherBeaconMap.end()) { + commonBeacons++; + } + } + if(commonBeacons >= 11) { + size_t otherIndex = 0; + size_t myIndex = 0; + for(size_t i = 0; i < otherBeaconMap.size(); i++) { + if(otherBeaconMap[i].getX() == 0 && otherBeaconMap[i].getY() == 0 && otherBeaconMap[i].getZ() == 0) { + otherIndex = i; + break; + } + } + for(size_t i = 0; i < otherBeaconMap.size(); i++) { + if(mybeaconmap[i].getX() == 0 && mybeaconmap[i].getY() == 0 && mybeaconmap[i].getZ() == 0) { + myIndex = i; + break; + } + } + return {other._map[otherIndex] - transformFunctions[transformIndex](_map[myIndex]), transformIndex}; + } + } + } + } + return {{0,0,0}, -1}; + } + + size_t getNumberOfBeacons() const { + return _map.size(); + } + + void copyBeacons(const BeaconMap &map, BeaconPos relativePosition) { + for(const auto &beacon : map._map) { + auto repositioned = beacon + relativePosition; + if(hasBeacon(repositioned)) { + continue; + } else { + addBeacon(repositioned); + } + } + } + + void updateMap(int transformIndex) { + for(auto &beacon : _map) { + beacon = transformFunctions[transformIndex](beacon); + } + } + +private: + bool hasBeacon(BeaconPos beacon) { + return std::find(_map.begin(), _map.end(), beacon) != _map.end(); + } + + std::vector _map; + std::vector>> _relative_map; +}; + +std::vector +getBeaconMaps(const std::string &file_name) { + std::vector beaconMaps{}; + + std::ifstream file(file_name); + std::string str; + while (std::getline(file, str)) { + if(str[1] == '-') { + auto cur = BeaconMap(); + beaconMaps.push_back(cur); + continue; + } + if(str.empty()) { + continue; + } + std::istringstream ss(str); + int x, y, z; + char delimiter; + ss >> x; + ss >> delimiter; + ss >> y; + ss >> delimiter; + ss >> z; + BeaconPos bp(x, y, z); + beaconMaps.back().addBeacon(x, y, z); + } + + beaconMaps[0].generateRelativeBeacons(false); + for(size_t i = 1; i < beaconMaps.size(); i++) { + beaconMaps[i].generateRelativeBeacons(true); + } + + return beaconMaps; +} + +std::pair> part1(std::vector maps) { + std::vector scanners{}; + std::deque mapIndexes{}; + for(size_t i = 1; i < maps.size(); i++) { + mapIndexes.push_back(i); + } + while(!mapIndexes.empty()) { + auto prevCount = mapIndexes.size(); + for(auto iterator = mapIndexes.begin(); iterator != mapIndexes.end(); iterator++) { + auto result = maps[*iterator].has12Common(maps[0]); + if(result.second >= 0) { + scanners.push_back(result.first); + maps[*iterator].updateMap(result.second); + maps[0].copyBeacons(maps[*iterator], result.first); + mapIndexes.erase(iterator); + maps[0].generateRelativeBeacons(false); + break; + } + } + if(prevCount == mapIndexes.size()) { + std::cerr << "Cannot find all beacons"; + return {-1, {}}; + } + } + return {maps[0].getNumberOfBeacons(), scanners}; +} + +uint64_t part2(std::vector &scanners) { + size_t maxManhattan = 0; + for(size_t i = 0; i < scanners.size(); i++) { + for(size_t j = i; j < scanners.size(); j++) { + auto diff = scanners[i] - scanners[j]; + auto manhattan = abs(diff.getX()) + abs(diff.getY()) + abs(diff.getZ()); + if(manhattan > maxManhattan) { + maxManhattan = manhattan; + } + } + } + return maxManhattan; +} + +int main(int argc, char **argv) { + if (argc < 2) { + std::cerr << "You must provide input file!" << std::endl; + return 1; + } + auto maps = getBeaconMaps(argv[1]); + auto result = part1(maps); + std::cout << "The number of beacons mapped in the ocean is \033[91;1m" << result.first + << "\033[0m." << std::endl; + std::cout + << "The largest manhattan distance between any 2 scanners is \033[91;1m" + << part2(result.second) << "\033[0m." << std::endl; + return 0; +}