From c47649e1fb43948738499c17d43eaedab97a0b78 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 8 Dec 2025 10:06:53 +0000 Subject: [PATCH] feat: Completed day 8 and fixed up clippy warnings in code. --- input/day08 | 1000 ++++++++++++++++++++++++++++++++++++++++ input/day08_test1 | 20 + input/day08_test2 | 20 + src/main.rs | 2 +- src/solutions/day06.rs | 17 +- src/solutions/day07.rs | 2 +- src/solutions/day08.rs | 221 ++++++++- 7 files changed, 1263 insertions(+), 19 deletions(-) diff --git a/input/day08 b/input/day08 index e69de29..6fee5f1 100644 --- a/input/day08 +++ b/input/day08 @@ -0,0 +1,1000 @@ +7864,52073,52717 +73987,76815,39348 +43996,44895,79963 +7169,63241,34744 +53123,11724,7522 +35332,63408,63777 +64302,90553,31228 +78558,11653,58116 +19721,19975,26208 +20463,36421,30631 +42696,51226,5228 +19147,59694,71410 +93312,4210,12931 +98064,72681,54700 +83447,84305,72621 +82894,87043,23953 +33118,35441,12625 +86827,3985,28375 +69953,15831,86527 +8268,46331,47385 +78078,13736,2162 +69810,31981,8383 +97307,55880,71192 +58094,94443,56480 +10522,8871,38082 +21193,25893,64534 +34945,82228,17430 +71287,34390,52805 +12517,44664,18659 +16370,44404,56800 +98889,24485,77986 +72613,81759,48482 +76743,43473,26034 +1700,60792,48850 +71430,38111,75794 +9430,62395,51200 +13977,69951,45579 +19885,22735,83939 +25854,41192,31574 +94291,76249,31084 +86175,38850,11744 +83548,18190,65589 +25517,35836,3128 +91348,14389,65735 +76276,6359,73362 +30373,30550,15692 +48417,69413,11138 +21510,3217,27132 +33001,16311,75838 +75150,69821,58735 +92823,32042,20827 +97637,32041,8205 +36788,56734,15555 +48912,42896,77002 +96746,89078,6611 +74982,30457,67615 +68035,26143,74371 +14312,41952,39424 +28103,18654,60153 +94121,32346,7966 +3666,57360,3683 +5663,63434,59883 +95668,52284,9127 +51735,48810,59576 +66064,86201,11256 +27229,34746,40159 +89165,93388,3600 +98439,76657,62892 +29105,37199,46349 +74876,96411,91304 +74255,36242,44290 +89826,69230,76100 +89556,74744,64278 +24201,24698,65400 +62590,3164,77971 +6093,67813,58784 +14999,99129,39000 +57460,55060,19796 +14425,16801,61885 +73705,33648,30495 +18575,58310,38094 +16594,87886,47642 +51217,88404,4067 +28158,79891,87340 +9732,70814,58954 +59397,93281,53692 +96132,22654,58640 +25808,684,97613 +62514,77766,76708 +29488,35385,11902 +82817,60268,94264 +63291,49890,35872 +99096,44851,75298 +1445,74448,82556 +87085,26477,45501 +80117,77917,55553 +36965,42276,23884 +33781,77684,79796 +25622,56195,97609 +81413,82158,24373 +10404,34192,45177 +12075,40118,96182 +12102,84895,77648 +17869,29931,40598 +78536,91989,72750 +30191,32327,6945 +69491,41915,22207 +35653,34908,27388 +36882,9535,56718 +81442,12266,82868 +21335,54026,44054 +64426,95741,37244 +81934,85675,95624 +85697,58205,44452 +51589,60201,16721 +42626,73429,39312 +49284,60490,14551 +89474,55288,40331 +57518,81731,63913 +99595,503,63567 +76846,49281,91590 +50371,41068,46539 +72256,68143,66776 +79078,86874,22293 +93412,31817,46318 +65774,38590,68655 +242,11379,59613 +21221,44520,59906 +17411,85573,32872 +69374,67793,12118 +60595,55611,57787 +35167,74523,15896 +75815,29837,38053 +93035,77021,35065 +39957,24958,70146 +23888,97604,46002 +34043,93567,31370 +51105,11832,48229 +9901,97810,73475 +41347,13425,86972 +15467,72380,82497 +29060,84679,5135 +6016,5883,35015 +10914,18734,78199 +96372,79322,83904 +58027,8967,77693 +10588,64488,38839 +46839,22140,56869 +86557,68221,47616 +95212,57816,24160 +41459,13584,16620 +48515,59709,14233 +56800,57009,88844 +51652,31153,82215 +62682,87322,77812 +73081,34996,61202 +96778,58905,42587 +61271,4613,5995 +82672,33221,39020 +96153,97191,78341 +86888,40788,57696 +47484,66682,75368 +51181,75427,39102 +95351,97872,74827 +36605,37248,75409 +48148,63518,44539 +64159,94382,2687 +6508,69965,14106 +83338,14072,73109 +19395,87293,37507 +15761,66656,48905 +19900,89980,71499 +98044,72490,60080 +70835,97650,5356 +27820,42608,88464 +82411,77722,16400 +6732,33179,69443 +36008,27064,16622 +41320,9511,76243 +22465,66426,83845 +96213,22196,41896 +54156,20431,81561 +72048,96862,89470 +6276,23904,47226 +24521,77146,33605 +14063,14065,5496 +37719,86963,45570 +96968,3573,84913 +33812,95454,66994 +73585,97176,9171 +28815,34801,7014 +20131,58271,62946 +59924,94637,42367 +35578,83713,46241 +65901,19391,2140 +30469,13280,27436 +27215,50795,26616 +2336,78805,32462 +6585,15709,15836 +31405,94931,12586 +439,22417,67651 +96823,62655,88404 +3152,19065,28244 +72085,73060,51298 +63339,65441,57840 +84355,57088,89757 +83524,78474,45131 +50883,48402,8344 +93585,57782,93531 +37662,43711,36293 +22838,13270,23049 +99684,97036,3398 +78863,139,80716 +592,86092,54132 +78905,38730,34750 +35030,44090,20358 +53355,59610,97733 +39763,76076,65012 +93321,7127,95114 +8500,47278,7111 +47566,5749,96692 +53009,91482,12119 +70917,55516,10867 +4949,20195,8004 +18425,29351,56412 +81541,70409,62958 +77040,20590,75037 +50640,56674,71081 +27698,74164,65183 +57379,88282,96400 +66363,43381,82154 +48723,45574,69664 +42845,84333,33786 +96304,65838,71073 +33852,6811,35807 +43625,2977,38791 +59276,26815,13841 +11710,48505,5728 +61726,70771,86074 +82739,32497,71867 +61251,81144,17944 +98222,25139,36155 +2819,41227,10515 +39603,38954,51640 +11040,59122,23781 +92448,2727,46996 +66120,9877,15435 +50804,24478,7115 +54010,76657,60693 +18127,15459,76877 +53444,62205,20992 +4811,45824,28762 +58580,18767,63505 +25226,24228,20448 +84413,86799,27673 +77480,87541,27441 +42854,28412,72796 +27991,94932,76873 +61171,44977,76749 +1148,36204,56793 +28732,89435,24669 +80901,30777,39344 +83548,23970,83618 +64010,28271,7903 +74618,34610,16263 +29119,60428,69345 +72613,826,71266 +88362,88781,32100 +70823,79966,39105 +61059,47389,30273 +86678,69182,78552 +21593,3902,10666 +99620,96879,22589 +34649,18369,62146 +83452,13148,92216 +76414,49726,6666 +12742,81408,29684 +59849,3611,99402 +58148,67431,16401 +77434,78480,46275 +22031,56269,49881 +91909,19215,71099 +56412,50987,73680 +52964,85596,56925 +29016,51467,67556 +21206,90896,73530 +57735,52933,37749 +64174,14767,63920 +92234,4570,72805 +64485,46656,82573 +57902,39035,51635 +90741,19048,22239 +89206,17171,75284 +51973,79604,32672 +54740,19271,44108 +12662,74057,47144 +25773,9113,77101 +27541,74950,53918 +90138,10840,39256 +3691,22127,46553 +67917,52309,29091 +2345,90905,43251 +1596,65548,64473 +12018,25526,45457 +69834,9600,5118 +23027,2062,82319 +40527,29092,15559 +69345,82260,2963 +82570,86296,74115 +85285,36601,77578 +72654,15761,85706 +79023,42925,35671 +96612,50301,70405 +73809,32382,29017 +62004,23368,19865 +67767,93251,86455 +81863,35573,74084 +76234,79373,66361 +76596,67261,15630 +91580,89649,79568 +82976,121,27876 +67615,20795,76515 +36807,71583,65380 +4707,57682,25082 +36391,17525,71208 +13374,27451,76393 +50655,42300,66976 +87885,63184,26590 +38001,86568,51343 +92839,99711,21545 +61758,36656,27819 +88257,18086,84081 +83458,10111,33361 +7146,60924,35362 +41402,94024,41419 +14332,86838,1780 +63771,27576,32482 +38860,92449,65974 +25722,64531,29603 +14355,62065,21214 +50434,57985,13776 +65452,92173,39851 +70323,55162,55944 +20908,460,20086 +80739,87392,74326 +63423,69925,61754 +29231,53698,26541 +86851,71252,94584 +25235,97882,43478 +48090,89887,81188 +72714,13792,88631 +52891,12442,78356 +16539,9448,8385 +79000,33130,94161 +22576,19725,12245 +52700,5602,26730 +58680,82370,18600 +80485,45088,6656 +85659,56185,27699 +72178,32881,96020 +46727,90108,17268 +18680,1230,50242 +11812,1075,85667 +71924,32921,90497 +71393,21811,33463 +82673,28380,902 +44933,31625,86254 +60392,12117,4441 +87368,52662,94446 +10254,54344,16310 +2106,84624,79233 +65606,28944,3493 +97829,39459,45992 +4710,80307,24033 +31606,68442,95686 +9153,60167,23295 +72652,60651,19341 +14586,21197,79688 +14813,70772,18945 +93026,85372,98406 +31730,44090,44497 +4353,91280,78898 +86989,4188,12994 +90486,20716,10102 +5911,73831,69385 +71159,14805,75643 +28360,48931,83641 +1357,72489,76478 +40924,68432,14314 +56828,63911,509 +89249,85938,71603 +22326,18317,26157 +49486,98583,31057 +39909,4235,20521 +53733,14451,48281 +68003,8809,50674 +94866,99850,79011 +49899,8956,65294 +56596,49162,71310 +404,89395,28595 +17780,17491,9058 +33992,67219,6590 +62830,27624,35645 +16702,93147,43853 +95708,42380,14037 +90189,6190,7078 +10431,95390,40995 +58547,46376,74245 +58640,97,66925 +3108,96983,2927 +42376,94460,62312 +33887,54142,25248 +22880,96125,710 +28863,63610,176 +97144,60146,17606 +44507,9918,6135 +50929,98523,63299 +51283,37198,9934 +47320,37274,48035 +41651,68845,46037 +92319,21446,5329 +6240,32308,27944 +29485,93123,66666 +57908,76769,88162 +10636,17642,75910 +56735,91255,52621 +75839,20893,62833 +97751,95512,71611 +94457,71018,31968 +15836,80098,97371 +67480,62570,23141 +90746,70130,57471 +84343,19925,30058 +4494,96377,69822 +83034,7418,13165 +91680,55977,4300 +9921,23007,8152 +98544,94741,86995 +14867,45142,57017 +39123,45685,77383 +17166,79874,69928 +44896,69124,62812 +92332,28978,17471 +88696,90250,45924 +8671,33009,20127 +97314,77077,26425 +59805,71177,65308 +17442,99626,96579 +12421,42089,29565 +52893,16879,7537 +52378,93791,92179 +41034,90064,64452 +16900,41328,18861 +31690,95858,1195 +38376,63415,61406 +3325,44993,52284 +52388,60574,75620 +84300,69772,9771 +22821,76108,61938 +37131,99021,3388 +86492,25312,69134 +89949,9356,56438 +45917,34628,73869 +72819,89545,96735 +23398,4788,6261 +5677,19195,91965 +54945,85226,22394 +95784,44836,6157 +24228,74114,12271 +84335,13989,37041 +64733,96430,79659 +51192,62180,77107 +84317,10228,99623 +14471,52708,44433 +62479,62799,20250 +32530,43905,20897 +32148,85215,58042 +71992,57280,50458 +17045,77760,81764 +4720,95513,59301 +35174,28011,11234 +65391,50636,51693 +54178,20998,73673 +30330,89969,3424 +76590,38359,13199 +10744,51360,71301 +89225,57049,97833 +33341,79793,55634 +82180,88921,22595 +77636,56961,17456 +22935,6880,36236 +35838,7121,48014 +41434,93313,92835 +94717,44052,24083 +7499,77064,55100 +92896,57703,95496 +56908,42845,42876 +15695,56431,58630 +56191,60426,9684 +75994,12741,46826 +30907,82944,46450 +60,47243,82156 +76171,35732,87332 +78892,30862,66381 +30585,64245,62202 +13872,80858,20152 +49291,25206,34731 +47685,98142,9316 +69091,32566,63071 +65644,86797,6948 +17375,29906,1928 +41334,97002,50472 +69732,28558,48522 +51503,30589,78101 +67238,71531,34855 +75405,93793,62997 +96842,30387,99232 +37656,24678,21948 +75923,72156,29499 +72140,31803,89113 +6366,62852,70333 +82019,32008,48712 +10906,2195,55794 +58597,41940,6166 +92352,2090,99820 +20323,12282,70772 +74548,3060,84421 +13001,71264,23954 +44939,57063,14784 +74581,58642,5318 +1307,30523,32941 +41323,52284,91932 +38922,5165,54999 +97821,64126,47695 +23162,83298,52783 +72252,39272,89467 +98970,89366,54736 +99920,49483,30312 +32360,66083,90947 +32777,55626,56398 +24505,93102,12646 +1192,71091,84067 +14467,640,66793 +43027,70930,99318 +2606,89373,33155 +34322,48947,28013 +83590,621,7668 +64852,31045,82681 +22112,22429,10555 +44191,70999,94603 +95899,2461,53478 +80234,51301,40358 +58711,6002,25422 +96520,85999,90350 +98719,89432,11815 +23469,59589,58354 +73492,85238,7200 +75042,80133,94471 +62794,95957,57681 +61934,46128,81773 +64770,97642,21157 +26451,70261,23756 +92877,74046,92865 +49678,4837,90317 +62639,40409,30393 +25999,57087,25000 +7774,7322,88819 +54419,53657,10578 +14070,90193,18014 +84900,92750,32616 +56591,40158,51002 +33809,90620,94224 +80446,72802,66370 +676,97606,41388 +92140,49160,89351 +27831,15653,36636 +81951,25408,60692 +58992,2651,1933 +94884,25279,2859 +70350,78982,77039 +94609,7491,6645 +24784,19088,94198 +88875,34195,34541 +44361,51880,93600 +36086,59411,85638 +28938,21613,78375 +13929,55824,4745 +88782,17577,43517 +70475,50866,70400 +30986,33429,54597 +82479,69750,18555 +46843,55230,55719 +54516,13072,56172 +57320,79876,85860 +13692,99467,79705 +50355,10417,42430 +27400,76691,41625 +16717,24571,91194 +77526,75332,46103 +94590,76845,84224 +82159,96856,97249 +41546,62439,98212 +15465,43267,77847 +45039,37868,63418 +22281,2208,58755 +4144,43269,41217 +17877,76261,59111 +61039,57250,73028 +5782,58238,67269 +96390,66960,99771 +7911,29680,1795 +38975,58115,37206 +68477,48341,77475 +18642,87190,25456 +3009,83806,47517 +97289,70544,16234 +36164,97128,66917 +6692,97762,94960 +34448,1005,90963 +10773,63068,80710 +24164,48845,94638 +86239,49378,35081 +6725,76718,22322 +77067,44117,69725 +54204,64017,82050 +10272,52485,79916 +68317,18165,79545 +74819,36753,64230 +19448,65899,72908 +31471,92447,68868 +56759,50267,67362 +71941,96132,25631 +5202,96640,88943 +38067,99403,63180 +29588,31909,23614 +21551,73726,60372 +36198,84224,41763 +34856,39551,21776 +21378,97077,34058 +60782,97178,61951 +12328,63957,15939 +52202,94330,18259 +90643,93621,32768 +8220,58888,63833 +8541,92219,70926 +71045,29062,510 +54529,11036,69965 +26512,71105,55665 +83399,46274,68545 +93357,98878,41004 +9181,88286,47362 +75347,16900,17056 +23807,29771,63952 +39595,41632,82807 +16233,2409,99212 +35543,35734,56441 +76643,10621,44633 +62184,59710,10201 +88817,59618,33685 +79869,84235,77951 +31,60172,62322 +77150,16757,93973 +20089,74707,90331 +8377,67676,15486 +42827,1251,77688 +25739,16058,3056 +65345,81585,7972 +94272,1077,76526 +95217,94361,77978 +92363,39022,26084 +68705,40705,98551 +39831,32698,78222 +25906,75037,36799 +95960,30557,28372 +56416,12126,45670 +75439,45121,37443 +23171,58481,27139 +43897,24083,56778 +47559,20557,52511 +19181,41393,96459 +29217,74666,76112 +9868,89133,82832 +18115,98435,68082 +22566,46202,26370 +70506,95226,22766 +85794,62179,84961 +63658,51425,40758 +40283,61903,4206 +10725,29855,99308 +2488,37217,64170 +68104,83249,11366 +41259,55765,43017 +15846,66822,67843 +33868,97336,60873 +62863,27051,98432 +7609,867,41201 +67313,42159,3102 +7449,93206,26281 +38545,86395,29617 +19766,86528,56099 +87552,50362,30871 +33030,55965,62591 +65703,54249,36571 +45781,96976,56449 +41622,9699,5673 +11564,74478,68818 +62462,77639,98586 +84914,1357,63680 +57159,94334,38305 +87173,66372,48131 +65282,79742,3682 +27768,30758,9308 +10952,69158,92328 +83754,42177,56529 +69396,61510,96621 +3456,44916,56589 +81192,60191,41367 +10470,57808,8114 +92073,39117,21502 +78277,23856,12089 +68268,61211,78820 +1124,95296,29449 +13979,49994,3635 +37965,98550,75585 +69821,86565,65500 +54785,19967,53067 +90366,83323,80686 +38330,55195,94539 +25711,183,85806 +89208,66494,10162 +18160,76018,7411 +6787,84931,37793 +60845,74410,11017 +85564,80516,75356 +50451,13640,60402 +53235,84691,28870 +75843,375,70384 +59564,28429,6169 +74358,99572,39466 +34705,54679,64686 +12042,98841,43098 +18096,19283,28601 +99299,3339,48399 +69326,47998,34166 +35007,79923,72463 +65834,10150,44222 +32643,79125,79990 +15123,52044,72506 +93688,7854,88330 +87495,83091,67117 +70575,3596,85099 +94879,67856,81098 +66694,28388,4888 +9278,48159,59094 +80341,23674,73029 +35485,81065,86495 +66354,69371,43316 +9047,95088,84533 +52264,22237,36608 +97468,19173,41733 +6532,56230,91783 +17092,5282,79112 +45481,77329,12662 +66607,76209,98671 +90712,66508,84152 +46991,26940,17240 +73837,85756,75070 +26540,45439,89962 +85935,91924,31571 +32330,98443,62957 +72338,33497,45077 +95288,13570,88844 +86677,17384,3992 +76337,59369,32296 +38981,56988,6619 +44763,29469,35956 +4269,25973,62563 +5647,20851,68636 +9014,29222,2778 +9327,35572,94692 +29717,95619,6632 +15981,53439,96261 +38654,32237,49834 +13856,77574,53128 +73118,83271,48563 +73116,80365,73775 +19628,77897,78354 +58818,86089,51626 +50975,51124,28070 +63471,29542,462 +93930,29915,28718 +22048,90720,67390 +53302,21233,10076 +34921,85015,37879 +21762,23863,40168 +88429,73359,60770 +14733,87045,57429 +37683,36384,94138 +36151,56937,90688 +21013,46790,67797 +17465,4249,61869 +13505,82041,70299 +99850,36703,23888 +59106,39734,36842 +1034,77732,81624 +22220,10356,76631 +89556,5285,11805 +53919,29610,56517 +88753,14821,10220 +32505,18426,48385 +95785,33113,29844 +52807,54514,35441 +1767,20580,39294 +70098,92864,17393 +36683,18878,897 +622,36802,68075 +38260,73898,3093 +60137,58872,76328 +26962,48591,34940 +86930,46103,22672 +133,45907,76735 +82231,41881,33877 +63194,96916,10374 +32832,26222,20430 +87375,13374,1115 +77965,22601,42108 +94799,37569,49344 +91507,67148,81826 +60910,18100,5706 +40448,93395,79752 +27959,35297,73296 +10682,13645,43303 +56645,5541,57972 +48443,92759,68660 +552,38301,74787 +18902,43897,93194 +1952,57268,38340 +13788,37494,76725 +99726,66784,40809 +39980,4299,85576 +22958,59997,97461 +14496,88895,73983 +8733,72770,22689 +74557,43330,15324 +42915,58042,4363 +21958,70048,11585 +94158,33640,89481 +50630,89968,66846 +42730,40676,42299 +56970,73468,65870 +95024,27584,77357 +10398,56659,23220 +9845,94742,44638 +67164,15500,34028 +47525,69432,82579 +26771,76671,70200 +94264,68811,72778 +55080,95173,19055 +74929,30315,65016 +80146,41272,9837 +58781,12766,83093 +14310,93811,62739 +85742,82122,95599 +70479,98358,50932 +73502,46583,1625 +30741,41973,12922 +9969,5340,35760 +76466,76168,79988 +22141,9102,89399 +45314,67908,32442 +82169,4940,9672 +74110,67413,80279 +72755,46168,39386 +6190,78811,90790 +1039,98919,58362 +86103,54867,68837 +29400,65210,25995 +92512,23326,6787 +44297,14349,49247 +25836,71605,72310 +8796,10579,87325 +79866,2238,73804 +92756,41835,57737 +98930,15434,99407 +92023,11130,43796 +78234,89974,9214 +58284,93808,40855 +59050,99547,85874 +11897,56257,11821 +58940,49898,46175 +15123,80309,17635 +74153,78826,16754 +817,31154,81492 +25713,16993,93057 +20786,82093,57319 +8932,19570,69499 +21427,82975,28327 +98917,82560,68530 +53051,61745,12455 +34648,77983,61993 +68244,18442,63050 +94351,50423,33641 +56080,44607,68118 +37946,94469,29731 +39833,40765,74956 +73893,41437,57360 +33938,29975,23404 +35381,14912,14004 +71745,27595,31391 +76498,55702,75845 +73462,13702,59608 +52468,34963,93162 +99275,48948,87838 +8245,82202,89381 +80112,54454,452 +1499,6455,1931 +24464,12219,13567 +72994,62025,9558 +92174,76922,65163 +79038,93057,63599 +57094,60980,49661 +10290,43674,38499 +36385,50479,93993 +5441,40878,88055 +51300,62566,30366 +98833,9196,9986 +55434,99445,84247 +59310,3173,67730 +93908,25960,61744 +49971,19781,20791 +97944,57525,44320 +25679,44063,72878 +30702,50753,21732 +45704,46664,32703 +28362,35858,83358 +38862,92869,96324 +55584,33014,19836 +2097,48401,78891 +34020,64507,38009 +47699,12635,16005 +21506,93238,96419 +80583,83930,27907 +99009,7708,61138 +11438,4476,90325 +21093,50531,62580 +13879,57274,4753 +25563,45035,1863 +7751,30135,53527 +6024,11401,26763 +12030,86942,68116 +41509,10910,573 +743,93531,96651 +26504,72848,42377 +42094,59396,88073 +76192,63506,79380 +83893,86877,51911 +12267,81558,86663 +4876,6704,44026 +67807,58652,18425 +31969,71833,90667 +46824,33968,35272 +8058,63201,50925 +97146,61314,76846 +1833,60790,33018 +29527,73952,84186 +92727,92799,96156 +53226,80923,68960 +70989,59439,70681 +95224,55369,35329 +46663,3207,48814 +87936,60963,49906 +93757,45244,56060 +71608,47423,85071 +51148,4468,76805 +36050,41870,2635 +5518,22665,70047 +80738,97256,8169 +42442,28173,59036 +47320,24486,31805 +78227,41049,49413 +2029,34278,32162 +26030,59116,90637 +71363,83565,98926 +58085,85137,87286 +35325,45498,59841 +16006,8040,11285 +26183,24529,55372 +87212,98662,38456 +31270,57770,51387 +97991,85344,28435 +85719,68998,37804 +17184,53937,29271 +75773,39643,3790 +35366,62564,40604 +54481,89031,84606 +15683,78317,75156 +92032,5984,62419 +96925,6369,76328 +50396,14457,43195 +71289,98698,15561 \ No newline at end of file diff --git a/input/day08_test1 b/input/day08_test1 index e69de29..20c6fd2 100644 --- a/input/day08_test1 +++ b/input/day08_test1 @@ -0,0 +1,20 @@ +162,817,812 +57,618,57 +906,360,560 +592,479,940 +352,342,300 +466,668,158 +542,29,236 +431,825,988 +739,650,466 +52,470,668 +216,146,977 +819,987,18 +117,168,530 +805,96,715 +346,949,466 +970,615,88 +941,993,340 +862,61,35 +984,92,344 +425,690,689 \ No newline at end of file diff --git a/input/day08_test2 b/input/day08_test2 index e69de29..20c6fd2 100644 --- a/input/day08_test2 +++ b/input/day08_test2 @@ -0,0 +1,20 @@ +162,817,812 +57,618,57 +906,360,560 +592,479,940 +352,342,300 +466,668,158 +542,29,236 +431,825,988 +739,650,466 +52,470,668 +216,146,977 +819,987,18 +117,168,530 +805,96,715 +346,949,466 +970,615,88 +941,993,340 +862,61,35 +984,92,344 +425,690,689 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ae4c90d..04966ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ async fn main() -> Result<(), Box> { Box::new(day05::Day05 {}), Box::new(day06::Day06 {}), Box::new(day07::Day07 {}), - // Box::new(day08::Day08 {}), + Box::new(day08::Day08 {}), // Box::new(day09::Day09 {}), // Box::new(day10::Day10 {}), // Box::new(day11::Day11 {}), diff --git a/src/solutions/day06.rs b/src/solutions/day06.rs index c531065..96324db 100644 --- a/src/solutions/day06.rs +++ b/src/solutions/day06.rs @@ -45,10 +45,10 @@ impl Solution for Day06 { } } - if let Some(op) = op { - if !numbers.is_empty() { - final_problems.push((numbers, op)); - } + if let Some(op) = op + && !numbers.is_empty() + { + final_problems.push((numbers, op)); } } @@ -78,11 +78,8 @@ impl Solution for Day06 { let mut problems: Vec> = Vec::new(); for col in 0..width { - let mut column_chars = Vec::new(); - for row in 0..height { - column_chars.push(grid[row][col]); - } - problems.push(column_chars); + let column: Vec = grid.iter().map(|row| row[col]).collect(); + problems.push(column); } // Split columns into groups separated by all-space columns @@ -116,7 +113,7 @@ impl Solution for Day06 { let mut num_str = String::new(); for &c in &col[..height - 1] { // all but the last row - if c.is_digit(10) { + if c.is_ascii_digit() { num_str.push(c); } } diff --git a/src/solutions/day07.rs b/src/solutions/day07.rs index e9f7871..47df0d7 100644 --- a/src/solutions/day07.rs +++ b/src/solutions/day07.rs @@ -25,7 +25,7 @@ impl Solution for Day07 { } impl Day07 { - fn solve_paths(&self, input: &Vec) -> (usize, usize) { + fn solve_paths(&self, input: &[String]) -> (usize, usize) { let lines: Vec<_> = input.iter().map(String::as_bytes).collect(); let width = lines[0].len(); let start = lines[0].iter().position(|&b| b == b'S').unwrap(); diff --git a/src/solutions/day08.rs b/src/solutions/day08.rs index d6c2668..a663271 100644 --- a/src/solutions/day08.rs +++ b/src/solutions/day08.rs @@ -1,3 +1,6 @@ +use std::cmp::Reverse; +use std::error::Error; + use super::Solution; pub struct Day08 {} @@ -5,16 +8,30 @@ pub struct Day08 {} impl Solution for Day08 { fn part1( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let points = self.parse_points(input)?; + if points.is_empty() { + eprintln!("No points provided."); + return Err("no points".into()); + } + + // default k = 1000 for the puzzle, but allow override with env var or small change here + let k = 1000usize; // for test input + Ok(Box::new(self.solve_part1(&points, k))) } fn part2( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let points = self.parse_points(input)?; + if points.is_empty() { + eprintln!("No points provided."); + return Err("no points".into()); + } + + Ok(Box::new(self.solve_part2(&points))) } fn get_day(&self) -> u8 { @@ -22,7 +39,197 @@ impl Solution for Day08 { } } -impl Day08 {} +impl Day08 { + /// Build all pairwise edges from points (upper triangle). + fn build_edges(&self, points: &[Point]) -> Vec { + let n = points.len(); + let mut edges = Vec::with_capacity(n.saturating_mul(n.saturating_sub(1)) / 2); + for i in 0..n { + for j in (i + 1)..n { + edges.push(Edge { + a: i, + b: j, + dist2: points[i].dist2(&points[j]), + }); + } + } + edges + } + + /// Solve: take first k edges sorted by distance, union them, then multiply top 3 component sizes. + fn solve_part1(&self, points: &[Point], k: usize) -> u128 { + let n = points.len(); + if n == 0 { + return 0; + } + let mut edges = self.build_edges(points); + // sort ascending by distance (stable) + edges.sort_by_key(|e| e.dist2); + + let mut uf = UnionFind::new(n); + let take = k.min(edges.len()); + for e in edges.into_iter().take(take) { + uf.union(e.a, e.b); + } + + let mut sizes = uf.sizes(); + sizes.sort_by_key(|&s| Reverse(s)); + // multiply top 3 (or fewer if less) + let mut prod: u128 = 1; + for s in sizes.iter().take(3) { + prod = prod.saturating_mul(*s as u128); + } + prod + } + + /// Part Two: + /// walk through edges; when union_count == n-1, return X_a * X_b of that last union. + fn solve_part2(&self, points: &[Point]) -> i128 { + let n = points.len(); + if n <= 1 { + return 0; + } + + let mut edges = self.build_edges(points); + edges.sort_by_key(|e| e.dist2); + + let mut uf = UnionFind::new(n); + let mut unions = 0usize; + let target = n - 1; + + for e in edges { + if uf.union(e.a, e.b) { + unions += 1; + if unions == target { + let xa = points[e.a].x as i128; + let xb = points[e.b].x as i128; + return xa * xb; + } + } + } + + // should never happen unless input is malformed + 0 + } + + /// Parse many lines of "x,y,z". + fn parse_points(&self, input: &[String]) -> Result, Box> { + let mut points = Vec::new(); + for (idx, line) in input.iter().enumerate() { + let line = line.trim(); + if line.is_empty() { + continue; + } + match Point::from_csv_line(line) { + Ok(p) => points.push(p), + Err(e) => return Err(format!("line {}: {}", idx + 1, e).into()), + } + } + Ok(points) + } +} + +/// A 3D point with integer coords. +#[derive(Debug, Clone, Copy)] +struct Point { + x: i64, + y: i64, + z: i64, +} + +impl Point { + fn from_csv_line(s: &str) -> Result> { + let parts: Vec<_> = s.trim().split(',').collect(); + if parts.len() != 3 { + return Err(format!("invalid point line: {}", s).into()); + } + let x = parts[0].parse::()?; + let y = parts[1].parse::()?; + let z = parts[2].parse::()?; + Ok(Point { x, y, z }) + } + + /// squared Euclidean distance, as u128 to be safe. + fn dist2(&self, other: &Point) -> u128 { + let dx = (self.x - other.x) as i128; + let dy = (self.y - other.y) as i128; + let dz = (self.z - other.z) as i128; + let s = dx * dx + dy * dy + dz * dz; + if s < 0 { + // should never happen, but cast safely + (-s) as u128 + } else { + s as u128 + } + } +} + +/// Edge between two indices with squared distance. +#[derive(Debug, Clone)] +struct Edge { + a: usize, + b: usize, + dist2: u128, +} + +/// Simple union-find / disjoint-set with union by size + path compression. +#[derive(Debug)] +struct UnionFind { + parent: Vec, // if negative, -size; else parent index +} + +impl UnionFind { + fn new(n: usize) -> Self { + UnionFind { + parent: vec![-1; n], + } + } + + fn find(&mut self, x: usize) -> usize { + // path compression + let mut root = x; + while self.parent[root] >= 0 { + root = self.parent[root] as usize; + } + // compress + let mut cur = x; + while cur != root { + let next = self.parent[cur] as usize; + self.parent[cur] = root as isize; + cur = next; + } + root + } + + fn union(&mut self, a: usize, b: usize) -> bool { + let mut ra = self.find(a); + let mut rb = self.find(b); + if ra == rb { + return false; + } + let sa = (-self.parent[ra]) as usize; + let sb = (-self.parent[rb]) as usize; + // union by size: attach smaller to larger + if sa < sb { + std::mem::swap(&mut ra, &mut rb); + } + // now ra has >= size + self.parent[ra] = -((sa + sb) as isize); + self.parent[rb] = ra as isize; + true + } + + fn sizes(&self) -> Vec { + let n = self.parent.len(); + let mut out = Vec::new(); + for i in 0..n { + if self.parent[i] < 0 { + out.push((-self.parent[i]) as usize); + } + } + out + } +} /// Test from puzzle input #[cfg(test)] @@ -44,7 +251,7 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "20"); } #[test] @@ -61,6 +268,6 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "25272"); } }