Day 11 of Advent of Code in q

Author - Rory Kemp

Original Problem Statement

--- Day 11: Plutonian Pebbles ---

The ancient civilization on Pluto was known for its ability to manipulate spacetime, and while The Historians explore their infinite corridors, you've noticed a strange set of physics-defying stones.

At first glance, they seem like normal stones: they're arranged in a perfectly straight line, and each stone has a number engraved on it.

The strange part is that every time you blink, the stones change.

Sometimes, the number engraved on a stone changes. Other times, a stone might split in two, causing all the other stones to shift over a bit to make room in their perfectly straight line.

As you observe them for a while, you find that the stones have a consistent behavior. Every time you blink, the stones each simultaneously change according to the first applicable rule in this list:

No matter how the stones change, their order is preserved, and they stay on their perfectly straight line.

How will the stones evolve if you keep blinking at them? You take a note of the number engraved on each stone in the line (your puzzle input).

If you have an arrangement of five stones engraved with the numbers 0 1 10 99 999 and you blink once, the stones transform as follows:

So, after blinking once, your five stones would become an arrangement of seven stones engraved with the numbers 1 2024 1 0 9 9 2021976.

Here is a longer example:

Initial arrangement:
125 17

After 1 blink:
253000 1 7

After 2 blinks:
253 0 2024 14168

After 3 blinks:
512072 1 20 24 28676032

After 4 blinks:
512 72 2024 2 0 2 4 2867 6032

After 5 blinks:
1036288 7 2 20 24 4048 1 4048 8096 28 67 60 32

After 6 blinks:
2097446912 14168 4048 2 0 2 4 40 48 2024 40 48 80 96 2 8 6 7 6 0 3 2

In this example, after blinking six times, you would have 22 stones. After blinking 25 times, you would have 55312 stones!

Consider the arrangement of stones in front of you. How many stones will you have after blinking 25 times?

First read the input, and cast to ints.
show i:"J"$" "vs first read0 `11.txt
125 17

Now we want to write a function that does the blinking process.

There are 3 cases:

  1. x=0 in which case we return 1.
  2. 0=(count s:string x)mod 2 in which case we return "J"$2 0N#s.
  3. This splits the string in half and casts both halves to ints.
  4. Otherwise, we return x*2024.

blink:{(),$[x=0;1;0=(count s:string x)mod 2;"J"$2 0N#s;x*2024]}

We can check the function is working as intended.

Also, the (), is so it always returns a list, rather than a scalar.

blink each 0 1 123456

Now, we apply the function to each item in the input, and concatenate all the results.

This is done 25 times.

count 25 {raze blink each x}/ i 
55312

Part 2:

Part 2 Original Description:

--- Part Two ---

The Historians sure are taking a long time. To be fair, the infinite corridors are very large.

How many stones would you have after blinking a total of 75 times?

This is the same as part 1, but doing it 75 times instead of 25 times.

Rather than storing all stones, which could be inefficient, let's store frequencies for each stone.

We start with a table where the frequencies are all 1.

show S: flip `s`c ! (i; count[i] # 1)
s   c
-----
125 1
17  1

Now, doing 1 step has 3 parts: Update s with blink each s.

update s: blink each s from S
s       c
---------
,253000 1
1 7     1
Use ungroup to flatten the table.
ungroup update s: blink each s from S
s      c
--------
253000 1
1      1
7      1
Now, sum the counts by the stone ids.
select sum c by s from ungroup update s: blink each s from S
s     | c
------| -
1     | 1
7     | 1
253000| 1

This can be stored in a function, called step, and we can apply it 25 times to check it is working as intended.

step:{select sum c by s from ungroup update s:blink each s from x}
sum 25 step/ S 
c| 55312

So now our result is:

exec sum c from 75 step/ S 
65601038650482

Recap:

The full solution is this.
i: "J"$" "vs first read0 `11.txt
blink:{(),$[x=0;1;(count s:string x)mod 2;x*2024;"J"$2 0N#s]}
count 25 {raze blink each x}/ i
S: flip `s`c!(i; count[i]#1)
step:{select sum c by s from ungroup update s:blink each s from x}
exec sum c from 75 step/ S