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:
1 2 3 |
rect AxB turns on all of the pixels in a rectangle at the top-left of the screen which is A wide and B tall. rotate row y=A by B shifts all of the pixels in row A (0 is the top row) right by B pixels. Pixels that would fall off the right end appear at the left end of the row. rotate column x=A by B shifts all of the pixels in column A (0 is the left column) down by B pixels. Pixels that would fall off the bottom appear at the top of the column. |
After all of the instructions have been followed, how many blocks in the grid are true?
Solution
- Set up a console to visualise the data. (I used a standard command window).
- Set grid = an array of bool[6,50]
- Set input = (input)
- For each instruction in input
- If instruction starts with rect:
- set width = (first number in instruction)
- set height = (second number in instruction)
- For row = 0 to width
- For col = 0 to height
- Set value of grid[row,col] = true
- End For
- For col = 0 to height
- End For
- Else if instruction starts with rotate row:
- set row = (first number in instruction)
- set blocksToMove = (second number in instruction)
- set rowList = new list, containing values of all blocks in row
- Reverse rowList
- For i = 0 to blocksToMove
- Remove the first value in the list, and add it to the end of the list.
- End For
- Reverse rowlist
- set currentColumn = 0
- For each value in rowList
- set grid[row,currentColumn] = value
- Add 1 to currentColumn
- End For
- Else (the only other instruction is rotate colunn)
- set col = (first number in instruction)
- set blocksToMove = (second number in instruction)
- set colList = new list, containing values of all blocks in column
- Reverse colList
- For i = 0 to blocksToMove
- Remove the first value in the list, and add it to the end of the list.
- End For
- Reverse collist
- set currentRow = 0
- For each value in colList
- set grid[currentRow,col] = value
- Add 1 to currentRow
- End For
- Display the grid:
- For row = 0 to height
- For col = 0 to width
- if grid[row,col]=true, write #
- Else, write .
- End For
- Go to next line
- For col = 0 to width
- End For
- For row = 0 to height
- If instruction starts with rect:
- End For
- 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
- 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