commit 95ee6b354e650c61002363dafd4421f2df02aa23 parent f771a9652753fd89882480d30984e2378b32e7c8 Author: Lukas Henkel <lh@entf.net> Date: Sun, 22 Dec 2024 06:38:29 +0100 Day 22 Diffstat:
A | input/22.txt | | | 1515 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/day-22.lisp | | | 37 | +++++++++++++++++++++++++++++++++++++ |
A | t/day-22.lisp | | | 15 | +++++++++++++++ |
3 files changed, 1567 insertions(+), 0 deletions(-)
diff --git a/input/22.txt b/input/22.txt @@ -0,0 +1,1515 @@ +16686445 +11830084 +6933708 +7042654 +668555 +6736470 +10828667 +13214162 +12181133 +3915508 +3133442 +808210 +9946458 +15268056 +1721297 +4243656 +12584720 +1935726 +16632260 +9479956 +15636763 +14296151 +11948091 +5703858 +8755338 +1820161 +5582132 +12978035 +11534677 +6507684 +3202212 +11246206 +9834672 +13863678 +16470246 +143917 +12024045 +4923087 +11042858 +2088869 +1766717 +11413735 +11983927 +327124 +16361847 +5956312 +7370490 +12323607 +11629902 +12060908 +5452184 +5278322 +6904482 +1822449 +12296418 +1762829 +10888018 +4677019 +1553461 +1624190 +10183574 +6684153 +6733526 +4189295 +2167338 +12489268 +3906315 +2918439 +4224666 +1953891 +10241320 +10559930 +1890200 +4916875 +4638416 +3636932 +5361392 +6010929 +4232108 +9411854 +2268557 +2881254 +443329 +9051581 +15493158 +10312235 +11784326 +11440223 +5800837 +225469 +1491902 +8893346 +3372507 +12027911 +11861469 +12485713 +6690942 +11003603 +13496514 +7806431 +16536115 +15130796 +14560808 +16492517 +12365226 +1988742 +12647197 +3034802 +7080173 +15654189 +8255078 +14575712 +5548413 +12259793 +14695051 +8460499 +2163821 +16693956 +6187232 +13831059 +8772136 +16763612 +13704319 +15077244 +13644184 +1379168 +5111500 +14269446 +13438510 +4677525 +14806459 +186164 +5123802 +10207227 +3203701 +9985037 +6091031 +3635512 +10637483 +1966362 +6343233 +7420267 +10291005 +8817246 +741884 +873769 +1238381 +12417028 +16462316 +10442188 +2844699 +6326316 +16552167 +9852445 +16632861 +8758329 +1034705 +494140 +12493033 +14624342 +8583098 +7028210 +2866512 +10139672 +14915854 +10281481 +7922219 +1934514 +15547064 +15847463 +8550457 +11212144 +7385278 +407052 +16710294 +10044427 +672821 +13119067 +12794237 +6777050 +839076 +2496591 +6282513 +2937038 +9619117 +7045571 +11850035 +325097 +2213250 +11585965 +14001512 +7315967 +8181695 +6190601 +3195385 +15768674 +921735 +15145118 +6859122 +5877118 +2952757 +11643759 +890553 +6904942 +16445082 +5325277 +6332388 +15198243 +12666041 +5937075 +4214775 +15437033 +14108666 +15258879 +5460361 +10055112 +8600136 +14175900 +4812105 +1479875 +8550385 +932038 +11642152 +7594686 +14995723 +9526413 +6662863 +6014357 +6467601 +5747779 +7283134 +1426891 +5153700 +8404775 +14356042 +8516446 +496070 +15129474 +11746912 +966441 +9965724 +3086664 +1725104 +8819351 +326553 +12052450 +4260466 +4619941 +10072829 +7799143 +4924627 +12354422 +7591694 +2809289 +9606254 +14577759 +6588095 +1686194 +8976132 +11550380 +7533419 +8809191 +9265415 +4158773 +12234532 +2476757 +12481844 +2151784 +13543753 +16261542 +425932 +8053104 +5176469 +569317 +14734997 +992234 +10297929 +7413758 +3663389 +6244788 +9824041 +5471049 +3304749 +7910262 +13152209 +15157587 +14800036 +11410370 +12958315 +7555095 +13297504 +12195308 +14129952 +13501585 +8435464 +566819 +4335222 +2417876 +15186661 +459484 +16074913 +12183843 +13396262 +1050413 +1004050 +14391801 +14820393 +10824533 +1796048 +323332 +10518653 +1933717 +5365980 +5781551 +3147925 +15177538 +9123987 +11571079 +16110608 +11528688 +9054246 +2067124 +4316606 +12151977 +4238947 +14863880 +12847225 +6112849 +5178526 +2747331 +8747755 +14433919 +11888922 +940336 +3826028 +11619965 +9726554 +12651135 +6843901 +12021377 +9181894 +5037823 +14648671 +14696647 +2678898 +6222660 +11145257 +6671032 +15073643 +11355964 +670611 +9532703 +13344943 +8133829 +1976967 +8412437 +14916334 +9163138 +15693900 +4996450 +10816379 +6397422 +4095475 +12017880 +11249963 +14945676 +16402004 +7337844 +16549802 +6603246 +783665 +13532957 +8256380 +4738553 +9193639 +8108849 +2211125 +7336275 +14920673 +1167706 +15614602 +16224037 +15444583 +11453379 +1270148 +8149173 +3426350 +10466829 +16020185 +16110925 +15425256 +13015398 +1672036 +4314816 +2058432 +4997495 +11790807 +14925484 +16682760 +10556967 +11693131 +9407778 +9116651 +12321191 +698572 +2781104 +12582032 +1353135 +5751794 +11465022 +4749454 +13953694 +11569302 +5052673 +16121814 +2921879 +8104705 +10316558 +4621483 +3116260 +7686269 +3088511 +8359782 +5501288 +12908743 +3764776 +7051566 +6174283 +5491200 +12546022 +11304163 +9517756 +16297907 +4041290 +11494870 +9914936 +10136319 +13407548 +572897 +3560351 +2560923 +7783006 +14588883 +14679096 +1490696 +11680455 +11023099 +13379244 +13407358 +11782749 +10033404 +10528044 +305223 +2688414 +12053199 +1637883 +5109696 +13784584 +14299431 +14952673 +3407820 +13902745 +1268268 +2953073 +10604959 +11932627 +11560605 +1199953 +14818467 +244412 +6385200 +8817262 +9664480 +1868026 +7636515 +1020519 +1742984 +11077070 +8960123 +1665138 +14884453 +16717097 +5484185 +3518089 +1847454 +15741773 +11223377 +13854663 +9684408 +12755364 +12210085 +6373821 +3248123 +8590689 +9684693 +3402924 +5770032 +4305235 +11116135 +10351273 +8319339 +3955458 +14375960 +10226462 +8176074 +14907562 +14246587 +13366849 +13587782 +5071869 +12416576 +4108487 +361044 +1136085 +2314998 +7952501 +4212663 +7785209 +1016197 +3787793 +8001793 +6292231 +8222633 +15149468 +7988037 +12679739 +9179955 +7561987 +1302022 +14118519 +9847892 +15769588 +13490950 +1280322 +5159288 +8049115 +368217 +10923212 +14497185 +15620609 +12183385 +16333558 +14435641 +4120318 +9290037 +15903889 +11393278 +6437908 +10219380 +7290246 +2766242 +2946890 +2018930 +668279 +3220794 +6742213 +3979701 +7456439 +6990012 +8567630 +2908510 +7485421 +5635832 +5390771 +8047617 +3166394 +10674098 +3607424 +12708642 +10028774 +6530972 +16653133 +15758617 +15570240 +7536914 +14350067 +9138011 +3432261 +5220494 +7838711 +9680706 +14347768 +10636990 +6903194 +1744128 +16618458 +6066273 +9907283 +6749193 +15301410 +13074625 +8613035 +9194793 +5627476 +3771095 +4230143 +3152029 +809568 +4415967 +3916069 +3246308 +13183770 +2654260 +7936904 +10238334 +6672759 +12985844 +13513231 +11124385 +2248575 +9632750 +9959920 +12766192 +11115041 +10632712 +891247 +4441952 +2031144 +13344291 +2988420 +16345865 +4748718 +16099632 +6916242 +8831792 +16244513 +10112737 +15899989 +341425 +972941 +1371411 +12073389 +14593069 +13103759 +11455916 +8662556 +3981943 +11786098 +5569597 +12551768 +837250 +15324763 +494421 +13533915 +15803792 +15763789 +13720582 +8624129 +7195548 +1419472 +12582784 +11736833 +7089380 +15911590 +8902471 +4660444 +13162024 +15668114 +16506606 +2946401 +1578370 +9091242 +9762452 +11735375 +14855150 +16419804 +14269182 +10263749 +4558156 +14570360 +8540733 +8758140 +8325685 +16582258 +13215326 +12603496 +7721809 +5144813 +11509199 +6183914 +11210933 +7880493 +622186 +10822700 +9975952 +15213202 +5179481 +1936336 +16043730 +6768022 +3435579 +14205389 +14164638 +1776347 +9386009 +3933987 +806429 +2179446 +10884055 +11780574 +9109749 +3143520 +836596 +2647519 +9714892 +4019140 +7322502 +2916586 +8807770 +14122820 +12621896 +8940965 +9540155 +5249514 +8812744 +3577452 +13082399 +3776106 +5779585 +11655727 +5779223 +11119292 +1516518 +1409287 +8902843 +13569966 +14809605 +4686000 +5411404 +8119834 +9664146 +4569262 +16631417 +4506058 +11112652 +2670501 +1590095 +7905702 +10440103 +16655650 +292007 +3750931 +1626464 +6902309 +16484844 +9018993 +10193260 +5305466 +4567842 +4932027 +6158184 +15528770 +4158245 +3753241 +3767143 +15858452 +6290325 +13343681 +15346592 +11715445 +1374793 +3467068 +3683385 +14558130 +3899208 +2247487 +7405400 +9443259 +6780118 +12433070 +677753 +8717323 +2771175 +5992260 +11951945 +6504283 +13834060 +14468185 +16604944 +5072810 +134657 +5759706 +3375863 +3597027 +7770247 +15215163 +3304207 +11112537 +16083906 +11469172 +16533849 +15197303 +8671417 +6030703 +7790200 +10619112 +6543769 +12196199 +6901515 +4464364 +3217576 +15537820 +1842439 +2372772 +12164179 +3988886 +4838843 +4064961 +5367111 +10141902 +7848279 +14172986 +13333337 +10789129 +10965018 +5768586 +6370117 +9744403 +14980740 +9100900 +5020054 +6574226 +8327420 +10907084 +14871986 +2470613 +15106979 +3654689 +13959876 +8058926 +14628854 +1579746 +598612 +2179658 +289583 +3247552 +1277326 +5647178 +12091689 +15742443 +4836806 +15063040 +836547 +9839004 +10326612 +16441514 +6756730 +7155290 +3389693 +13991902 +6054715 +5421758 +890363 +13253630 +1508339 +7453223 +16218458 +12982151 +14457003 +11800389 +2652064 +5003507 +15792264 +3057467 +6826960 +5856660 +8013085 +6220752 +11947906 +2585809 +1634305 +5860625 +15694672 +14832784 +730801 +15570884 +7251211 +14404966 +5098553 +5227309 +13625858 +5213972 +10550520 +5014450 +8372560 +12596574 +7452543 +2623078 +4664448 +9092100 +10457371 +16588964 +16462557 +9877428 +13431109 +1764495 +9088402 +342856 +13959113 +3689644 +13166391 +5877105 +6867429 +4909975 +10881188 +4695286 +8840796 +10124915 +2223582 +15622747 +12094123 +708255 +13135990 +16711250 +2836548 +13420315 +11278399 +315080 +2880243 +3750170 +9080633 +9165214 +4662552 +1630018 +2373986 +12846651 +3198596 +4476288 +11170991 +12832529 +14994286 +3522917 +16238515 +443385 +12121322 +9392799 +4234839 +14720756 +12384209 +6410624 +16650961 +3340358 +5356824 +13764633 +16125285 +2743881 +4834028 +8601393 +2956961 +7627372 +4430837 +548887 +15840640 +10375393 +12666200 +15543585 +9471142 +12131653 +10108969 +1411524 +2184947 +722270 +1682188 +15087116 +5864407 +9538921 +2033875 +4758931 +5443181 +8907008 +14365679 +8987946 +4478695 +6642099 +13185139 +643552 +4164582 +3922599 +2380417 +1516882 +3237972 +3342404 +11694260 +15262718 +10156491 +15156740 +8961314 +1132430 +10308369 +13579189 +12468069 +6838065 +14820610 +16384342 +7068128 +11669376 +11734581 +11302302 +16715852 +3231917 +16674294 +16091563 +12021857 +8302945 +3912064 +3169573 +1852183 +12004638 +15692336 +5491459 +11993293 +10672850 +3653656 +1745341 +10129447 +5403080 +13317546 +10642258 +7635527 +15023467 +2205266 +13392827 +11002479 +12611784 +15129200 +2408639 +10395362 +10788668 +12689139 +15332863 +4626471 +4971913 +12627116 +1613078 +15386270 +3339022 +7259061 +8914245 +12132230 +2008049 +4616379 +12855757 +6727251 +8906690 +15723629 +1745762 +8900682 +10263126 +7632999 +9290136 +4240158 +13794177 +13932479 +5637634 +1818177 +12786629 +15010784 +8005098 +3706431 +14380318 +14686116 +7983656 +14159000 +7421358 +12200588 +4075605 +14420192 +11068264 +6663088 +13838091 +4064546 +12265844 +9015130 +3109172 +9077449 +544706 +14226032 +15303820 +5431896 +206840 +7093148 +9205393 +3555973 +13921988 +9464498 +6195932 +11327059 +7742119 +16225052 +2582434 +13252824 +284470 +11271458 +7740055 +16475248 +4571930 +412533 +12862178 +2909474 +4441399 +7648782 +4691437 +892648 +6549453 +5707864 +10575384 +9683865 +4209864 +177842 +10349833 +1525000 +3025404 +15734226 +15649102 +8409558 +1326244 +13399264 +1620332 +1710680 +12797501 +10647862 +10610452 +805306 +6653845 +1335149 +5961048 +465498 +1121999 +8521921 +10874372 +865823 +3639333 +2157879 +16766472 +7825455 +13775927 +8339858 +5118192 +3143765 +16187374 +8739864 +9566413 +12780744 +16299578 +15199003 +15535272 +11155687 +14979416 +275641 +14314709 +4813305 +13051059 +11804321 +10120126 +2757021 +1327121 +5891975 +1060017 +15234072 +16555356 +11108580 +7713342 +524871 +12632585 +9275065 +6509646 +13573987 +4981010 +1151895 +4342971 +16086329 +12609598 +3784886 +3787162 +13060602 +13721760 +6462387 +13115304 +12794885 +13403179 +5557699 +9381733 +10049172 +5936798 +9478475 +2671963 +15958113 +10583067 +8979277 +1278923 +1972154 +4233583 +14648581 +5040827 +10725571 +9125427 +8859304 +2761908 +16402112 +11986409 +16387776 +11858923 +2663335 +15429502 +374955 +4611758 +5420336 +3762362 +14168212 +2003955 +11464689 +15110597 +516855 +1029055 +14154483 +6652420 +5537982 +13423565 +4820101 +16214146 +14102359 +4378979 +6156003 +10101849 +12985500 +2320056 +8240236 +12853944 +9449721 +12273240 +16429840 +13346181 +14093901 +6325069 +15286428 +14105852 +7317244 +11514479 +11438441 +15834437 +11961883 +15039034 +1919963 +5287033 +5426971 +642059 +8061197 +8135536 +6127673 +12973322 +4720810 +12104004 +8150736 +12509300 +12670971 +13362339 +1128205 +1321200 +4132038 +7515070 +9076954 +4091315 +15804516 +12048882 +5433731 +6249211 +9734456 +1015385 +14521460 +1646102 +3908680 +5633461 +10187932 +10605729 +10971486 +16540307 +16595743 +14599964 +9092541 +3788023 +11847373 +2349322 +13756168 +12232026 +7569749 +7345889 +11445620 +6761107 +5759814 +1068300 +417027 +7634118 +7118356 +14439618 +1770706 +13667985 +11686596 +2463044 +8738094 +12392024 +189562 +590684 +7619672 +4252102 +14644995 +3256108 +10744445 +9181855 +5247608 +281507 +13634180 +14687045 +14237047 +3128237 +14084846 +1781889 +12003025 +10347043 +2288381 +8534713 +7611736 +8196874 +11815156 +12355579 +8935521 +14192945 +4192563 +13160479 +423088 +16288666 +16457543 +16654756 +16099313 +9354278 +10329409 +3362561 +14536558 +14147119 +1064348 +8807865 +13227390 +4983126 +11773248 +13082797 +6892350 +10945712 +14665791 +6138633 +6898005 +3402229 +13834438 +14339710 +13718038 +4460716 +11098130 +4295551 +14971085 +13365389 +2805721 +15282885 +6933820 +8735098 +8453085 +8016292 +9735698 +12308527 +8534536 +943185 +7940554 +16081590 +2955136 +10119129 +4278729 +13334557 +5643753 +198193 +2887502 +3952016 +7188714 +3112864 +6751482 +1181951 +11756728 +14011055 +4888748 +12880097 +6110829 +1778865 +2627856 +11426406 +8798546 +13717026 +16434900 +15263071 +5471574 +14064960 +7500618 +7827640 +13249864 +16118664 +6536303 +14644464 +4864841 +4871708 +11896966 +10919233 +6509135 +13607567 +243991 +14536062 +9982057 +2991900 +14149049 +527236 +4194738 +14887944 +16417347 +4516151 +7097266 +6322560 +9130217 +1521796 +12769054 +3813207 +14880624 +15056807 +6325918 +8996785 +1478165 +3357231 +7475059 +14852333 +11265721 +7419039 +3111023 +9112339 +9464542 +6084243 +2258900 +5932878 +3645028 +6302531 +8467772 +5110348 +2798407 +14955530 +3155140 +13445566 +10776469 +3763519 +9177123 +3504050 +7414139 +3630351 +7134321 +3981588 +306614 +9217926 +4590504 +7159116 +14170586 +16530400 +13486855 +4505802 +544168 +12713860 +11699899 +14790926 +10376262 +1616796 +3519274 +2687946 +10841976 +14071090 +239719 +5724605 +16110324 +2852363 +16668780 +6786576 +16716943 +976895 +14942098 +8890930 +16168618 +1810108 +8084713 +10824098 +12964880 +16573929 +14204901 +12305996 +16605127 +7423388 +11941583 +5958388 +5928572 +9661892 +16549960 +7242342 +6970086 +7840967 +10239499 +14917182 +5368113 +5898004 +3273450 +15575950 +6788994 +13948574 +13442513 +2376912 +10416617 +13677406 +5618270 diff --git a/src/day-22.lisp b/src/day-22.lisp @@ -0,0 +1,37 @@ +(defpackage #:aoc/day-22 + (:use #:cl #:aoc/utils) + (:export #:day-22)) +(in-package #:aoc/day-22) + +(defun calculate-secret-number (secret n sequences) + (loop with local-sequences = (make-hash-table :test #'equal) + with last-price = 0 + with history = nil + with price = 0 + repeat n + for i from 1 + do (setf secret (mod (logxor secret (* secret 64)) 16777216) + secret (mod (logxor secret (floor secret 32)) 16777216) + secret (mod (logxor secret (* secret 2048)) 16777216) + price (mod secret 10)) + (when (> i 1) + (push (- price last-price) history)) + when (> i 4) + do (unless (gethash history local-sequences) + (incf (gethash history sequences 0) price) + (setf (gethash history local-sequences) t)) + (setf history (take 3 history)) + do (setf last-price price) + finally (return secret))) + +(defun best-sequence (sequences) + (loop for bananas being the hash-values of sequences + maximize bananas)) + +(defun day-22 (input) + (loop with sequences = (make-hash-table :test #'equal) + for line = (read-line input nil) + until (null line) + for initial-secret = (parse-integer line) + sum (calculate-secret-number initial-secret 2000 sequences) into task-1 + finally (return (values task-1 (best-sequence sequences))))) diff --git a/t/day-22.lisp b/t/day-22.lisp @@ -0,0 +1,15 @@ +(defpackage #:aoc-test/day-22 + (:use #:cl #:lisp-unit2) + (:import-from #:aoc/day-22)) +(in-package #:aoc-test/day-22) + +(define-test test-day-22 + () + (assert= 37327623 (aoc:run-day 22 "1 +10 +100 +2024")) + (assert= 23 (nth-value 1 (aoc:run-day 22 "1 +2 +3 +2024"))))