Spoiler warning.
Day 5 offered a chance to have some fun at the kdb+ console.
Parsing the input:
i:flip -1 1 10 -1 -1+(6#" I";" “)0:(1+r?”“)_r:read0`05.txt // instructions s:trim flip reverse((-1+r?”")#r)[;1+4til 9] // stacks
The solution is to iterate over the stacks and list of instructions, appending and removing from the correct indices as necessary:
last each{@/[x;y 2 1;(,;:);](reverse y[0]#;y[0]_)@:x y 1}/[s;i] last each{@/x;y 2 1;(,;:);@:x y 1}/[s;i]
The examples provided in the problem description are always quite visual, and since this is an iterative solution, this provided a nice opportunity to visualize the solution too. The animate function takes the stacks and formats it to match the that of the original input, before writing it to the console:
animate:{ 1"\033[H\033[J"; // clear the console x:reverse flip(7h$first system"c")$x; // pad the stacks to match the console size -1 {@/[raze"[“,‘x,’”]“;0 2+/:3*where null x;:;” “]}each x; // surround crates with and write to stdout system"sleep 0.5”; // pause for dramatic effect }q)animate s // initial stack state [P] [Q] [T] [F][N] [P][L] [M] [H][T][H] [M][H] [Z] [M][C][P] [Q][R][C] [J] [T][J][M][F][L][G][R] [Q] [V][G][D][V][G][D][N][W][L] [L][Q][S][B][H][B][M][L][D] [D][H][R][L][N][W][G][C][R]
If we then call this animate function at each step of the iteration, we get a visual of the crates moving between the stacks at each step. Once this is finished, we can then read off the solution - the top crate on each stack. Part 2, for example:
q){animate o:@/x;y 2 1;(,;:);@:x y 1;o}/[s;i]; [J] [N] [C] [Z] [H] [P] [R] [F] [G][M] [M][V] [M][L] [R][G] [R][T] [W][M] [H][L] [D][B] [P][L] [D][Q] [R] [F][D][L][T] [B] [J][G][P][N][C] [S][N] [D][H][Q][Q][H][T] [M][L][L][W][G][C][Q][H][V]
You can take part by following the details here: https://community.kx.com/t5/kdb-and-q/Advent-of-Code-2022/td-p/13453