Day 22 of Advent of Code in q

Author - Rory Kemp

Original Problem Statement

--- Day 22: Monkey Market ---

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?

First, read the input and convert to numbers.

show nums: "J"$read0 `22.txt
1 10 100 2024

Now, we can see that all operations are done mod 16777216. This is 2^24, so instead we store the numbers as 24 bit values. The 1= is to cast to booleans.

show flip nums: 1=(24#2) vs "J"$read0 `22.txt
000000000000000000001010b
000000000000000001100100b
000000000000011111101000b

Having our values as a boolean matrix allows us to apply the operations on all numbers at once.

The evolution process has 3 steps:

  1. Multiply by 64 (shift left by 6), and xor.
  2. Divide by 32 (shift right by 5), and xor.
  3. Mulitply by 2048 (shift left by 11), and xor.

Let's define a function that does one step. We can use xprev to do the shifting, and pad.

3 xprev nums

We will need to reshape however, so the empty arrays become arrays of 0s.

The XOR is equivalent to not equals.

The full function becomes:

step: {y<>count[first y]#'x xprev y}

Now, we can find the next number by doing the three steps.

2 sv step[-11] step[5] step[-6] 1=(24#2) vs 123
,15887950

Now, apply this 2000 times. We keep the intermediate results, but use the last one.

show last res:2000{step[-11] step[5] step[-6] x}\nums
1011b
0110b
0010b
0000b
0010b
1101b
0100b
0110b
1100b
0001b
0100b
0100b
0110b
1010b
1110b
1101b
0011b
1010b
1100b
1110b
0010b
1011b
..

The part 1 result is the sum of the decoded values.

sum 2 sv last res
37327623

Part 2:

Part 2 Original Description:

--- Part Two ---

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?

Now we want to decode the values, and take the last digit.

mod[2 sv'res; 10]
1 0 0 4
3 0 7 9
3 2 4 5
9 7 3 3
5 6 7 4
9 3 8 3
0 3 0 1
5 9 2 6
9 0 8 3
7 6 2 7
2 7 8 0
0 2 6 9
1 3 7 9
3 2 7 2
9 6 0 6
8 3 3 9
5 6 3 0
7 8 6 9
6 5 0 8
6 8 8 8
9 3 8 1
3 0 9 2
..

We want to find patterns of 4 deltas. We do this by applying prev 3 times.

3 prev\ deltas mod[2 sv'res;10]
1  0  0  4  2  0  7  5  0  2  -3 -4 6  5  -1 -2 -4 -1 4  1  4  -3 1  -1 -9 0 ..
`long$()    1 0 0 4     2 0 7 5     0 2 -3 -4   6 5 -1 -2   -4 -1 4 1   4 -3 ..
`long$()    `long$()    1 0 0 4     2 0 7 5     0 2 -3 -4   6 5 -1 -2   -4 -1..
`long$()    `long$()    `long$()    1 0 0 4     2 0 7 5     0 2 -3 -4   6 5 -..

We drop 3 values from each of these. Then, we can encode 4 values into a single integer by adding 10 and using 20 sv.

20 sv 10+3_'3 prev\ deltas mod[2 sv'res;10]
132251 125010 75150  66714 
54612  78250  115757 91335 
114730 59912  93787  76566 
13736  82995  20689  67828 
120686 132149 97034  123391
118034 14607  132851 62169 
69901  128730 38642  115108
43495  94436  129932 29755 
66174  44721  70496  153487
91308  90236  91524  87674 
100565 76511  84576  28383 
133028 115825 28228  113419
78651  61791  105411 109670
59932  107089 85270  13483 
98996  101354 108263 152674
76949  61067  37413  79633 
83847  107053 145870 83981 
108192 45352  87293  28199 
37409  58267  92364  89409 
113870 114913 12618  124470
69693  125745 88630  70223 
59484  78287  108431 115511

Now, pair this up with the original values. We sum the dictionaries, and then find the maximal sum.

{show x; max x} sum {(flip 20 sv 10+3_'3 prev\deltas x)!'flip 3_x} mod[2 sv'res;10]
132251| 9
54612 | 9
114730| 16
13736 | 0
120686| 5
118034| 9
69901 | 7
43495 | 2
66174 | 0
91308 | 1
100565| 3
133028| 9
78651 | 8
59932 | 5
98996 | 7
76949 | 6
83847 | 9
108192| 9
37409 | 3
113870| 7
69693 | 5
59484 | 2
..
24

Recap:

The full solution is this.
nums: 1=(24#2) vs "J"$read0 `:i/22t.txt
step: {y<>count[first y]#'x xprev y}
sum 2 sv last res:2000{step[-11] step[5] step[-6] x}\nums
max sum {(flip 20 sv 10+3_'3 prev\deltas x)!'flip 3_x} mod[2 sv'res;10]