Monkey Market

Advent of Code 2024 [Day 22]

22-12-2024

As you’re all teleported deep into the jungle, a monkey steals The Historians’ device! You’ll need to get it back while The Historians are looking for the Chief.

The monkey that stole the device seems willing to trade it, but only in exchange for an absurd number of bananas. Your only option is to buy bananas on the Monkey Exchange Market.

You aren’t sure how the Monkey Exchange Market works, but one of The Historians senses trouble and comes over to help. Apparently, they’ve been studying these monkeys for a while and have deciphered their secrets.

Today, the Market is full of monkeys buying good hiding spots. Fortunately, because of the time you recently spent in this jungle, you know lots of good hiding spots you can sell! If you sell enough hiding spots, you should be able to get enough bananas to buy the device back.

On the Market, the buyers seem to use random prices, but their prices are actually only pseudorandom! If you know the secret of how they pick their prices, you can wait for the perfect time to sell.

The part about secrets is literal, the Historian explains. Each buyer produces a pseudorandom sequence of secret numbers where each secret is derived from the previous.

In particular, each buyer’s secret number evolves into the next secret number in the sequence via the following process:

Each step of the above process involves mixing and pruning:

After this process completes, the buyer is left with the next secret number in the sequence. The buyer can repeat this process as many times as necessary to produce more secret numbers.

So, if a buyer had a secret number of 123, that buyer’s next ten secret numbers would be:

15887950
16495136
527345
704524
1553684
12683156
11100544
12249484
7753432
5908254

Each buyer uses their own secret number when choosing their price, so it’s important to be able to predict the sequence of secret numbers for each buyer. Fortunately, the Historian’s research has uncovered the initial secret number of each buyer (your puzzle input). For example:

1
10
100
2024

This list describes the initial secret number of four different secret-hiding-spot-buyers on the Monkey Exchange Market. If you can simulate secret numbers from each buyer, you’ll be able to predict all of their future prices.

In a single day, buyers each have time to generate 2000 new secret numbers. In this example, for each buyer, their initial secret number and the 2000th new secret number they would generate are:

1: 8685429
10: 4700978
100: 15273692
2024: 8667524

Adding up the 2000th new secret number for each buyer produces 37327623.

For each buyer, simulate the creation of 2000 new secret numbers. What is the sum of the 2000th secret number generated by each buyer?

function generateNextSecret(secret: number): number {
  secret = secret & 0xFFFFFF;
  secret = (secret ^ ((secret * 64) & 0xFFFFFF)) & 0xFFFFFF;
  secret = (secret ^ ((secret >>> 5) & 0xFFFFFF)) & 0xFFFFFF;
  secret = (secret ^ ((secret * 2048) & 0xFFFFFF)) & 0xFFFFFF;
  return secret;
}

function solve(input: string): number {
  const initialSecrets = input
    .trim()
    .split("\n")
    .map(Number);
  const secretsAt2000: number[] = [];
  for (const initialSecret of initialSecrets) {
    let current = initialSecret;
    for (let i = 0; i < 2000; i++) {
      current = generateNextSecret(current);
    }
    secretsAt2000.push(current);
  }
  return secretsAt2000.reduce((sum, num) => sum + num, 0);
}

async function main() {
  const input =  await Deno.readTextFile("input.txt");
  console.log(solve(input));
}

main().catch(console.error);

Of course, the secret numbers aren’t the prices each buyer is offering! That would be ridiculous. Instead, the prices the buyer offers are just the ones digit of each of their secret numbers.

So, if a buyer starts with a secret number of 123, that buyer’s first ten prices would be:

3 (from 123)
0 (from 15887950)
6 (from 16495136)
5 (etc.)
4
4
6
4
4
2

This price is the number of bananas that buyer is offering in exchange for your information about a new hiding spot. However, you still don’t speak monkey, so you can’t negotiate with the buyers directly. The Historian speaks a little, but not enough to negotiate; instead, he can ask another monkey to negotiate on your behalf.

