Advent of Code: Day 8 Solution

This year, I’m posting my solutions to the puzzles in the Advent of Code. Each post will consist of pseudocode, with a link to the actual code I used to solve the puzzle on Github.

And now, on with the solution:

Day 8, part 1

Simplified problem: Given a list of inputs, manipulate the data on a grid 50 wide by 6 high. The grid is empty to begin with, and is manipulated by instructions in the format below:

After all of the instructions have been followed, how many blocks in the grid are true?

Solution

  1. Set up a console to visualise the data. (I used a standard command window).
  2. Set grid = an array of bool[6,50]
  3. Set input = (input)
  4. For each instruction in input
    1. If instruction starts with rect:
      1. set width = (first number in instruction)
      2. set height = (second number in instruction)
      3. For row = 0 to width
        1. For col = 0 to height
          1. Set value of grid[row,col] =  true
        2. End For
      4. End For
    2. Else if instruction starts with rotate row:
      1. set row = (first number in instruction)
      2. set blocksToMove = (second number in instruction)
      3. set rowList = new list, containing values of all blocks in row
      4. Reverse rowList
      5. For i = 0 to blocksToMove
        1. Remove the first value in the list, and add it to the end of the list.
      6. End For
      7. Reverse rowlist
      8. set currentColumn = 0
      9. For each value in rowList
        1. set grid[row,currentColumn] = value
        2. Add 1 to currentColumn
      10. End For
    3. Else (the only other instruction is rotate colunn)
      1. set col = (first number in instruction)
      2. set blocksToMove = (second number in instruction)
      3. set colList = new list, containing values of all blocks in column
      4. Reverse colList
      5. For i = 0 to blocksToMove
        1. Remove the first value in the list, and add it to the end of the list.
      6. End For
      7. Reverse collist
      8. set currentRow = 0
      9. For each value in colList
        1. set grid[currentRow,col] = value
        2. Add 1 to currentRow
      10. End For
    4. Display the grid:
      1. For row = 0 to height
        1. For col = 0 to width
          1. if grid[row,col]=true, write #
          2. Else, write .
        2. End For
        3. Go to next line
      2. End For
  5. End For
  6. Return number of blocks in grid where value = true

Day 8, part 2

After following the instructions, you realise that the grid spells out a code word. What is it?

Solution

  1. It turns out that I had already solved this in Part 1 – using the visualisation I had created to verify that the instructions were being followed correctly, I was able to read the code word without any further modification.

You can find the full solution on Github

Leave a Reply

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