Advent of Code with OpenRefine Day 8

This should have been relatively straightforward, and really was much simpler than day 7 but my brain got stuck on some of the logic for the second part and then I made some stupid mistakes in applying transformations. Eventually I solved the problem with the example data from the description of the problem and checked I got the correct answer – then I was able to extract the transformation history and apply to the larger project – which was a nice use of that functionality in OpenRefine 🙂

The video below skips the actual code I used to work out part 2 so if you’re interested it looked a bit like this:

with(rowIndex-row.record.fromRowIndex,position,
  filter(
    forEachIndex(
row.record.cells.treeHeight.value.slice(position+1),i,v,if(value<=v,i+1,null)
    )
    ,j,j!=null)[0]
  )
)

This code goes through a record tree heights (one record per column or row) and then finds all the trees either before or after the current position (this is what the ‘slice’ does in the middle of this GREL) and then finds the position along from that of each taller tree in the list, and then extracts the first of those positions – which gives us the number of trees you can go along the row/up or down the column before you find a tree the same size or bigger.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.