Advent of Code F# — Day 22

ps. look out for all my oth­er solu­tions for Advent of Code chal­lenges here.


Day 22

See details of the chal­lenge here.

Today’s input looks like this:

root@ebhq-gridcenter# df -h
Filesys­tem                           Size    Used     Avail    Use%
/de­v/­grid/n­ode-x0-y0     87T     71T     16T       81%
/de­v/­grid/n­ode-x0-y1     93T     72T      21T       77%
/de­v/­grid/n­ode-x0-y2     87T     67T      20T       77%
/de­v/­grid/n­ode-x0-y3     89T     65T      24T       73%
/de­v/­grid/n­ode-x0-y4     93T     67T      26T       72%

Of the infor­ma­tion pre­sent­ed to us we only need the x & y posi­tion of the node, its size and amount of space used. Every­thing else can either be derived from these or are not need­ed for this chal­lenge.

For part 1 of the chal­lenge, if we sort the array of nodes by the amount of avail­able space in descend­ing order, then for each of the ele­ments we only need to iter­ate through the sort­ed array for as long as the avail­able space is >= the used space of the cur­rent node. Hence reduc­ing the aver­age time com­plex­i­ty from O(n2) to O(n * log n).


Part 2

Now that you have a bet­ter under­stand­ing of the grid, it’s time to get to work.

Your goal is to gain access to the data which begins in the node with y=0 and the high­est x (that is, the node in the top-right cor­ner).

For exam­ple, sup­pose you have the fol­low­ing grid:

Filesystem            Size  Used  Avail  Use%
/dev/grid/node-x0-y0   10T    8T     2T   80%
/dev/grid/node-x0-y1   11T    6T     5T   54%
/dev/grid/node-x0-y2   32T   28T     4T   87%
/dev/grid/node-x1-y0    9T    7T     2T   77%
/dev/grid/node-x1-y1    8T    0T     8T    0%
/dev/grid/node-x1-y2   11T    7T     4T   63%
/dev/grid/node-x2-y0   10T    6T     4T   60%
/dev/grid/node-x2-y1    9T    8T     1T   88%
/dev/grid/node-x2-y2    9T    6T     3T   66%

In this exam­ple, you have a stor­age grid 3 nodes wide and 3 nodes tall. The node you can access direct­ly, node-x0-y0, is almost full. The node con­tain­ing the data you want to access, node-x2-y0 (because it has y=0 and the high­est x val­ue), con­tains 6 ter­abytes of data — enough to fit on your node, if only you could make enough space to move it there.

For­tu­nate­ly, node-x1-y1 looks like it has enough free space to enable you to move some of this data around. In fact, it seems like all of the nodes have enough space to hold any node’s data (except node-x0-y2, which is much larg­er, very full, and not mov­ing any time soon). So, ini­tial­ly, the grid’s capac­i­ties and con­nec­tions look like this:

( 8T/10T) --  7T/ 9T -- [ 6T/10T]
    |           |           |
  6T/11T  --  0T/ 8T --   8T/ 9T
    |           |           |
 28T/32T  --  7T/11T --   6T/ 9T

The node you can access direct­ly is in paren­the­ses; the data you want starts in the node marked by square brack­ets.

In this exam­ple, most of the nodes are inter­changable: they’re full enough that no oth­er node’s data would fit, but small enough that their data could be moved around. Let’s draw these nodes as .. The excep­tions are the emp­ty node, which we’ll draw as _, and the very large, very full node, which we’ll draw as #. Let’s also draw the goal data as G. Then, it looks like this:

(.) .  G
 .  _  .
 #  .  .

The goal is to move the data in the top right, G, to the node in paren­the­ses. To do this, we can issue some com­mands to the grid and rearrange the data:

  • Move data from node-y0-x1 to node-y1-x1, leav­ing node node-y0-x1emp­ty:
    (.) _  G
     .  .  .
     #  .  .
  • Move the goal data from node-y0-x2 to node-y0-x1:
    (.) G  _
     .  .  .
     #  .  .
  • At this point, we’re quite close. How­ev­er, we have no dele­tion com­mand, so we have to move some more data around. So, next, we move the data from node-y1-x2 to node-y0-x2:
    (.) G  .
     .  .  _
     #  .  .
  • Move the data from node-y1-x1 to node-y1-x2:
    (.) G  .
     .  _  .
     #  .  .
  • Move the data from node-y1-x0 to node-y1-x1:
    (.) G  .
     _  .  .
     #  .  .
  • Next, we can free up space on our node by mov­ing the data from node-y0-x0 to node-y1-x0:
    (_) G  .
     .  .  .
     #  .  .
  • Final­ly, we can access the goal data by mov­ing the it from node-y0-x1to node-y0-x0:
    (G) _  .
     .  .  .
     #  .  .

So, after 7 steps, we’ve accessed the data we want. Unfor­tu­nate­ly, each of these moves takes time, and we need to be effi­cient:

What is the fewest num­ber of steps required to move your goal data to node-x0-y0?

If you visu­al­ize the grid then the solu­tion becomes pret­ty clear and one that we can actu­al­ly work out by hand.

Here, the tar­get node is marked as red, the emp­ty node green (which is ini­tial­ly at posi­tion (7, 17)), and the very large, very full nodes are marked as black. We want to move the emp­ty node all the way to (35, 0) so we can start mov­ing the tar­get node at (36, 0) hor­i­zon­tal­ly towards (0, 0).

We can again use the Lev­el Order Tree Tra­ver­sal approach we have seen so many times this AOC for the first part. First, let’s trans­form the Node[] into a 2D array first to make our task eas­i­er.

The trick for the tra­ver­sal part of the prob­lem is to avoid pass­ing through the same coor­di­nate more than once, a prob­lem that can be eas­i­ly rec­ti­fied with a cache of posi­tions that we have already passed through.

Once the emp­ty node is at (35, 0) we can com­mence the sec­ond part of the prob­lem — to move the data in the tar­get node to (0, 0). Every time we move the data to the emp­ty node on the left, we’ll have to move the emp­ty node in front of it again, which means going down, left, left and then up, so that means a total of 5 moves for every step towards (0, 0). Except, when the data is at (1, 0), which requires only one move.

Which means, there’s a total of 5 * 35 + 1 move left ONCE we have move the emp­ty node to (35, 0).

In case you’re won­der­ing why I left out the “+ 1” on line 33, it’s because the path that’s returned by find­Paths includes the start­ing posi­tion (7, 17), so real­ly line 33 should have read let part2 = path.Length — 1 + 35 * 5 + 1