Part 1
Description
The input is the output of a df -h command on a grid of storage nodes. Every line gives the node's position (x and y), its size, how much is used, how much is available, and the percentage used. Part 1 asks for the number of viable pairs of nodes (A, B): A is not empty, A and B are not the same node, and the data on A would fit in the available space of B.
Solution
I pasted the 957 lines into A7:A963. The first thing is to turn every line into numbers. TEXTSPLIT cuts a line at the text that surrounds the numbers (/dev/grid/node-x, -y, spaces, T and %) and ignores empty pieces, and -- makes them numbers. A REDUCE does this for every line and stacks the results, and DROP removes the starting empty row:
b: DROP(REDUCE("",SEQUENCE(ROWS(A7:A963)),LAMBDA(x,y,
IFNA(VSTACK(x,--TEXTSPLIT(INDEX(A7:A963,y,1),{"/dev/grid/node-x";"-y";" ";"T ";"%"},,TRUE)),""))),1)
This is a table with six columns: x, y, size, used, avail and use%. The same formula in B7 spills it onto the sheet, in B7:G963.
For the pairs, I compare every node A that isn't empty with every node B. us>0 marks the non-empty nodes, and us<=TRANSPOSE(av) is true where A's used space is at most B's available space. The first SUMPRODUCT counts these for all pairs, including A = B, and the second one subtracts the pairs where a node would fit its own data:
B2: =LET(
a, A7:A963,
b, DROP(REDUCE("",SEQUENCE(ROWS(a)),
LAMBDA(x,y,
IFNA(VSTACK(x,--TEXTSPLIT(INDEX(a,y,1),{"/dev/grid/node-x";"-y";" ";"T ";"%"},,TRUE)),"")
)
),1),
x, INDEX(b,,1),
y, INDEX(b,,2),
sz, INDEX(b,,3),
us, INDEX(b,,4),
av, INDEX(b,,5),
pe, INDEX(b,,6),
c,SUMPRODUCT((us>0)*(us<=TRANSPOSE(av)))-SUMPRODUCT((us>0)*(us<=av)),
c
)
Part 2
Description
Now I want to move the data. Data can only be moved from a node to a neighbouring one (up, down, left or right), and all of it at once. The goal is the data in the top-right node, and I want to bring it to the top-left node, where I can get it. Part 2 asks for the fewest moves.
Solution
For this one I looked at the grid and solved it by hand. First I drew the grid on the sheet. H7 holds a key for each node (=B7*100+C7), the x positions are in row 6 and the y positions in column I, and every cell shows the size and the used space of its node:
J7: =INDEX($D$7:$D$963,MATCH(J$6*100+$I7,$H$7:$H$963,0))&"/"&
INDEX($E$7:$E$963,MATCH(J$6*100+$I7,$H$7:$H$963,0))
Looking at the picture:
- Almost all nodes have a similar size and use about the same amount of space. The data of any node fits in the free space of only one node, the empty one. There is exactly one empty node.
- One row of nodes has so much data that it can never move. It forms a wall across the grid, with a gap at the left end.
So this is a sliding puzzle with one empty tile, and the only thing that moves is the empty node. Moving the empty node onto a neighbour moves that neighbour's data into it. I counted the moves in three parts:
- Take the empty node to the top-left corner, going through the gap in the wall.
- Move it along the top row until it is next to the goal data, and then one more move to swap places with it. Now the goal data is one step to the left, with the empty node on its right.
- Every further step of the goal data to the left takes 5 moves: the empty node goes down, left, left and up to get in front of the data, and then right to swap places with it. That is 5 moves for every position, until the data reaches the corner.
D2: =LET(
input, A7:A963,
split, DROP(REDUCE("",SEQUENCE(ROWS(input)),
LAMBDA(x,y,
IFNA(VSTACK(x,--TEXTSPLIT(INDEX(input,y,1),{"/dev/grid/node-x";"-y";" ";"T ";"%"},,TRUE)),"")
)
),1),
x, INDEX(split,,1),
y, INDEX(split,,2),
sz, INDEX(split,,3),
us, INDEX(split,,4),
av, INDEX(split,,5),
pe, INDEX(split,,6),
Part1,REDUCE(0, SEQUENCE(ROWS(input)),
LAMBDA(p,q,
LET(
u, INDEX(us, q),
a, INDEX(av, q),
p + SUM(--(FILTER(us, us>0) <= a)) - --((u>0)*(u<=a))
)
)
),
grid, WRAPCOLS(us,SUM(--(x=0))),
tx, MAX(x),
ty, MIN(y),
r, SEQUENCE(ROWS(grid), 1, 0),
c, SEQUENCE(1, COLUMNS(grid), 0),
marked, IF((r=ty)*(c=tx), CHAR(64), grid),
path, BFSa(marked,tx,ty,MIN(x),MIN(y)),
init_state, VSTACK(marked, HSTACK(0, EXPAND("", 1, COLUMNS(grid)-1, ""))),
move2, REDUCE(init_state, SEQUENCE(ROWS(path)-1), LAMBDA(k,l,
LET(
curr_grid, DROP(k, -1),
curr_moves, INDEX(k, ROWS(k), 1),
idx, MATCH(0, TOCOL(curr_grid), 0) - 1,
czero, MOD(idx, COLUMNS(grid)),
rzero, QUOTIENT(idx, COLUMNS(grid)),
path0, BFSa(curr_grid, INDEX(path, l+1, 1), INDEX(path, l+1, 2), czero, rzero),
move, REDUCE(curr_grid, SEQUENCE(ROWS(path0)-1),
LAMBDA(p,q,
LET(
xa, INDEX(path0, ROWS(path0)+1-q, 1),
ya, INDEX(path0, ROWS(path0)+1-q, 2),
xb, INDEX(path0, ROWS(path0)-q, 1),
yb, INDEX(path0, ROWS(path0)-q, 2),
va, INDEX(p, ya+1, xa+1),
vb, INDEX(p, yb+1, xb+1),
IF((r=ya)*(c=xa), vb, IF((r=yb)*(c=xb), va, p))
)
)
),
next_grid, IF((r=INDEX(path, l, 2))*(c=INDEX(path, l, 1)), 0,
IF((r=INDEX(path, l+1, 2))*(c=INDEX(path, l+1, 1)), "@", move)
),
next_moves, curr_moves + ROWS(path0),
VSTACK(next_grid, HSTACK(next_moves, EXPAND("", 1, COLUMNS(grid)-1, "")))
)
)),
Part2, INDEX(move2, ROWS(move2), 1),
Part2
)