Unfortunately, the monkey only knows how to decide when to sell by looking at the changes in price. Specifically, the monkey will only look for a specific sequence of four consecutive changes in price, then immediately sell when it sees that sequence.

So, if a buyer starts with a secret number of 123, that buyer’s first ten secret numbers, prices, and the associated changes would be:

     123: 3 
15887950: 0 (-3)
16495136: 6 (6)
  527345: 5 (-1)
  704524: 4 (-1)
 1553684: 4 (0)
12683156: 6 (2)
11100544: 4 (-2)
12249484: 4 (0)
 7753432: 2 (-2)

Note that the first price has no associated change because there was no previous price to compare it with.

In this short example, within just these first few prices, the highest price will be 6, so it would be nice to give the monkey instructions that would make it sell at that time. The first 6 occurs after only two changes, so there’s no way to instruct the monkey to sell then, but the second 6 occurs after the changes -1,-1,0,2. So, if you gave the monkey that sequence of changes, it would wait until the first time it sees that sequence and then immediately sell your hiding spot information at the current price, winning you 6 bananas.

Each buyer only wants to buy one hiding spot, so after the hiding spot is sold, the monkey will move on to the next buyer. If the monkey never hears that sequence of price changes from a buyer, the monkey will never sell, and will instead just move on to the next buyer.

Worse, you can only give the monkey a single sequence of four price changes to look for. You can’t change the sequence between buyers.

You’re going to need as many bananas as possible, so you’ll need to determine which sequence of four price changes will cause the monkey to get you the most bananas overall. Each buyer is going to generate 2000 secret numbers after their initial secret number, so, for each buyer, you’ll have 2000 price changes in which your sequence can occur.

Suppose the initial secret number of each buyer is:

1
2
3
2024

There are many sequences of four price changes you could tell the monkey, but for these four buyers, the sequence that will get you the most bananas is -2,1,-1,3. Using that sequence, the monkey will make the following sales:

So, by asking the monkey to sell the first time each buyer’s prices go down 2, then up 1, then down 1, then up 3, you would get 23 (7 + 7 + 9) bananas!

Figure out the best sequence to tell the monkey so that by looking for that same sequence of changes in every buyer’s future prices, you get the most bananas in total. What is the most bananas you can get?

function generateNextSecret(secret: number): number {
  secret = secret & 0xFFFFFF;
  secret = (secret ^ ((secret * 64) & 0xFFFFFF)) & 0xFFFFFF;
  secret = (secret ^ ((secret >>> 5) & 0xFFFFFF)) & 0xFFFFFF;
  secret = (secret ^ ((secret * 2048) & 0xFFFFFF)) & 0xFFFFFF;
  return secret;
}

function generatePricesAndChanges(initialSecret: number): {prices: Int8Array, changes: Int8Array} {
  const prices = new Int8Array(2001);
  let current = initialSecret;
  prices[0] = current % 10;

  for (let i = 1; i < 2001; i++) {
    current = generateNextSecret(current);
    prices[i] = current % 10;
  }

  const changes = new Int8Array(2000);
  for (let i = 0; i < 2000; i++) {
    changes[i] = prices[i + 1] - prices[i];
  }

  return { prices, changes };
}

function findBestPattern(buyers: number[]): { pattern: number[], maxBananas: number } {
  const buyerData = buyers.map(b => generatePricesAndChanges(b));
  let maxBananas = 0;
  let bestPattern = [0, 0, 0, 0];

  const ranges = [-3, -2, -1, 0, 1, 2, 3];
  
  for (const a of ranges) {
    for (const b of ranges) {
      for (const c of ranges) {
        for (const d of ranges) {
          const pattern = [a, b, c, d];
          let totalBananas = 0;

          for (const {prices, changes} of buyerData) {
            for (let i = 0; i <= changes.length - 4; i++) {
              if (changes[i] === pattern[0] && 
                  changes[i + 1] === pattern[1] && 
                  changes[i + 2] === pattern[2] && 
                  changes[i + 3] === pattern[3]) {
                totalBananas += prices[i + 4];
                break;
              }
            }
          }

          if (totalBananas > maxBananas) {
            maxBananas = totalBananas;
            bestPattern = pattern;
          }
        }
      }
    }
  }

  return { pattern: bestPattern, maxBananas };
}

