From 4f5898255bd837631b9092196d4d3c1973074457 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 22 Dec 2024 08:16:42 -0600 Subject: [PATCH] 2024 Day 22 Complete! --- 2024/day22/input | 1632 +++++++++++++++++++++++++++++++++++++++++ 2024/day22/main.go | 116 +++ 2024/day22/testinput | 4 + 2024/day22/testinput2 | 4 + helpers/helpers.go | 4 + 5 files changed, 1760 insertions(+) create mode 100644 2024/day22/input create mode 100644 2024/day22/main.go create mode 100644 2024/day22/testinput create mode 100644 2024/day22/testinput2 diff --git a/2024/day22/input b/2024/day22/input new file mode 100644 index 0000000..5657928 --- /dev/null +++ b/2024/day22/input @@ -0,0 +1,1632 @@ +8945015 +12837429 +9210031 +1866170 +13626549 +5759740 +8110456 +16550935 +15494709 +8840159 +4366810 +11646429 +16054706 +1318639 +6286402 +6293852 +9250312 +12437685 +13525420 +15715428 +232907 +7437620 +470598 +8469997 +8556311 +11203336 +2942972 +14194947 +3397980 +13887710 +280416 +3436954 +14716460 +10310831 +5282448 +14551259 +3054496 +13344449 +3546404 +3059012 +9526934 +5670448 +15622777 +238626 +11827453 +7220002 +1840308 +11455420 +8684421 +6337853 +10937948 +6049030 +6503030 +904437 +2351439 +9688737 +4051453 +2647402 +8888347 +4413091 +3149576 +12745822 +299487 +16102077 +6217203 +3796857 +10387429 +7677002 +11773775 +2186133 +16724778 +14779635 +16207036 +7425348 +7377445 +9092856 +4010846 +5542622 +14931960 +7917127 +9844318 +12849555 +446795 +16333879 +7596569 +5013959 +6833121 +2425615 +11261672 +224484 +9301996 +12152637 +1822963 +10733897 +10342654 +1529556 +13457517 +2281853 +6454070 +5122713 +10222180 +11402255 +1340476 +6396924 +5535155 +11348672 +10238255 +10966573 +6971646 +8960880 +3866510 +7170990 +7085377 +6860076 +5642693 +13650645 +1565817 +16429418 +13429937 +7098401 +5247966 +9831917 +1144095 +12730991 +12434831 +13709616 +15472334 +11428031 +6020382 +2564726 +13083957 +2898086 +14744832 +7754113 +13161637 +7251266 +16312351 +10611206 +10374180 +2250302 +2251181 +14945654 +14213260 +11774531 +15410702 +15078532 +2542656 +15461935 +4501771 +6906351 +12412653 +8320858 +4966280 +10406827 +13933146 +2240871 +14626018 +3700557 +2781079 +3771464 +12051845 +15529180 +4597304 +4262757 +9765028 +11186340 +6529409 +13381705 +9589209 +15852442 +8460483 +15800223 +3285057 +13431569 +3021909 +7091210 +12628719 +10601992 +12753621 +12012753 +4469199 +15442272 +3722862 +3165277 +8938965 +15425120 +12925750 +11065650 +10570470 +1614257 +9062795 +4284715 +2189630 +5869006 +14933434 +6272940 +2148871 +14175297 +11183096 +2658743 +5163913 +579225 +2220286 +12370954 +4705261 +15096845 +12152669 +3294397 +179572 +8080645 +5790652 +10781006 +1858604 +5104496 +866811 +10215548 +1737360 +12480473 +13039164 +156426 +2920418 +4907119 +5728907 +6077479 +12845081 +3281663 +5928915 +12662781 +16333045 +2840031 +15241854 +14746089 +10068550 +11790363 +762869 +8540617 +7655331 +8701968 +16551391 +2788832 +7845811 +1589903 +1734093 +11547403 +11309004 +8889637 +11465603 +14441534 +6306303 +5666263 +12332256 +14609054 +2987719 +8288969 +12153819 +15257094 +1131062 +10591907 +6680756 +14798933 +5246509 +4908142 +12111883 +4583802 +13705004 +4704420 +12457198 +16651524 +12918635 +5625540 +5115544 +1249479 +732001 +3224692 +16333555 +3638544 +232311 +6968965 +2784855 +6033141 +7346047 +2622077 +3060095 +3603039 +10899851 +15909468 +12973028 +1151146 +5727780 +16759129 +16287271 +15615991 +1625913 +6398197 +3242330 +10392529 +8015104 +9174929 +14663768 +4843276 +6828993 +13458946 +2843531 +13954726 +13113070 +7631394 +6697139 +10312398 +5308403 +9734722 +10382186 +4994112 +3766816 +4506298 +2040661 +14163408 +9345928 +3411019 +3933677 +9155098 +6102268 +6568473 +11212550 +4979001 +2017359 +11680060 +14991395 +11968426 +7715779 +3562623 +13524674 +2024356 +14871282 +13567491 +9298100 +2567833 +1444000 +7323540 +8996509 +5501680 +2474200 +5401445 +15334280 +13234078 +15431486 +7217847 +8779904 +1345996 +12528086 +4632684 +8159191 +11728269 +4435067 +8621087 +15659193 +11319116 +7941157 +5544055 +4756567 +3622312 +12337749 +3772092 +2381270 +6536275 +14895222 +879493 +11522248 +10320309 +7468979 +519745 +10418897 +10868772 +4821878 +13531386 +14520216 +2358774 +2571043 +5544001 +16201211 +12245718 +12179875 +15011444 +4566493 +5245559 +5654304 +6838998 +10226357 +4819469 +16142806 +8709622 +14261087 +7101658 +1621517 +9560754 +10250075 +6463916 +1249870 +10995709 +13146098 +14535952 +7199601 +16473093 +13134964 +15280103 +13867945 +3114941 +9299666 +12066084 +5826460 +8557368 +15717780 +3074404 +12082329 +2988321 +9804872 +11458964 +3777097 +6792136 +3452913 +10043342 +16191673 +2050509 +12350804 +16172365 +5886041 +16196943 +15633962 +3392155 +8408158 +9772992 +4777102 +4202958 +3407953 +153616 +15621171 +8007508 +15623865 +16452039 +4139774 +1255814 +15855772 +8000503 +4970991 +13415466 +9585455 +4450153 +5762866 +8538099 +16173642 +15478402 +8230506 +3527971 +3750462 +10844035 +1593404 +14352532 +10000981 +16559479 +330030 +14639535 +2527690 +3274288 +519650 +7932719 +3742478 +14052812 +2890423 +810475 +13145140 +7773239 +6867633 +9499016 +13629136 +7858115 +6557823 +6840423 +15688972 +8899493 +3950657 +16412678 +7615632 +9464459 +14640282 +4174510 +1192393 +7798459 +14141769 +7536724 +15562346 +2001772 +1741921 +12353641 +15486035 +10192869 +15778015 +14043798 +9312715 +16356818 +3892521 +13930082 +4005068 +4184800 +5579144 +4636463 +6627396 +6916918 +6193538 +2117682 +5482337 +11890875 +5388896 +11817129 +12062647 +16233286 +828660 +15188203 +10658447 +9396706 +5184529 +13249237 +3256299 +3595835 +5558212 +7544969 +13683121 +4950751 +11311835 +7494045 +5419815 +6180046 +8007592 +1025391 +10893107 +14645748 +13758050 +7444388 +2682616 +9220780 +1740283 +3952323 +7020869 +9010508 +14142332 +14283313 +11063722 +784867 +6891363 +8999420 +7754612 +7664349 +1875915 +14849415 +3487916 +15617832 +3396703 +4397909 +9318128 +6255789 +11620940 +15460889 +12694366 +11505774 +15293896 +4359825 +7199474 +11576754 +8820638 +2306242 +13014398 +4159661 +4963253 +7918275 +4038161 +6307041 +12944451 +1835384 +9896894 +15258954 +13750467 +13427060 +858340 +12023214 +16683663 +5535122 +613541 +9351963 +7337785 +16289513 +6310349 +3163803 +6475140 +3431466 +7210852 +11387473 +12413196 +15884003 +12247374 +10070032 +5455192 +8428707 +9554329 +7746137 +4337462 +4675221 +10386875 +5558037 +9113052 +14278888 +3493066 +3995657 +1562718 +7776804 +14541997 +9799976 +16462475 +13944419 +12022666 +12243466 +13023833 +2023809 +5586959 +9953245 +10127216 +12986951 +886270 +487660 +15725926 +1983969 +14926875 +3331175 +5740963 +3648297 +11343060 +6941145 +13751232 +8439457 +8254260 +5987519 +9264550 +2961287 +6928715 +4787260 +12874602 +3412455 +5436387 +3528087 +5514566 +3155509 +823397 +11031654 +2878396 +13589191 +11476198 +8443325 +3547725 +281416 +7501539 +4523645 +6771629 +341047 +391838 +12368160 +831173 +8782219 +8692513 +15105730 +15675726 +1508772 +8167618 +14111525 +14380786 +1295876 +8995192 +5567868 +12447361 +846703 +1841844 +196973 +14560469 +977054 +13315895 +3462792 +14414424 +14439197 +14236744 +2478531 +1050735 +16091765 +2545621 +5004788 +1503774 +14241934 +4912230 +8558432 +15876982 +7923681 +16420726 +13855883 +6877325 +2918138 +6066783 +3450145 +10259134 +8464834 +6983012 +14186697 +1520930 +14781966 +9317959 +10709995 +3943198 +3759901 +254097 +7152156 +15616273 +9173227 +6984776 +9795047 +16587506 +3331472 +1728444 +14630365 +11909846 +12292694 +7451179 +401587 +6761750 +12635314 +14790245 +6043376 +5180633 +3210984 +12228112 +15536719 +11607414 +10778084 +14481174 +16665208 +11978534 +8962949 +6851736 +1289683 +5665551 +11622306 +12710702 +626061 +11872186 +10202087 +12049525 +16238262 +4615805 +2719968 +147397 +12040528 +8216654 +8567443 +15858853 +9796280 +16476144 +12025469 +2873020 +5078116 +8959033 +6695147 +10547782 +11039803 +1645228 +15841881 +576166 +15368735 +5381189 +15502286 +8634966 +4907023 +13199325 +3658014 +15145878 +4620656 +9918990 +16182889 +8557369 +531332 +267771 +9208128 +13476477 +10581723 +6526936 +15114921 +6437229 +12749008 +195954 +138055 +6203424 +2207355 +906589 +6398980 +6322744 +11997580 +8580817 +7983338 +1819738 +5600382 +1471881 +14202602 +16666297 +1550431 +10250813 +11265549 +6494642 +12329971 +13583534 +10546348 +2613550 +4625557 +1047382 +3513384 +13101290 +3071341 +7225881 +8315176 +8183254 +2653125 +9877608 +5748261 +13750183 +3305559 +4414358 +16573697 +4390975 +10811181 +11009786 +16361538 +15781059 +15463279 +11672786 +12997803 +5670247 +8457218 +13636515 +14937346 +8150124 +9972293 +9705500 +5085731 +15358594 +8263498 +14534201 +626108 +13215148 +7284548 +8880518 +640152 +7110602 +1353593 +14264124 +16364237 +2345591 +10328850 +1120470 +4603595 +9184816 +3889023 +13481620 +4457727 +783062 +914382 +2298443 +4913793 +15240774 +5769322 +5422972 +5754677 +10396491 +12767238 +2503096 +3761346 +13868619 +15585356 +14078148 +7087317 +1922580 +4086487 +6078179 +9741379 +11895976 +9674387 +11824175 +4511513 +6956639 +5695530 +941480 +13631666 +5082044 +8449919 +926499 +5421242 +6792200 +3840362 +12330987 +9625413 +7963489 +5566941 +12345964 +3511562 +2137614 +8123568 +4827834 +11566666 +9622507 +12532664 +10562125 +11883790 +7657797 +9578949 +12819384 +16418566 +6188570 +5608489 +12147604 +14684829 +12075125 +14500336 +6569777 +1485499 +8608458 +4223478 +13311484 +12209940 +12984943 +15509739 +1280751 +8178953 +1223869 +6264875 +7909255 +6498467 +9013834 +14114564 +11277616 +13548434 +16582244 +3126093 +15509530 +2637521 +9610573 +6600126 +3265237 +3022872 +12305657 +15139691 +10643473 +3013676 +5472690 +3193329 +13929256 +3487718 +1061330 +8124579 +15926279 +13709506 +12990680 +3170243 +1424214 +12249309 +14932538 +7156050 +11701136 +14437993 +15060739 +11871862 +11429625 +15534554 +10378682 +16417430 +3736607 +15412579 +16759359 +10238832 +11208391 +14033366 +8299042 +14327124 +14802132 +3933542 +2890124 +5907813 +6973906 +11566720 +16215098 +7240552 +6421363 +2047131 +6565093 +8237593 +16773469 +9916576 +9952108 +8194608 +11390515 +16372632 +9279049 +9912988 +1577665 +5077519 +13448782 +9208086 +395704 +10062575 +7557973 +9347955 +13729370 +14083733 +6346071 +3898447 +13821399 +16278599 +2045590 +4022166 +5090817 +10790510 +8571058 +8146293 +706422 +9361056 +5893932 +8122032 +10023163 +12685004 +8656249 +3341612 +15356226 +1766092 +15623382 +15679634 +3297021 +7397918 +14816823 +5780086 +1101926 +12241554 +4937221 +8780084 +6868754 +1208167 +1157655 +14982632 +16442899 +16626348 +7453975 +16724561 +4969229 +15185270 +9858449 +11116661 +5909004 +15887630 +14911823 +16107741 +2444127 +12313665 +6436593 +9088739 +11226609 +3141258 +14856104 +5840494 +13929192 +11605051 +15827105 +224429 +16450698 +14370918 +11960100 +14206950 +15300004 +768235 +13126271 +4366968 +15506372 +8340456 +1211306 +11396176 +8366290 +11284877 +16141103 +789945 +5362603 +4027234 +6605336 +11454567 +12062312 +5597198 +15403441 +3313873 +5113873 +12116433 +16281500 +3558570 +13832573 +8563759 +10397479 +12551219 +16203298 +10100626 +16207355 +1138021 +11033722 +14968342 +1490976 +13816526 +4068000 +14757960 +1236439 +8351141 +10923434 +842019 +13360194 +11543910 +13885703 +250733 +6500248 +7675774 +4907557 +12188995 +15557304 +15837700 +16697165 +10208930 +4698893 +4707726 +5429879 +15561380 +7993649 +7274019 +14509446 +4126290 +4631913 +9630507 +15814693 +2867460 +5411550 +2517980 +1467415 +8197797 +15539379 +5394986 +10294893 +1208688 +16467003 +9569182 +11921098 +13292633 +7671619 +976543 +824397 +10559160 +4861960 +12789183 +5185126 +6206094 +3943791 +11053724 +9144647 +12867185 +7770448 +15714417 +4859424 +5685252 +13851388 +6798647 +12673508 +15733182 +5244367 +10170469 +14435722 +16159722 +716151 +13509094 +8619112 +11709117 +13945758 +16116655 +16550736 +10123196 +8584200 +6930053 +9245119 +164061 +6726782 +15902767 +819311 +5196097 +15647977 +12261374 +7903764 +261095 +15433878 +4257809 +2780974 +14054709 +7801869 +8183544 +7878676 +13323018 +11727377 +9846951 +8152204 +13529254 +16100894 +12230528 +1010010 +4266675 +9157222 +14149948 +14724709 +4607133 +15624816 +10606610 +3001853 +15042944 +9929137 +15624463 +795656 +812303 +8003059 +8281587 +14990520 +15393305 +11270135 +12913953 +4850296 +4374956 +7229659 +214591 +1920184 +1969461 +8535977 +8271880 +12095769 +8619157 +15929349 +16157864 +8329589 +13192357 +364631 +12941164 +16719614 +833669 +2003642 +4940349 +5085348 +13933985 +15761358 +2626649 +4142234 +15660414 +12788977 +13856821 +7232952 +5774893 +10833826 +6536473 +5509392 +5351543 +13252012 +5008419 +11016683 +3407586 +6224994 +2330818 +2799064 +15093803 +3620992 +11213953 +12749650 +16181215 +7641954 +12594708 +5103352 +15840906 +14455030 +9133563 +13689070 +4801847 +971633 +9531633 +10768790 +15143892 +6067665 +3529580 +14835011 +12168358 +10218592 +12993272 +13428237 +10821109 +16294371 +12844020 +14300259 +12483538 +16126115 +16534924 +4628048 +1330211 +7180155 +2034705 +4179444 +14347245 +5172220 +9991396 +3929298 +6949977 +3261218 +8837168 +6907691 +12592385 +4575621 +4705616 +9657261 +2641315 +13664677 +5230744 +9958446 +5855407 +7340780 +7288170 +9150278 +16488420 +15496478 +7036963 +1463272 +2005426 +2462286 +13363532 +756524 +8646409 +562197 +12345514 +12713154 +3343084 +305022 +4537489 +10144620 +11662659 +16475462 +9537941 +2120765 +15060558 +13938971 +12661297 +14454798 +4728148 +315965 +1201785 +1885934 +4012392 +7682384 +16048889 +2159631 +7050094 +6971988 +6305072 +12667348 +16351976 +4109726 +1084606 +12227696 +13920453 +12373817 +13356121 +2251134 +10349010 +1357950 +12041734 +11935676 +9061195 +945037 +11721899 +11923218 +4809931 +8022860 +3300157 +4977059 +4704434 +248776 +10789120 +14165655 +5424434 +6170182 +1258551 +12585460 +1904944 +15829232 +16425618 +12576140 +12592987 +1530879 +14453281 +3856824 +11015210 +14076991 +4896846 +16479306 +5500605 +15654187 +11812744 +2456724 +10417712 +825958 +16013727 +4802598 +2759248 +16300305 +7763381 +15990856 +15859065 +14781550 +13864633 +1998804 +4553466 +3452555 +14291949 +2512759 +1312588 +13814761 +16030123 +6342397 +6767430 +3293421 +9903844 +8159347 +3055231 +12173189 +7927114 +2914716 +12354878 +2888619 +1761259 +13911124 +4475000 +12835997 +16676925 +12055289 +14713002 +1912606 +3122464 +15384080 +3163104 +1542634 +6179894 +1177574 +9052551 +14958659 +12672322 +14914762 +7784818 +13887206 +1505963 +6364955 +965628 +10929359 +16481107 +9535522 +13755661 +3121931 +10031629 +5832254 +9564324 +8545399 +3368887 +13846703 +15413886 +5953399 +8443864 +6403468 +5943656 +10188741 +7032312 +12845688 +11284261 +13569319 +11869566 +866207 +2464964 +14478418 +13717483 +12499597 +14509804 +475475 +16097155 +7954007 +15815355 +3626451 +1511488 +13188239 +9603243 +15716623 +5396414 +3965259 +14943307 +2951860 +8200278 +4911199 +15491966 +14007502 +14998838 +10081761 +1985448 +8235718 +7808596 +7871399 +7861273 +13872094 +11488630 +14640225 +10246414 +3694007 +10783315 +7409285 +2424489 +3374974 +5065931 +15520030 +3648882 +1013448 +8659633 +7466511 +7550792 +16194730 +4418651 +564263 +10680802 +260754 +7450948 +3468008 +307360 +9853741 +14268032 +7916915 +7686112 +6538040 +4590741 +1818028 +16330158 +6340655 +10306382 +8537875 +11095166 +16232170 +9996750 +7356000 +4196684 +7328185 +4622515 +11086425 +243090 +12608512 +10724747 +11065271 +10333050 +682092 +4627234 +7921790 +1007038 +16237990 +12976948 +5272142 +1964548 +2255856 +1885480 +6646451 +2531756 +4797277 +16098319 +15744948 +13948800 +8576576 +5488109 +10723619 +1543734 +14622005 +12693693 +2918507 +10393910 +5243244 +7100398 +6618081 +7425238 +15423012 +1530176 +13650847 +10772998 +310007 +14467837 +1270848 +10883362 +6348105 +11544897 +13114069 +15173071 +14229336 +1404378 +15753349 +3921819 +3380862 +16365498 +5842766 +2850402 +8144854 +10302813 +11579265 +5735340 +4758035 +1823423 +4321871 +9146514 +3619899 +10084508 +5381378 +14374932 +1336086 +2306425 +1342744 +6817137 +7328154 +5106419 +195482 +746123 +8860400 +10440729 +1585340 +11063605 diff --git a/2024/day22/main.go b/2024/day22/main.go new file mode 100644 index 0000000..e3d0987 --- /dev/null +++ b/2024/day22/main.go @@ -0,0 +1,116 @@ +package main + +import ( + "fmt" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToIntSlice() + part1(inp) + fmt.Println() + part2(inp) +} + +func part1(inp []int) { + var total int + for i := range inp { + total += steps(inp[i], 2000) + } + fmt.Println("# Part 1") + fmt.Println(total) +} + +var cache map[int]map[string]int + +func part2(inp []int) { + fmt.Println("# Part 2") + cache = make(map[int]map[string]int) + for i := range inp { + wrk := inp[i] + cache[inp[i]] = make(map[string]int) + s := Seq{} + for n := 0; n < 2000; n++ { + next := step(wrk) + seqChg := (next % 10) - (wrk % 10) + s.Add(seqChg) + if s.Len() == 4 { + if _, ok := cache[inp[i]][s.String()]; !ok { + cache[inp[i]][s.String()] = worth(next) + } + } + wrk = next + } + } + var best int + var bestKey string + checkedKeys := make(map[string]bool) + for i := range inp { + if i > 0 { + h.ClearProgress() + } + h.PrintProgress(i, len(inp)) + for k := range cache[inp[i]] { + if _, ok := checkedKeys[k]; ok { + continue + } + var total int + for j := range inp { + total += cache[inp[j]][k] + } + if total > best { + best = total + bestKey = k + } + checkedKeys[k] = true + } + } + fmt.Println() + fmt.Println("Most Bananas:", best, bestKey) +} + +const prune = 16777216 + +func steps(n, cnt int) int { + for i := 0; i < cnt; i++ { + n = step(n) + } + return n +} + +func step(n int) int { + n ^= (n * 64) + n %= prune + n ^= (n / 32) + n %= prune + n ^= (n * 2048) + n %= prune + return n +} + +func worth(n int) int { + return n % 10 +} + +type Seq struct { + s []int +} + +func (s *Seq) Len() int { return len(s.s) } + +func (s *Seq) Add(n int) { + if len(s.s) >= 4 { + s.s = append(s.s[1:], n) + } else { + s.s = append(s.s, n) + } +} + +func (s Seq) String() string { + var ret string + for i := range s.s { + ret = fmt.Sprintf("%s,%d", ret, s.s[i]) + } + return ret[1:] +} diff --git a/2024/day22/testinput b/2024/day22/testinput new file mode 100644 index 0000000..07b41ba --- /dev/null +++ b/2024/day22/testinput @@ -0,0 +1,4 @@ +1 +10 +100 +2024 diff --git a/2024/day22/testinput2 b/2024/day22/testinput2 new file mode 100644 index 0000000..201df76 --- /dev/null +++ b/2024/day22/testinput2 @@ -0,0 +1,4 @@ +1 +2 +3 +2024 diff --git a/helpers/helpers.go b/helpers/helpers.go index b3cf1ee..0ed477c 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -228,6 +228,10 @@ func PrintProgress(curr, total int) { fmt.Print("]") } +func ClearProgress() { + fmt.Print("\b\b\b\b\b\b\b\b\b\b\b\b") +} + // StringPermutations takes a string and returns all permutations of it func StringPermutations(str string) []string { perms := stringPermHelper(str, 0)