From a529516bab2ca0075ad52e2d64cee0088128c0d9 Mon Sep 17 00:00:00 2001 From: Gareth Date: Wed, 22 Jan 2025 22:31:04 +0000 Subject: [PATCH] Day22 --- 2024/gareth/day22/day22.go | 112 +++ 2024/gareth/day22/day22_test.go | 23 + 2024/gareth/day22/input.txt | 1629 +++++++++++++++++++++++++++++++ 2024/gareth/main.go | 8 +- 4 files changed, 1768 insertions(+), 4 deletions(-) create mode 100644 2024/gareth/day22/day22.go create mode 100644 2024/gareth/day22/day22_test.go create mode 100644 2024/gareth/day22/input.txt diff --git a/2024/gareth/day22/day22.go b/2024/gareth/day22/day22.go new file mode 100644 index 0000000..2054e58 --- /dev/null +++ b/2024/gareth/day22/day22.go @@ -0,0 +1,112 @@ +package day22 + +import ( + "fmt" + "strconv" + "strings" +) + +type Queue struct { + data []int + size int +} + +func NewQueue(size int) *Queue { + return &Queue{ + data: make([]int, 0, size), + size: size, + } +} + +func (queue *Queue) Push(value int) { + if len(queue.data) == queue.size { + queue.data = queue.data[1:] + } + queue.data = append(queue.data, value) +} + +func (queue *Queue) Get() []int { + return queue.data +} + +func (queue *Queue) String() string { + strs := make([]string, len(queue.data)) + for i, val := range queue.data { + strs[i] = fmt.Sprintf("%d", val) + } + return strings.Join(strs, ", ") +} + +func Part1(input string) int { + secretNumbers := parseInput(input) + + total := 0 + for _, number := range secretNumbers { + for i := 0; i < 2000; i++ { + number = getNextNumber(number) + } + total += number + } + return total +} + +func Part2(input string) int { + secretNumbers := parseInput(input) + memo := make(map[string]int) + + for _, number := range secretNumbers { + seen := make(map[string]bool) + queue := NewQueue(4) + prevNumber := secretNumbers[0] % 10 + + for i := 0; i < 2000; i++ { + singleNumber := number % 10 + cost := singleNumber - prevNumber + queue.Push(cost) + if i > 3 { + if _, ok := seen[queue.String()]; !ok { + memo[queue.String()] += singleNumber + seen[queue.String()] = true + } + } + prevNumber = singleNumber + number = getNextNumber(number) + } + } + return maxMapValue(memo) +} + +func parseInput(input string) []int { + parts := strings.Split(input, "\n") + secretNumbers := []int{} + for _, part := range parts { + num, _ := strconv.Atoi(part) + secretNumbers = append(secretNumbers, num) + } + + return secretNumbers +} + +func getNextNumber(number int) int { + step1 := (number * 64) ^ number + step1 = step1 % 16777216 + + step2 := (step1 / 32) ^ step1 + step2 = step2 % 16777216 + + step3 := (step2 * 2048) ^ step2 + step3 = step3 % 16777216 + + return step3 +} + +func maxMapValue(memo map[string]int) int { + maxValue := 0 + + for _, value := range memo { + if value > maxValue { + maxValue = value + } + } + return maxValue +} diff --git a/2024/gareth/day22/day22_test.go b/2024/gareth/day22/day22_test.go new file mode 100644 index 0000000..8a4d0d8 --- /dev/null +++ b/2024/gareth/day22/day22_test.go @@ -0,0 +1,23 @@ +package day22 + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPart1(t *testing.T) { + r := Part1(`1 +10 +100 +2024`) + assert.Equal(t, 37327623, r) +} + +func TestPart2(t *testing.T) { + r := Part2(`1 +2 +3 +2024`) + assert.Equal(t, 23, r) +} diff --git a/2024/gareth/day22/input.txt b/2024/gareth/day22/input.txt new file mode 100644 index 0000000..5263644 --- /dev/null +++ b/2024/gareth/day22/input.txt @@ -0,0 +1,1629 @@ +3163651 +11534532 +980758 +1430559 +960757 +9525786 +5122707 +2330307 +16689657 +15006246 +8571540 +1803539 +1435881 +12882872 +2208594 +6874020 +1609012 +16306777 +12760062 +13024762 +2882520 +3976492 +8657850 +15785227 +2334352 +16088551 +3702420 +686358 +11898624 +4239409 +14170816 +8641352 +9194215 +4518076 +12393960 +12018934 +6256606 +8947234 +14595444 +1699992 +13837564 +16732500 +7329590 +15657505 +14800508 +10867114 +11863249 +15019534 +8151762 +8684347 +2025391 +2090484 +2400481 +830982 +1142869 +1096806 +9368481 +15795130 +6260255 +15744408 +5025898 +2855297 +12568449 +3130474 +6519025 +2360619 +15069695 +7698766 +15346040 +15931991 +6107160 +2461659 +6661385 +13326045 +525671 +14013923 +2049346 +1164983 +15456828 +10609021 +14690370 +7665906 +750289 +14591888 +10167598 +5800434 +3266369 +16468036 +6581855 +11418909 +6191933 +6326040 +16606865 +5081771 +4369243 +9250618 +3536004 +1246907 +6323364 +4034426 +3714343 +16350994 +1408498 +15618338 +7272429 +10296870 +16727096 +9433065 +543269 +1725927 +9439186 +13855843 +13214961 +9149620 +4735856 +13976428 +6915398 +4437102 +14453238 +1825107 +13747595 +997446 +5179999 +8879348 +10345638 +14248705 +11219744 +559707 +3356131 +2860777 +1375560 +14730264 +14602186 +7990118 +4679180 +3023635 +1907979 +10122320 +3649268 +14179437 +3373493 +16336284 +7818601 +12110036 +4919167 +11957435 +9026753 +6790809 +13262174 +8364364 +5117535 +3822805 +16504979 +6586908 +1051882 +6760473 +5003121 +10896252 +3737047 +16059733 +6270799 +7365098 +336643 +3519452 +14591906 +9566665 +15427650 +2470128 +4133628 +496544 +3623453 +3115941 +7435142 +8994229 +16193318 +1537170 +8942295 +12278071 +1197981 +15019873 +10355765 +14257236 +2161432 +7707180 +1907746 +2591904 +7482680 +10333525 +13782953 +15484910 +15764595 +5405605 +10203426 +3065503 +9876493 +3879770 +14556711 +13370053 +5562493 +9653023 +8233769 +16252146 +2969187 +4890378 +12667790 +15420351 +5327930 +12476184 +6672713 +15601458 +13488382 +4014103 +13517810 +10485500 +9638310 +12953193 +14845124 +5159776 +1456274 +3186081 +5630551 +9418281 +12642195 +11523529 +822252 +11019387 +11379520 +15391958 +14850021 +6372770 +2127857 +8193471 +928616 +292639 +12917157 +13460647 +14078333 +519455 +9120674 +10276264 +13043114 +7883140 +9057913 +7374817 +7581952 +6450184 +5949112 +11431383 +10612609 +14685660 +6561580 +12475500 +13291307 +5686063 +4551486 +16470091 +6221965 +9805681 +430989 +1340428 +16104025 +1550096 +1041493 +6229288 +6448535 +4535402 +7544301 +11266148 +11515419 +2793279 +5715675 +12858713 +156271 +10784695 +10199228 +7674246 +1941223 +9705790 +9248864 +9595780 +11339892 +2859000 +4170272 +12098537 +13589725 +11112031 +13752475 +8177870 +8097366 +15116769 +6080892 +12525569 +4083664 +4500949 +3142948 +4665475 +6827320 +6757872 +10028646 +12491600 +12226212 +8058298 +6236839 +527132 +14487241 +12913402 +10734601 +14133304 +13873964 +9129745 +2986576 +3139281 +8630125 +2512995 +13741935 +16576345 +4406149 +5053082 +3911090 +10072345 +716573 +6218882 +14977706 +12810952 +149821 +6923641 +14413527 +5284428 +12075790 +869647 +2624096 +1577201 +1226279 +10237574 +9897410 +9210320 +13246909 +1784073 +13844146 +12657250 +5117480 +5754441 +12886037 +12316412 +3594889 +8969476 +1010676 +7762775 +3689722 +9457014 +12886820 +15298595 +6263138 +7234641 +8448829 +13989716 +2547225 +15269618 +2044960 +454372 +5574328 +2718421 +1601095 +8344204 +4328666 +1211427 +269730 +15953480 +10845001 +4949274 +3894679 +12952176 +13058538 +16354227 +10767701 +3148972 +9500369 +832875 +5237609 +2329639 +12122925 +8909929 +14565354 +342302 +4931682 +15294073 +4327786 +12262721 +9947709 +6375245 +14433573 +2697501 +6053455 +13045099 +5845663 +11782261 +2463102 +14572998 +7684402 +7842138 +11098996 +15451850 +10407580 +1291682 +2125583 +8171979 +3103753 +2468222 +8701652 +5325621 +11114079 +2013694 +14345350 +11980784 +797455 +9721992 +4500026 +277058 +11974377 +989858 +2402753 +1502305 +7331008 +5917890 +4793343 +12495744 +10547459 +13744470 +5238967 +15211907 +13035916 +9635819 +16496503 +9676552 +6150446 +7010512 +9674654 +6154345 +12998841 +15609232 +5357377 +16734336 +6438456 +1409872 +10413628 +12494018 +8782702 +3798832 +8008705 +1116412 +8687735 +1453003 +4551379 +771954 +381880 +3135270 +2765220 +13659575 +11069215 +7888768 +9090667 +9264251 +948409 +11849978 +6311117 +15985408 +13890744 +8063804 +12077029 +14803564 +13857977 +4772622 +15786377 +9335887 +843560 +11695389 +2589879 +8868599 +2344298 +10703687 +4393954 +10707947 +12419916 +3430686 +6360473 +2715109 +6402559 +2463152 +6222440 +11994642 +5320618 +2004021 +14789912 +16536842 +4163176 +7362988 +8417879 +9413896 +13980961 +251741 +2069848 +12661096 +6172262 +2961339 +4789367 +3187964 +13897771 +8730940 +12366312 +6615862 +14925487 +8089449 +7043386 +7319604 +9420958 +7015125 +16058889 +739304 +8062441 +16362085 +5710428 +10425695 +2959719 +2167027 +12771129 +13605612 +14301872 +11702104 +6793079 +6966158 +10667336 +5386661 +2708326 +11099982 +12699488 +5600386 +9304777 +7999652 +7610632 +13615781 +2879909 +12319200 +13307884 +9123756 +15428276 +7653574 +11476758 +7533268 +3896260 +14773532 +6936945 +15844983 +13403159 +9414306 +16452637 +345791 +11177623 +5726578 +374470 +8746900 +9298215 +5617152 +15753089 +10079576 +9596506 +2641439 +362693 +14377727 +8275924 +3220366 +4712005 +14389183 +14670855 +10772541 +6405260 +11476138 +8012387 +16715339 +1370704 +15125613 +11387896 +16598855 +3439979 +7772878 +627791 +4116610 +9022356 +7064439 +15074737 +2836392 +10062780 +13280834 +1086747 +4390843 +7877649 +9930573 +2888761 +6872462 +3528050 +8583433 +13144601 +12608668 +1520427 +3539631 +7077317 +185255 +7948409 +730252 +8052705 +15981839 +7938135 +782898 +1569075 +6879994 +15835846 +10518166 +10238841 +13597877 +7563410 +4283671 +15091470 +7842433 +12533567 +9021846 +8356569 +2314487 +8904103 +984769 +4092822 +5887670 +7539257 +2085443 +4432867 +15265715 +16646589 +6475677 +12850831 +10545849 +11199440 +16544281 +14215272 +14837998 +2759968 +10209726 +7904584 +2528834 +13484490 +2902986 +2719070 +611694 +5984213 +9927433 +10459806 +8879367 +7134799 +3501099 +14432983 +13864368 +2753500 +10007586 +2889847 +10624447 +338978 +11157413 +5720549 +1087021 +870579 +9404677 +15054189 +13590645 +664737 +6898111 +4082930 +7143234 +9679879 +4392322 +7561509 +9307472 +5689163 +4551105 +10299071 +6543556 +222994 +12686702 +8190229 +501380 +6790652 +8359175 +2843544 +10349206 +4639661 +5440982 +11986730 +16722319 +746657 +8066311 +3710932 +12494141 +16023420 +11342339 +4010774 +15684982 +9129645 +11426978 +2231887 +7712458 +2668359 +1162385 +10106545 +4640038 +11485169 +12991929 +8157422 +10457635 +13445102 +1823342 +6706504 +8840876 +4295203 +3321260 +2538692 +11954166 +6043127 +13640464 +8631592 +8044913 +5131226 +1911983 +5692163 +14307936 +8704052 +8822430 +6228561 +9017114 +11938079 +16318883 +4800328 +10573212 +16510334 +10049642 +3735722 +5540933 +1923431 +5593443 +10408806 +3307958 +3221755 +420225 +5555078 +12533713 +13058604 +8697378 +10412103 +5845672 +3501042 +15916178 +4161512 +16187590 +10617047 +5349597 +9671423 +16240075 +2815905 +4860433 +10391119 +5827754 +16409159 +5129332 +1918132 +10491149 +12547360 +1792202 +405628 +13582680 +6279588 +14647152 +16341870 +2652627 +8582325 +10523835 +16219896 +780035 +7521217 +8392451 +15843694 +14936825 +3013039 +916352 +213443 +11247788 +6623172 +12041045 +12027950 +6879952 +3461939 +555805 +15138566 +14183055 +5144921 +14082716 +6443931 +12856222 +7181817 +4058480 +5081634 +11687901 +7650333 +943643 +14983599 +16194151 +12586620 +11671579 +15781114 +10402377 +4604814 +14733705 +2419200 +9359768 +8386198 +14214699 +6382797 +15042478 +9643568 +9804526 +11069542 +7130706 +3382525 +10948176 +7207823 +13552501 +7890472 +429703 +14101836 +6960359 +5374120 +15427055 +656896 +4422359 +2475269 +253880 +6374868 +12407395 +11478419 +4480723 +1556086 +408769 +11539451 +454037 +1733980 +1890948 +1842139 +13671130 +11129777 +12296048 +16422211 +4493082 +14815627 +1725225 +382382 +13573716 +11424384 +5099386 +10639073 +14921874 +3217788 +10866568 +13794329 +12285814 +6803645 +10235251 +15907466 +1716541 +14587940 +6455076 +15758874 +7745174 +5230407 +5183702 +14850538 +5785025 +7850389 +2729460 +8440064 +4980888 +14133386 +1044567 +369150 +12986744 +11391534 +6837397 +777363 +4506170 +4468810 +11837789 +9935816 +4234775 +15670881 +13084939 +8782083 +7035039 +4679970 +13969851 +14771215 +15900369 +932125 +3375590 +8149000 +717029 +8217482 +8661092 +2183024 +12829034 +10423948 +8412998 +2946639 +16289281 +11893190 +7471929 +9479029 +329048 +10233893 +15773351 +538001 +3744195 +371603 +3686822 +4303092 +738934 +11106000 +2486236 +607073 +16102203 +2153463 +4490557 +9414505 +7941716 +1947413 +6619946 +16579627 +2003545 +16261893 +13894380 +8639636 +11687096 +5006094 +16548330 +398679 +3425805 +11486460 +6916359 +7859981 +2377038 +12614952 +14173356 +2391326 +1621939 +16428947 +5437397 +14341642 +8803876 +2115158 +11545388 +2599413 +12676122 +14818778 +5872568 +3210266 +3710960 +1124815 +384036 +10901282 +10418159 +1979003 +16716214 +12848650 +327345 +5492721 +8094799 +5758823 +10357465 +14219210 +9062523 +1872566 +9488060 +3039598 +6462874 +10619455 +4381206 +11093105 +13093259 +6661115 +12111801 +12618583 +14776544 +13349818 +5652700 +4424479 +10600393 +14429326 +11115680 +8768540 +13936167 +14498394 +15862358 +6638347 +8333831 +7361005 +9747774 +457004 +5400780 +14094857 +10569337 +10786480 +16011713 +9755942 +2427739 +3130442 +11937405 +11878023 +4120719 +1914269 +9893817 +12947645 +10427127 +937767 +6959486 +3656552 +3423163 +16078252 +6205848 +12346713 +14555839 +8754037 +9924627 +6331144 +5330746 +10006414 +15183175 +2007678 +12941118 +11504888 +14562448 +3021184 +3664319 +12911089 +11368485 +16233498 +15653067 +13216450 +5511906 +295949 +14614694 +5641773 +7111113 +3045643 +10914263 +5575719 +3142168 +16419053 +3997014 +6523091 +3791744 +8950380 +915606 +13551000 +4495806 +4589350 +5654672 +9371632 +1257263 +1832671 +8969355 +12284135 +10243211 +8956229 +9289994 +13260901 +5312959 +3651724 +3247644 +16409649 +11211586 +5241199 +14862352 +8561465 +1421005 +3115036 +15618970 +5997028 +2034744 +4940008 +3136636 +4361007 +10179777 +14944858 +8768736 +2788387 +11392351 +7358577 +4846048 +16444044 +7447028 +1216974 +7486476 +16100613 +4569179 +12534397 +16654097 +15136865 +10812702 +1390836 +15216478 +3554528 +7158129 +13552581 +8445180 +8684601 +6986921 +1095011 +13436367 +6115118 +4348875 +573992 +12180478 +11286144 +1944100 +1542030 +9366880 +3376689 +1462015 +15021409 +10056597 +13745033 +3541107 +2516577 +5263291 +7541999 +8670010 +7710846 +14474669 +7085703 +9235701 +12054911 +9971495 +10727075 +16589574 +10960551 +7261282 +4942170 +4117824 +14012491 +1534817 +15886903 +11174673 +2138406 +10647156 +2219398 +9530545 +9531272 +10595175 +3233781 +2423265 +9547816 +783430 +11979320 +10530400 +953029 +12975801 +9416436 +11711205 +13189954 +9811113 +7283374 +13564358 +5872208 +10468900 +11145075 +10375626 +1204770 +14544460 +8292325 +12577183 +9181679 +11266469 +5922480 +10718339 +11563391 +8405005 +16021473 +3507792 +14444571 +9356896 +6568022 +14287855 +15520687 +2043906 +312427 +2156142 +10664182 +7411025 +10344747 +2387086 +1208298 +10806923 +2904755 +4487568 +8274873 +13060260 +2735630 +12212772 +11171511 +15491107 +16479377 +14881578 +3894492 +13718371 +9894370 +11662019 +7703925 +9330264 +12184550 +7427200 +5585576 +463968 +5995661 +15101734 +1527438 +2858399 +14276567 +10178171 +7069052 +520970 +11021462 +7905220 +1687351 +14959456 +9202524 +14032249 +5996663 +6949410 +16698508 +15314818 +7143252 +464122 +10705892 +4250311 +2355910 +12857827 +208075 +14945928 +16452454 +1299988 +1209986 +12814585 +4261208 +417963 +12984766 +15388931 +13102090 +11348482 +13351276 +8886721 +10269739 +601779 +14360121 +8970078 +7217669 +11239818 +6926131 +15488377 +12934640 +12701395 +5449989 +1906377 +13467991 +12450779 +15833231 +15713804 +11233355 +1672187 +11349052 +15526536 +2490700 +14135033 +7549202 +4247648 +12892763 +11173962 +5133462 +3160045 +12864275 +5081452 +9341616 +3334396 +1536727 +7090282 +1138231 +5427309 +3474377 +4385930 +16096095 +10811914 +4467309 +14957362 +1131554 +7257791 +9171584 +10697749 +16033734 +15534696 +8973994 +6026009 +3198706 +9141607 +7157302 +4124037 +6600000 +2965229 +3206435 +15441037 +15005415 +4208290 +2147133 +1384138 +4431135 +13334682 +14123545 +14008750 +15987951 +7845666 +217868 +3107278 +14826116 +7551952 +11536726 +14078345 +3683780 +3538550 +10343669 +8292226 +555939 +6833745 +10382209 +8158798 +5319094 +12405149 +8077766 +6251207 +10542814 +4162332 +1764766 +11842461 +2630467 +16324004 +16283582 +6256891 +13570949 +11488484 +7226372 +13734760 +5840408 +13298107 +4618214 +13816699 +11146650 +10148167 +16602481 +5287298 +6315926 +11319738 +13697339 +9274074 +14756730 +10174577 +1734280 +7838557 +1206642 +4678508 +8264107 +13128237 +11709380 +16047701 +14823798 +15027974 +2731671 +8858275 +1874025 +16672289 +13066585 +6669358 +14149861 +9419255 +1761756 +6957426 +11810943 +14919993 +9814735 +629359 +9359640 +10155440 +232880 +14248827 +9379377 +3294203 +8323645 +10661070 +4445521 +13094339 +10283862 +16440512 +13438241 +5107022 +8206764 +9233305 +592713 +12415044 +5679051 +12604908 +512170 +1350888 +6793243 +14952190 +7033342 +11083578 +3304435 +14475623 +4361091 +3217154 +15602271 +266871 +10790014 +2294553 +234799 +1496635 +4769552 +9496117 +5040848 +13039611 +9274619 +2143176 +9736941 +6264614 +5779046 +6727097 +3596810 +1178673 +13742055 +2405197 +13292890 +8693202 +14738561 +1506264 +1962669 +9539133 +2490944 +7072419 +6496451 +13716363 +2250916 +4215274 +14801264 +4106090 +7285216 +1916615 +16281811 +13130361 +1007457 +11482265 +3160685 +13375552 +5788154 +14649735 +4152893 +818096 +5646910 +11359419 +5600238 +1028693 +14453135 +5875487 +11481328 +7675929 +13990785 +10313372 +2641587 +7821151 +335115 +2416443 +684151 +13794189 +5252885 +4737774 +9853680 +13098970 +2918346 +14660944 +12639130 +13628189 +13800701 +8088127 +14211320 +12149961 +15531463 +14426619 +1587166 +6822117 +13208937 +15415601 +15502299 +6275548 +16177448 +9329741 +1204485 +10858855 +5642699 +5638480 +12698605 +4267665 +13398757 +15904689 +9136161 +10112968 +2472451 +15906750 +15552112 +5699214 +15079760 +6119524 +12740959 +1365988 +13668362 +5179546 +15517537 +10515152 +4059815 +14636556 +4261663 +2926253 +4749346 +3984211 +16583079 +9629415 +14460288 +14494086 +12857466 +12010157 +12994311 +11184589 +6564242 +7159524 +13671484 +10897514 +15270095 +6834218 +7445270 +8483210 +8488191 +7058475 +5153864 +3614296 +8527216 +12830157 +10342644 +6734781 +7865342 +14678216 +13075805 +7727516 +5570072 +3174459 +8634267 +12740222 +3187793 +7774859 +4782213 +2926120 +12518453 +14480530 +1827682 +11267830 +9296300 +4380795 +2826780 +1249157 +15871147 +15221543 +10588904 +11872719 +4533257 +14454498 +2252169 +626610 +15499055 +13233538 +6667370 +13534379 +3525864 +6797197 +4965614 +8054413 +1713612 +631802 +2915703 +8112382 +8821734 +3689856 +14510725 +7068357 +8015546 +5472479 +13544077 +10532352 +11289208 +16407733 +15709423 +7740141 +9871896 +2891933 +7147396 +2258207 +6336045 +3674140 +12172069 +5124790 +15477932 +14780360 +13423961 +4489635 +2900050 \ No newline at end of file diff --git a/2024/gareth/main.go b/2024/gareth/main.go index 4173aa4..3f57a47 100644 --- a/2024/gareth/main.go +++ b/2024/gareth/main.go @@ -1,7 +1,7 @@ package main import ( - "aoc2024/day21" + "aoc2024/day22" "fmt" "os" "time" @@ -9,9 +9,9 @@ import ( func main() { start := time.Now() - data, _ := os.ReadFile("day21/input.txt") - fmt.Printf("part 1: %d\n", day21.Part1(string(data))) - fmt.Printf("part 2: %d\n", day21.Part2(string(data))) + data, _ := os.ReadFile("day22/input.txt") + fmt.Printf("part 1: %d\n", day22.Part1(string(data))) + fmt.Printf("part 2: %d\n", day22.Part2(string(data))) elapsed := time.Since(start) fmt.Printf("Execution time: %s\n", elapsed) }