async function main() {
  const input = await Deno.readTextFile("input.txt");
  const buyers = input.trim().split('\n').map(Number);
  const { maxBananas } = findBestPattern(buyers);
  console.log(maxBananas);
}

main().catch(console.error);
Click to show the input
12345474
5973536
2862795
10661632
3375595
6010645
16498246
4478227
4795745
13303096
7967226
16579243
13557043
1170734
604319
2985993
4022529
10283144
13719853
2935006
7501136
16220610
887417
8253676
8185442
14399069
10677455
388612
10924279
8176477
3878345
1412036
386597
10570626
14349888
6401782
15202488
7997770
3730745
13337768
1344114
8559223
8027433
13183652
10841427
10936647
4627970
11534497
11479120
16478695
9077191
9173234
4949796
6111936
6246015
10707591
8321437
9968987
8090259
7872949
1685374
3175761
6337455
9295708
10918437
4113552
8204118
9673717
12051541
11428265
2108272
10525499
6808084
7411542
13111056
2147177
10143253
10177926
3417251
914783
8167240
2762095
8262092
2924370
16174843
1895659
2828792
12824407
14001834
10896207
1510070
13071261
13783760
12078157
13117344
8896740
2489891
13340212
3613331
10609297
14624100
7626955
16109473
9572259
13360979
10290042
1611267
7791306
3265955
15795510
16464899
10254069
4373142
15872150
9701001
4913882
10422940
6542341
11803037
14799431
9976205
13779629
6428903
13734232
8542047
15837152
8044976
489789
14714789
11185705
15822507
13086119
14621191
7116289
10794004
7111366
7069394
5228610
15682699
3162947
7449036
4517623
9035573
11483574
6061594
12877271
5862003
11341531
16594810
6437915
3477145
5993179
16566002
13227163
12635595
3418646
5460786
13191494
1946660
15460911
4277801
9639143
6472179
3262507
13437373
15340547
7932902
8090149
4449388
6052918
3786345
6097735
13564773
3797736
16012704
955812
6971901
5433762
9833890
16158688
10595734
13594937
16091846
13193908
8689860
13416285
3918588
1648867
4958273
144526
13734703
11454839
11847499
16115085
5145061
2708723
9940798
1952668
13141567
7610472
5856968
14425199
2984848
3945106
2004899
3571415
15598005
8156282
16152017
5135680
7582800
5580411
5625943
5711123
9898731
9793829
5576913
965311
5403908
132344
16254164
7509964
5262002
5054932
2373850
11477966
10936474
14703744
12247127
5155921
15384467
4305898
7205116
13945864
3465502
14908781
1288054
13471849
10632624
13186493
12730796
11325385
10670931
16386068
4164968
5444269
14171895
10478774
6568286
3961474
389974
16459782
15612380
5093443
11313649
872976
3000568
7981022
15294533
5189841
15225797
13208340
590042
3640135
11745831
4635013
2998363
11341312
14603495
7488839
9586255
14387391
2712897
16395528
1360536
16639407
3080591
7596047
9506875
10185399
14375880
12422857
7986766
13920893
9727824
13755790
6966613
8356366
14238771
12335196
4637868
9938728
2000148
14274282
4345039
2803686
301834
2886208
9960373
12946331
5721714
14086476
15414163
4734133
3011840
3613291
11472290
16658624
10247059
12219881
3175850
8694984
4306207
1563267
13605674
12035552
12826249
881362
3378725
4820435
2355620
2107968
1012395
10037211
11056760
6546332
12237610
15470212
1440169
15215577
3248570
8642563
2533477
10533551
13959032
15140539
10982705
12374511
10398211
2405873
14411021
5514565
6231569
12663912
14233791
16244697
10315271
13262090
12384925
374243
14114883
2844311
14593288
7005894
15941035
168986
10948776
10252895
15802845
11566529
3572451
10775454
7220758
15043262
10263606
12631087
3513014
16596454
1069839
12872458
10099673
15896047
16505163
13349680
3913812
6767742
14078193
8663704
9570783
3897982
15027902
16059511
210407
13953487
9387742
3733337
9020191
8323524
7069888
6643841
15993935
13425328
289295
832502
7002254
13535839
9020903
1944898
7278453
5737804
6831076
14657945
12552267
8086585
14209579
2699877
10912174
2158822
12553605
3563680
12994178
4203697
14457491
16443213
2048349
142900
9171587
15611778
9401344
8670505
5960725
12031000
15794329
7074651
10790555
487378
12112381
7330769
10714973
15142971
10465809
15834130
13677094
1260233
7714308
5247976
14668531
5526268
3408359
4099071
10006634
16681036
3466387
9611353
6268053
12507994
281791
4671193
1469005
9129169
8973386
2261215
11932184
12222121
10293956
9910102
503715
14128149
10395469
7049125
555450
4253309
13459410
11578229
3571859
15468551
13695920
14523006
454962
7335957
2559378
6967859
5419088
1086689
13793156
14448819
8026729
10467574
13508529
4357165
12759464
11896010
1783926
7541639
14390317
15363544
15844863
3442247
9844112
3002113
3687031
3110235
4502720
8841889
9620393
8923868
10201930
4089835
990324
9920688
6921115
8250244
1467981
8321928
15960386
12853497
14929300
9979352
9713474
14264584
8221470
7731981
13864240
10885328
11561209
2292637
7478154
15114772
13450208
5780596
16701876
9690531
369022
16473267
8008744
15880357
12572906
12141023
9830125
13120801
8721010
7067955
2981597
15212410
10301687
391975
889663
5111536
14945736
15018239
12658741
2904114
12219126
7862734
14747856
5494805
15782845
8427843
419090
5647165
14463393
9192782
7070693
15009599
6429544
3009778
709372
15010842
8127500
9457746
5725623
9227096
11781948
1996169
16451717
6950453
11483631
13923564
14407388
7890766
10847207
14280401
15486620
7469052
6999524
14570315
7488476
7354241
7660445
11695627
10238767
6851418
7228247
7461885
12449306
1752765
6703934
2691638
5575930
7838795
5626210
2319446
14863003
1698348
13285975
15331529
12879829
13186989
16385873
6285551
13164504
2936115
1317497
12535728
5324909
13155840
6968081
7903212
15432494
11847584
4609509
7592648
11896244
8199996
9383217
4841967
5815815
551443
11204869
9240762
972265
12869227
13924822
10004065
15151712
7110165
4429235
520306
2288803
142649
15448368
1676563
9244097
1202376
2130625
13141510
12543641
10930455
859609
4297912
7164223
16257167
11527625
15350612
3962800
6049070
1106614
14934877
14066870
9654194
8403133
1363495
10278650
7824837
273833
7977806
11334332
14831440
16494633
7273263
15934514
3455812
13154259
9014415
10969736
15075234
5966521
12505743
4299111
7373957
13389712
10702943
16536650
9622503
13788645
1734752
15063891
9533859
13964283
7872990
5798952
13347315
243544
10723553
1190433
776674
6882604
8068194
11476849
11638477
6979061
9291176
11342867
811249
6369263
8815995
9555746
226104
11477261
2158246
8067791
1040677
12474164
8158785
10994270
9221288
8275894
5961536
9468868
6734301
821478
13884657
12200685
7463042
10696125
1930574
2521970
5364408
690044
5793120
5604711
7174424
4321075
3855695
13579004
15338957
14245096
744542
3276323
8469829
13403930
3398802
7401176
236692
3262255
6449028
15895429
2180391
14192919
7966643
16428288
15957149
11172453
8704134
6641091
7006154
9580989
5667111
14039165
11659989
13987648
2922640
4746187
1992521
3521630
8747303
1687462
8660344
1548550
3624376
891993
10625599
12005608
9676239
1085937
7310941
1030419
11684375
3772885
293626
5390270
14438325
8843699
14057676
14365101
5302670
722734
10929760
9682196
13892800
628138
10936206
1602914
13719755
15536938
6383086
3842247
14751892
14718378
4704593
3369912
16246156
12656487
13611447
185126
11482974
986451
11074341
7782228
15019457
6301591
16615649
5577399
4724038
5685017
16462379
11597171
10694790
15553818
15614066
12550756
11663958
12415381
16078630
5581263
13244810
8647306
1784310
13796114
1955877
2924636
3172387
10087882
13870321
3934820
14003408
3219237
12211212
10185045
12316123
877828
10825518
7850841
5447349
5173729
11271736
6971093
939002
12207403
5958143
3146863
2140463
11073582
4076382
6362730
2733309
3136047
2508018
5878410
2915230
15109046
11506788
15068069
7246801
3775398
5397914
2863221
12782244
12954838
10001604
6389197
7509726
8600465
2703613
11805200
7081812
7752550
10006010
2682575
16359849
15753795
7728434
9251510
5560886
7193042
13362121
10213697
12559293
12158627
15685593
11265662
12554080
10496373
14824090
14373602
7412992
4554125
9716581
4158827
16360113
7901894
3845845
5513325
5615915
14644863
5661381
4992820
6249612
14647103
11823464
13991696
2043264
8202077
3092611
3329454
8147203
3161735
4524888
4560761
10755460
13165697
11138014
10912646
10454232
13763545
4736932
13946305
12940488
2051539
9483235
10325083
15564104
961561
15620163
14004506
3075466
16127267
2838056
11651799
11970821
5980327
13961360
8670623
8488269
13839758
15895573
16548073
14512726
7947869
14175460
14094872
5569039
12283663
4122472
16586160
1366967
7199664
595128
7297816
6181976
13329691
7189480
2176061
838134
6589573
3203600
11404365
8545212
10394213
14916059
12211657
921650
9672095
2670213
15278170
12164368
11485081
13152847
13500782
652196
529713
8151979
206623
5309163
3900993
16722878
11580177
13971115
7756169
8968757
1935813
11119274
9270423
8725269
13464202
5340146
2302544
5927048
4762476
9177505
2183796
7557813
1551275
15623223
408675
15513381
11648215
15171494
6298410
9461358
5361484
11561600
8217521
8924829
12708081
4296089
7773044
3135753
6925537
9094219
641583
14844015
14524299
1495082
724541
9348451
1657809
1150088
9373261
14645648
13409064
3289194
6170247
11406008
11948819
9621979
3112682
5278442
12156473
5293429
15826963
11516174
13288765
16253449
8695376
1724835
9737123
6069187
5794854
8327270
16514713
5178887
14993701
11638392
2645480
6422629
9763593
7029427
5929564
6480772
1407442
7557357
9056214
7866307
3276439
13524511
9169826
13953647
9729122
13207731
12519700
1167643
11898248
10810036
6925886
11228757
13675083
10579187
3367010
8946018
3164859
2415602
9403146
13163790
383266
11773678
7824632
5642674
941878
1555541
1417925
12707232
15340221
10275478
2658256
15935227
8892701
11464296
2939428
660413
635052
10671505
204098
7787029
9792858
16192607
9427246
14513628
9562268
3009111
6567626
12194025
5862705
15008977
14071680
5502272
11856526
12837576
2110540
5612257
13937787
4364440
7407252
5875951
7083367
5370618
3369930
12000313
11632845
5231460
13906601
1007092
12411349
221842
5473732
7179631
13389108
1443345
11119207
8521958
13149949
14573688
6241345
1199480
5994973
11853632
16757977
10872340
12701692
10887712
12503499
5117669
6513222
844882
8303838
7786312
9469096
15981904
7740726
3932573
7350351
2064958
5300389
3981201
10607510
1489041
1648282
9235566
7710168
9227097
16395101
6528759
970465
2198573
3948889
3187457
16624173
4852893
11749027
9978249
2400425
2636545
15523465
8550015
15375353
6597233
12007643
11798197
1552332
5605347
10822297
13047927
3568640
9054858
4001031
9615183
8622285
13474298
12462242
4542781
7196759
3601893
15767607
1385017
3613294
581574
16125220
5416055
14588764
4273014
12204086
6981905
11995380
1486383
9748899
11935727
10710311
9576836
4310665
4796381
13624980
1889883
14219756
14051944
2648492
5278032
3195479
12407824
8223483
11500190
2048884
10662296
1567057
12078599
7924588
4550888
7116669
7383128
8816279
10506233
16037171
4566416
13184980
1902713
8489183
9153018
8311072
3337370
2034636
6197061
7398721
404077
10173232
3318283
6117620
16281753
5307976
1651226
4509398
6398667
3076030
3788156
5317947
3766138
7340594
16232774
13526883
662721
2678932
5032200
8305632
4184321
10014404
12135418
15093696
5478107
16228485
1139458
9605321
2593977
4180111
6545961
1087578
8537995
8546875
10018878
4834210
11583709
9525681
13716987
8184979
3572341
3662929
6170763
7665813
10299333
303208
15345384
5840391
9064556
9905123
10947673
13092807
10221706
8116518
6836499
15963485
4930951
14150299
967432
4480774
15554821
4904184
393452
14747071
16212540
8863927
3923586
7766124
13549797
13150311
5623971
9566092
7331313
8691459
9846409
15140970
773876
3574437
5057151
16217706
10935014
15327294
14682998
15176968
7848770
2136112
12009626
14218827
5489900
16721130
1797563
11585465
11006562
9033648
8835096
15374990
4562387
9811978
3118322
3861161
13328231
11893575
13378403
10213833
15944699
14538421
437958
7111119
10171329
6677528
12528756
6598750
16670962
983066
10070910
5799854
2950579
4483382
15017399
13581184
6170506
13969101
10099583
16020765
11847565
1003196
13495702
16297396
10406490
2146296
15601286
8004336
10146636
1029451
6243900
2037109
14357777
4472919
3386096
12800986
10538964
6919595
13322386
16737809
15512704
4790922
14168276
7263344
11675248
13244357
12955400
5151335
1370843
11978151
3295805
2735775
9201862
573367
4661817
15310985
7630596
14792353
649051
9117041
8347867
5769025
10643182
15992063
12623713
9210468
14185182
3747205
10194543
14943547
4666006
11865003
15487516
4645434
14739555
5975567
14716481
10076253
8749232
1575927
16604733
15705960
9454639
9754041
7426427
3651091
6850888
3636584
16218769
5475964
12574978
6753873
5907686
15464845
5500415
10627379
6111415
14075483
4892253
10000774
5001498
16128728
10377397
5074151
15533144
770668
4417268
11448493
7116364
15909169
15899741
15098327
3588068
12387072
1080488
9801940
8581111
16289613
6909453
4068436
14457281
3711573
2526410
11351048
16049654
13075423
13500875
8306005
15627878
4079679
15232252
2308138
6437454
12686338
9124910
13314223
1355067
5024241
9985649
16524077
2096199
13023456
13614937
4462962
2508733
13782754
10830565
11523100
11268878
7083315
9363293
13473162
12202939
15087427
2427918
16449468
13757302
8336114
13729676
4688193
16024372
5424145
3699672
14686006
4963873
4957408
10747859
5122750
6632874
12059690
8587575
321168
13884574
8164146
10466965
15396121
13834339
15785340
11389655
5740642
15523268