Day 4: Printing Department
Part 1
You ride the escalator down to the printing department. They're clearly getting ready for Christmas; they have lots of large rolls of paper everywhere, and there's even a massive printer in the corner (to handle the really big print jobs).
Decorating here will be easy: they can make their own decorations. What you really need is a way to get further into the North Pole base while the elevators are offline.
"Actually, maybe we can help with that," one of the Elves replies when you ask for help. "We're pretty sure there's a cafeteria on the other side of the back wall. If we could break through the wall, you'd be able to keep moving. It's too bad all of our forklifts are so busy moving those big rolls of paper around."
If you can optimize the work the forklifts are doing, maybe they would have time to spare to break through the wall.
The rolls of paper (@) are arranged on a large grid; the Elves even have a helpful diagram (your puzzle input) indicating where everything is located.
For example:
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
The forklifts can only access a roll of paper if there are fewer than four rolls of paper in the eight adjacent positions. If you can figure out which rolls of paper the forklifts can access, they'll spend less time looking and more time breaking down the wall to the cafeteria.
In this example, there are 13 rolls of paper that can be accessed by a forklift (marked with x):
..xx.xx@x.
x@@.@.@.@@
@@@@@.x.@@
@.@@@@..@.
x@.@@@@.@x
.@@@@@@@.@
.@.@.@.@@@
x.@@@.@@@@
.@@@@@@@@.
x.x.@@@.x.
Consider your complete diagram of the paper roll locations. How many rolls of paper can be accessed by a forklift?@..@@.@.@@@....@@..@@@.@@@@@@..@..@@.@.@@.@@...@.@.@...@@.....@..@@.@@@@.@@@.@@@..@.@@@@@@.@@@@@@@@@@...@@..@..@@.@@@@@@..@@@...@.@@@@@@
@.@@.@@@@@@..@.@@@@.@@@@@@.@.@@@@..@@@@@@@@.@.@@@...@.@@@@@@..@..@.@@@.@@@@.@@@@.@@@@@@@@@@@..@@@@@@.@@.@@.@.@@@@.@@@@@@@@@@...@.@@.....
@@.@@@@@.@.@@.@@@@@@@@..@@@@@.@@.@@.@@@@.@..@@@.@.@@@@@@@@.@.@@@@@@@@@@@...@@@@@@.@@@@@@@.@@@@.@@.@@.@@.@@.@@.@@@..@@..@@.@@@@@@.....@.@
@@@@.@..@@@.@@.@@.@@.@@@@.@.@.@@@@@@@.@@.@@@...@.@@.@@@@@@.@@@@@.@@.@@@@@.@@..@.@@@@@@@.@@@@.@.@@@.@...@@.@@@@.@.@@@@.@.@.@@@@.@@.@@@@@.
.@@@@.@@@.@@.@@@.@@.@.@.@@..@.@@..@.@@.@@@@@@@@.@.@.@.@@.@@@@..@@.@..@..@.@@@@@@@.@@@.@.@@@@@..@@@@@@@.@@@.@@.@@..@@...@@..@.@@@.@@@@@.@
@@@@@@@.@@.@.@.@@@.@@.@@@@@@@@..@@@@.@@.@@@..@@..@@@@@.@@@.@.@.@..@...@@.@..@@@.@.@@.@.@.@@.@@@@@@.@..@.@@@.@..@..@@..@@@@.@@.@@@@@@@.@.
..@@@.@@.@...@.@@@.@@.@.@@@@@@@@@.@@.@..@.@@@.@@@@.@.@.@@@.@@@.@.@@@@@@.@@.@...@@@..@@.@.@.@@@.@@.@.@@@..@.@@@@@..@.@@.@..@.@@.@...@@@..
@@.@.@.@@..@@@@.@.@@.@@@@@@@@.@.@@.@.@@@..@..@@@@@@@...@@@@@@.@@@@@@.@.@@@@@@@@...@.@@@@@@@@@@@.@@@..@.@.@@@.@@@.@..@@@.@..@@.@.....@@.@
@@.@.@@@@.@@@@..@..@.@@.@@.@@..@.@@@...@@@.@@.@.@@@@..@@.@.@@@.@@@@..@@@.@.@@@..@@...@@.@....@@@.@@@@@@@..@@.....@@.@@@@..@.@@@.@@@.@@@@
@.@@@@..@@@@..@..@@@@@@.@@..@.@.@@@@@..@@@.@@@.@.@@@@..@@@@.@.@@.....@@@@@@@@@@.@..@@.@@@@@@@@.@@@..@..@.@@@@....@.@.@@@@.@@.@.@.@.@@@@.
@.@@....@.@@@@@..@@@@..@@.@@@.@@@.@.@..@@@.@@.@@.@@@@@@@@.@@@.@@.@..@@@@..@@@@@@@@@@.@@@...@@@@.@@.@@@@@.@@@..@@..@@.@@@.@@@@@@@@.@@.@@@
@@@.@@.@.@.@.@@.@@@..@@.@..@@@..@@@@..@@@@..@.@@@@@@@.@@@@..@.@@..@@.@..@@....@@@@@@@@@@@@@.@..@@@@..@@@@@.@@@.@...@@.@..@.@@@@@@@.@@.@@
.@...@.@@@.@@@.@.@@.@...@@.@@.@@.@.@@..@@.@.@@.@.@@@@@@@.@.@@@@.@@@@@@@.@@@..@@@@@@..@@@.@@.@.@..@@@@@..@.@@@@...@..@@@@@@@.@@@@@@@.@.@@
@@@@.@.@.@.@.@.@@@@@@.@@@@@@@..@@@@..@@.@....@..@.@@..@@@@@@@.@@@@@@@@...@@@..@@@@..@@@@@.@@@@.@@..@@.@@.@@@@.@@..@@@@@@@@.@@.@@@@@...@@
@@.@.@@@..@....@@@@@.@@..@@@...@...@@@.@@@@@@..@@@@@@@@.@@@@@@..@.@@...@.@@.@..@@..@@@@.@..@..@@@.@.@@@.@.@@@@@.@@@@@.@.@@..@@@@..@.@.@.
@.@@@@.@..@@.@..@.@@.@.@@@@..@.@.@..@@@@.@@@.@@.@@.@@@@@@.@......@..@@.@@@...@...@@@.@@@.@@.@@@.@@..@.@.@@@@..@@@@@.@@@@@@.@@..@@@@@@@@@
@.@@@@@@@@.@.@@.@.@.@...@@@@@..@@.@.@@@@@@@.@@...@@....@@@..@@.@@@@.@.@@@@@.@@@@.@@..@.@@@....@@.@.@@@.@@@@@.@@@@@@.@@@@@..@...@....@@@.
@.@@@@.@..@@@@@@@@@@@@@@..@@@@...@@@@@@@@..@@@@@.@@@@...@@.@@@@.@@@@@@@.@@..@.@@..@@@.@......@@@@@@@@@@.@@@@@@@..@@@@@..@..@@@@@@.@@@@@.
@@..@@..@@@@@@@@@@.@...@@.@@@@@@@@..@..@@@.@.@.@.@@.@@@@@@@@@@@..@.@..@@@.@....@@@.@@@@.@.@@@.@@@.@@..@@@.@.@@@@@..@@.@...@@@@@@@.@@@...
@..@@.@@.@@@@.@..@@.@@@@@@@@.@@.@.@@@@@..@.@@.@..@.@@.@@@@@@@@@@@...@.@@@.@@@.@.@@@@@@@@@@..@.@@@@@@..@@@@@.@.@@.@@@@..@@@@@....@@..@@.@
..@@.@..@@..@@.@.@.@@@.@..@.@@@@@@@@@..@@@@@.@@@@@@.@@.@@.@..@@@@@@@..@...@..@@..@@.@@@.@..@@@@.@..@@@.@@@.@@@.@.@@@@@@@.@.@.@@@.@@@@...
...@@....@.@@@@@@@@...@@@@@@@.@.@@@@.@@@...@@@@@@@@@@@@@@@@@@@@@@@@.@..@@@@@@.@@@@..@@.@@...@@@@..@..@@@@@@@...@.@@.@@.@@..@.@.@.@.@@@@.
.@@@.@@@.@..@@.@.@..@.@@...@@.@@.@.@@..@@.@...@..@@@@@@..@@@@.@@.@..@@@.@@.@.@.@.@@.@@@.@@@.@..@.@@@.@.@@@..@@.@.@@@.@.@@@@@.@@@@@...@@@
@@.@..@@.@@@@@@@@@@@@...@@.@@@@.@@@@..@..@..@@@@@@@.@.@@..@@@.@@@.@@@@@@@@@@@.@@@@...@@.@.@.@@..@.@@.@....@..@@.@.@@..@@@@@@...@@.@@@.@@
@.@@.@@.@@@@@..@..@@@@@@@@..@..@@.....@@@@.@.@@@@@.@@@.@@@@@@.@@@@.@.@.@.@@.@.@@.@@.@@@@@.@@@.@..@.@@...@.@@@@@@@@@..@@@...@.@.@.@.@@@@.
.....@@@@@@@.@@.@.@...@@@..@..@@.@@@@.....@..@.@@@@@.@@.@@...@@.@.@@.@@@.@@@.@@@@@@.@.@@@@@@..@@@@@@@@...@..@.@..@@@..@..@@.@....@@@@@@@
@@@@.@@@...@@.@.@@.@@.@.@@.@@@@.@@@@@@@@@@.@@@.@..@@.@@@.@@.@..@..@@@@.@..@@@@.@@@@...@@.@@.@..@@@@@@@.@@..@@@.@@@@@@.@@@.@@@...@.@@@@.@
.@.@@.....@.@@@@@@@...@..@@.@@..@.@.@@..@..@@..@@@@@@@...@@..@@@.@.@@@..@@.@@...@@..@@@@@@.@...@..@.@@@@@@@@@.@@.@@.@...@@@@@@@.@@@@@..@
@.@@@@@...@.@@@@@@@...@@.@.@@@@@@.@@@@@@..@..@.@.@...@@....@@@.@...@@@@@@....@@@..@@@@.@@..@.@@@@....@@@@@@@@@@@@@.@.@@.@@@@@@.@.@..@.@@
@@@@.@@@@@.@..@@@@@@@.@@@@@@@@@@@@@.@.@@.@@@@.@@@@@..@@@@@@.@....@@@@@@@@.@@@@@@.@.@..@@@@.@.....@.@.@@@@@@@@@@@@@@@@@@@@@...@@.@@.@.@.@
....@.@@@@...@.@@.@.@.@..@@.@.@@.@@@@@@@@@@@@@@@@.@@.@.@@@@@@@@.@@.@@@@.@@.@@@@@.@.@@@.@.@.@.@@@@..@..@@@.@@...@@.@...@@@@..@@@..@@@@@.@
@.@@@@..@@@..@@@@...@@@@@@@.@@.@@@..@..@.@@@.@@@@@@@@..@.@.@..@@@@..@@..@@@..@@@@....@....@.@@..@@.@@@@@@.@@@@..@.....@@..@@@@@@.@@@.@@@
@@@@@@.@@@@@@.@.@@@@.......@...@.@...@@.@...@@..@@@...@@@.@@@@@@@@@@@.@.@@@...@.@@@@@@@@@.@@@@@@@@..@.@@.@@..@.@@@@@@.@..@..@.@.@.@@..@@
@@@@.@@.@.@@.@@@@@@@.@@..@@..@@@.......@@.@.@.@@@@@.@@@@.@@.@@@....@@.@@@@@.@@.@@@@..@@@@.@@@@@@@@@@@@@@@@.@.@.@.@..@.@.@@@@@..@@@@.@@@@
@@.@@..@@@@@@.@@@..@.@.@.@.@@.@@.@@.@@@@@@@@@@.@@.@.@..@@@.@@@.@..@...@@@.@@@@@..@...@@@@.@@@@@@@@.@@.@@.@@@@.@@.@@@@..@@@....@@..@.@@@@
@@....@@..@..@@@@.@@@.....@@.@.@@.@.@.@@.@..@@@@@..@.@@@..@@.@@@.@@@....@@@..@@@@.@@.@.@..@@@....@@.@.@..@@@@@@@.@@...@..@@.@@.@.@@....@
..@@.@.@@.@.@.@@@@.@@@@.@.@@@@@@.@.@.@.@@.@@...@.@@@@@@@.@@@@@.@@@@.@@@....@..@@@..@@@.@@@@@@.@....@.@...@.@.@.@@@@@@..@@@@@@@.@..@@@.@@
@@@@@@@.@@.@@@@@...@@@@.@@@.@.@@@.@.@.@.@@@...@.@@@@.@@.@@@@@@.@@@@.@.@@.@@.@@@...@.@.@...@@@@@@@.@@@@@@.@@..@@@@@@@@...@@.@@@@@..@@@@.@
@.@@.@..@..@.@@@@@.......@..@@@..@@@.@.@@@....@@....@.@@@@@@@@..@@.....@@@@@.@@.@@@@@@@..@@@.@..@@.@@.@@@@.@.@.@@@@@@.@@.@.@@@.@@..@..@.
@@@.@.@@.@.@@@@..@.@......@.@@@..@@@@@.@..@@@@@@.@.@.@.@.@.@.@@.@@@@@.@.@@@@@...@@..@.@@.@@.@@..@@@@@@@@.@@@@.@.@@@.@@@@.@@.@@@@@@.@.@@@
@..@@@@.@@@@@.@..@@.@..@@@@@@@.@@@@@@.@@@...@..@.@@@@..@@.@@@@@.@.@@.@@@@@.@@@@@@@@@..@@@.@.@.@@.@@@@.@@@@@@@..@@@.@@@@@@@.@@@@@@@.@@@@.
.@.@@@.@@.@@@@.@@@@@.@@@@@.@.@@@@@.@@....@@@@@.@@.@@@..@@@@@@.@@@@@.@..@@...@@@@@@@@...@.@@@@@.@@@@@@@@..@@@@@@@@..@@..@@..@@@.@@.@.@@.@
@@.@.@.@@..@@@.@...@@@@@@..@.@@.@@@@@..@@@@@@@.@@@@.@.@.@@.@@@.@@@@@@@.@@@@.@@@@@..@@@@.@@@.@@@@@@@@@@.@..@.@@@@.@.@@@@@..@@.@@@@.@.@@@@
@@@..@@..@@@@@@@@@@.@.@.@@@...@.@@.@@@.@@@@@@@@@@@@@@@.@@@@@.@...@..@@.@@@.@@@@@...@.@.@.@.@@@@.@.@@..@@@@..@.@@@@.@@.@@.@.@@@@.@@.@..@@
...@@@@@@.@@.@.@@.@@@..@@@@@@..@@...@.@.@..@.@..@@@.@.@@@@@@@@@.@@.@@.@@@@@@..@@@@@@@.@@@@...@@@@@@@.@@.@@@.@@@@.@@@@..@@@...@....@@@.@@
@.@@@.@.@.@@@@.@.@@@.@..@@@@.@@@@.@@.@@..@@@@.@.@@.@@....@@@..@@.@@@@@.@@.@@.@.@@..@@..@@@@@.@..@@.@@@@@.@..@.@@@@@@.@.@@@@@@@@@@@@@@@@@
@.@@@.@@.@@@@..@.@.@..@@@@@...@@@@@@.@@@@.@.@@@@@@.@@...@.@@@..@.@@.@@.@@..@@..@@@@@@@@@..@.@@@@..@...@@..@.@..@@@....@@.@@@...@@@@@@.@@
@@.@@@@.@@@@.@@.@..@@.@@@@.@@@@..@@@.@@@@@@.@.@@@@@..@..@..@@..@@.@.@@.@@@.@..@@@.@@@@@@@.@@@@@@.@@.@@..@.@@@@@.@@@..@@.@.@.@....@@@..@.
@.@@.@@.@@.@@@.@@@@.....@.@@@.@@@..@.@@@@@@....@@.@@@@@@@..@@.@@.@@@@@.@@@..@@@.@@@.@.@@.@@@@@@..@.@....@@@@@.@.@@@@@@@.@@@@@.@@@@@@@.@@
@@@@.@@@@..@..@@@@@@@@.@@@@@...@@@.@@.@@@.@.@..@@@@@@.@@.@.@@.@..@@@@.@@@@@@@@@@@@.@@@@.@@@@.@.@@@.@@@@@@.@@@@....@..@@.@@@..@@@@...@..@
@@@@@.@.@@@..@.@.@....@@@@@..@@@@@@@.@@...@@@@@@.@@@@.@@@.@@@@@.@.@@.@@@@.@.@@@@@@@@@@..@.@@@.@.....@@@@.@.@@@..@@@@@.@.@@@@@@@.@@@@@.@.
@@@.@@.@@.@...@@.@@@@@@@@@@@.@@@.@.@..@..@.@.@@@...@@.@@.@.@@.@.@@@@@@..@.@.@...@.@.@.@..@@@@.@.@@@@@@@.@@@@@.@@.@@..@.@.@@..@@@.@@@@@.@
@@@@.@@.@@...@@@@@@...@.@@@@@@@@@.@@@@@@@@.@@@@@@.@@@@.@@.@@@@@.@@...@@.@...@@@..@@...@.@@@@@@.@....@.@@..@@@@..@.@.@@.@@@..@@.@@@.@.@..
...@@@@@@..@.@@.@.......@..@@..@@@.@@@@@..@.@.@@@@.@@...@...@@.@.@@@..@@.@@...@.@.@..@@....@@@.@..@@@@@..@@@.@@@@..@@@..@@@@@.@@@.@..@@.
@.@@.@..@.@@@@@.@@.@...@.@@.@.@..@@@@.@.@@@..@..@..@@.@@@@@@.@...@..@@@@@@@@.@@..@.@@@..@@@@@@@.@...@@@@.@.@@@@@@@.@..@.@@.@@..@@....@.@
.@.@@.@.@@.@@@@@@@@..@@@@.@@.@...@@..@@@@@@@@.@@@@@@@@@@@@@@@.@@@.@.@@@@@..@@.@@@@@@..@@@@@@@@@@.@@.@@.@@..@@@.@@.@@@.@..@@.@..@..@@.@.@
@.@@@....@.....@.@@@@..@@.@.@.@@@...@@@@@@..@.@.@@@..@@@@..@@@@@.@@@@@@.@@@@.@@.@@.@@@@@.@@@@@@@@.@@@.@@..@..@.@..@.@@@.@@@.@@@@@....@@@
@@.@..@@@@@@......@@@@.@...@.@.@@@.@.@@.@@@..@.@@@@@.@@@@@@@@@.@@@@.@.@@.@@@@@@@.@@@.@@@.@@.@@..@....@@@@@@@@@@@@@....@@@@.@.@..@.@@@.@.
@...@.@.@@@.@@..@@@@.@@@.@@.@..@@.@@..@@@@@@....@.@@@@.@@.@.@..@@@@..@..@..@@@@@@@@@@@@@.@@@@@@@.@@.@.@@@@@..@@@@.@@@@..@@@.@@.@.@@.@@@.
@@..@@@@@.@@@..@..@@@@@@@@@@@@@@@..@.@@.@@.@.@@@@...@.@@@@@@@@@@.@..@@@@@@@@@@@@@@@..@@@@@...@.@@..@..@@@@@@@@...@@@@.@@@@.@.@.@@@@@@@@@
@@.@.@@@.@.@@@@@@.@@.@@@.@@@...@@@..@@@@..@.@@@@@@@@.@@@....@@.@@@....@@@@@@@@@@.@.@@...@@.@@@@@@.@@..@@@@.@.@@@.@@.@@@.@@@.@@@.@@@..@@@
@.@..@@..@@@@.@@@..@@.@@@.@.@@@@@@.@....@..@..@@@@@@..@@@@@@@@.@.@@@@@..@@..@@@.....@@@.@.@@@@.@@.@@..@@@@...@@.@@@@...@.@@@@.@@.@@@@.@@
@@@@@.@.....@@@..@..@@..@.@....@@...@.@@.@@.@@@@@.@..@@.@@.@@..@@....@.@.@.@@....@@@..@..@.@@@@.@...@@..@@@.@@@..@@@.@.@@.@@@.@@@.@@..@@
@@@@.@@.@..@.@@@.@@@@.@.@@@@@@@.@@@@.@@@@.@..@@@@@@@@.@.@@.@.@@@.@@@@@.@@@@..@@@@@@...@.@@.@@@@@@@..@@..@@@.@@.@.@@@.@@.@@@@@....@@@@@..
.@@@.@@.@@@..@.@.@@.@@@.@@@@@@@@@@@@@.@..@@@..@@@@..@@.@@@.@@@@@@@@@@..@...@@@@@@@@@@.@@@@@@@@@@@...@....@@@..@@@@...@..@@@@...@@@@.@@..
@@@@@....@@@@@.@@@.@@.@.@@@@@...@.@@@.@@@@.@@@@@...@@@@@@...@@..@@.@@@@@.@@.@@@@..@....@@@.@@.@.@@@.@@@..@@...@@@..@@.@.@@@@@@@@.@.@.@.@
...@@@@.@@.@@.@@@@.@.@@@@@..@.@...@@@.@@@@@@@.@.@.@@@....@@@.@.@@.@@..@@.@@..@@@@..@.@@@@.@@...@.@@.@@@@@@@..@@..@@.@@.@.@@@@@@@@@..@@.@
.@@@@@..@@@@@@@@@.@.@@@@.@@....@@@@@.@@@@.@.@@@.@@@..@@@...@.@@..@.@.@@@.@.@@.@@.@@@@@@..@.@@.@@@@@..@..@@@@@...@.@@@@.@@@@@.@..@@.@..@@
@@.@.@@@@@@.@@@.@..@@.@@@.....@....@@.@@@@@@@@@@@@.@..@@.@...@.@.@@@@@@.@.@@@@@@@.@@@.....@@.@.@@@@@.@@.@.@.@.@@..@@@@@@@@.@@@.....@.@@@
@.@..@.@.@@@.@@.@@@.....@.@@@.@@@@@..@@.@@@.@@@@.@.@.@@.@...@..@@...@@@@.@.@@@.....@.@@@@@@@...@.@@@@@@.@@@..@@@@.@@@.@@...@.@@.@...@.@@
.@@.@@@@@@@@@@@..@@@.@@@@@@.@@@@.@@@.@@@@..@@@@@@.@@@@@@@@@@.@..@@...@@@...@@@@.@@.@.@.@@@.@@@@.@.@.@.@.@@@@..@@@@@...@@@@@@@@@@.@@.@...
..@@....@@....@@.@@@@@@.@@@@@@.@..@@@@@@@@..@@@@...@@@@@@@@.@...@.@@@@@@@@.@.@@@.@.@.@@@@.@@.@.@@@..@...@@.@.@@@@@@@.@@@..@.@@.@@.@@@@@@
@@@@@@@@..@..@@@.@.@@@.@@..@..@@@@@...@@@@@@.@.@@..@.@..@@@@..@..@....@@@.@...@...@..@@@@..@.@@@@@@@.@.@@@@.@@.@@@@@@@.@@@@.@@.@.@@@..@@
@.@.@.@..@@.@@..@@@@@@.@.@@@@@.@@.@@.@..@@@@...@@.@@@@@@.@@@@@@.@@@.@..@@@@.@@@@.@.@@.@@.@@@@@.@@..@@@@@@.@@@@.@..@@.@@@...@@@@.@@.@@@..
@@@@@@..@@.@...@.....@@@.@.@@@@@@@@@@..@.@.@..@..@@@...@...@@.@@.@@@@@@.@@.@..@@.@@..@@@..@@@@..@@@@@@@@..@@@@@@.@@.@...@.@..@..@..@@.@@
@.@.@.@...@@@@@@@@@@@@..@@@...@@@.@@@@@@@.@@.@@.@.@.@@@@@.@@.@.@@.@@@@@@@@@....@.@@.@..@@@.@@@..@@@.@.@@@@@@.@....@@....@..@@@@..@@@@@@.
@@@@@@..@....@.@..@@.@@..@.@.....@.@@@@@.@@@@...@@@@.@.@@@...@@@@@@@@@.@@.@@@.@@@.@@@@.@.@@.@@.@..@.@@@@@.@.@.@..@@.@@@.@@@...@@@..@@.@@
@..@@.@@@@@@@.@@@.@..@.@@@.@@@...@@@@...@.@@@@@.@@..@@@..@@@.@@.@@.@...@@@@.@@@.@@@@@@.@.@@@@@@.@..@@@....@@@...@@@@@@..@@@.@.@.@@.@@@.@
@.@@..@.@@@@.@@@@@..@@@@.@@@.@@@@@.@@...@@.@.@@.@@@.@@.@@@@..@@@.@..@@@.@@@@...@@.....@@@@@..@@@@.@@@@...@.@@..@@@@@..@..@@@..@@@..@.@@@
@..@@.@@@@@.@@@@@@.@@..@.@@@@@@..@...@@@@.@@.@@@@@.@@@@@@@.@@.@@...@@.@@@@.@.@@.@@@@.@@@@@.@@@.@.@@@@@@.@@@.@@@@@.@..@.@@@...@.@@.@...@@
@@@@@.@@@@@@@@@@.@..@@@...@@.@.@@.@@.@@@@@....@@@..@..@.@..@..@@.@@...@@.@@@.@@.@.@.@@@@@.@@@@.@...@..@@@@@.@@@@.@@.@@.@@@.@.@@@@.@@@@@@
.@..@@@.@@@@@@@@@@.@@@@@@@.@..@@.@@@.@.@@@@@.@..@.@@....@@@@.@@@.@@..@@@@@@@@.@@@..@.@.@.@@@@.@@..@.@@@@@@@@@@@@@@@@@...@@@.@.@@@@..@@@.
@@@.@..@@.@@@@..@.@@@.@@@.@@@@@.@...@@.@..@@@@@.@..@@@.@@..@.@.@@@@....@@@@@..@@@.@@.@.@@@.@@.@@..@.....@@@.@@.@@@..@@@.@.@.@@@@..@@..@.
@@.@@@@@@..@@@@@@.@@@@......@.@@@@@.@.@@@.@..@@....@@...@...@.@.@@@.@..@.@.@@@.@@.@@@@@@@...@@.@@...@.@@@@.@@@@@@@@@.@@@.@..@@.@@.@@..@@
@@@.@@.@@@@@@.@@@@@@.@@...@....@.@@@@@.@....@@@@....@@.@@@.@....@@@@@@.@@@..@@@..@.@@.@@@.@.@..@@.@.@..@@..@@.@.@.@@@.@@.@@.@.@@.@@.@@@.
.@.@...@.@.@@.@@@.@@..@..@@.@@@..@@@@@.@.@@@....@@.@@.@@.@@@.@@@@@.@@..@@@@@@....@@@@.@@.@@.@@@.@@@.@@@.@@.@@.@.@@@.@@@@@@@@@@@@@@@@@@@@
@@..@@@@@@@.@@@@@..@.@@.@@@@.@@@@@@@.@.@..@@..@.@@.@@@@@@.@@.@@@.@..@@.@@@@@@.@@@.@@@@@..@.@.@@..@@@.@@.@.@@@@.@.@@@@.@.@.@.@@@.@.@@@@@@
.@.@@@@.@.@.@@@@.@@.@.@@..@...@..@@...@.@@.@.@@@@@@@@@@@.@@@.@@@.@@.@@@@@@@@...@@.@.@..@@@@@@@.@.@@@.@@@.@@@@@..@@@@@.@@@..@..@@.@.@.@@@
@@@.@@@@@@.@@.@@@@@.@@@.@@@@.@@@@@@@@@@@@@.@@.@.@@.@..@.@@.@@@..@.@@..@.@.....@.@@@@@.@@@@..@@@.@.@@@@@@@@@@@@.@@@@@.@..@@..@@@.@@@@.@..
@.@@..@@.@@@.@@@@@@..@@@.@@.@@@.@@.@@..@@@@.@@@..@@@..@.@@@@@.@.@.@..@@@.@.@@....@@.@@..@@@.@.@@@@@@.@@.@.@..@@...@@.@..@@.@@@.@@@@@.@@@
@@@@.@@..@.@@.@@@@@@@@@@@..@@.@.@..@.@.@@@@..@@@@@@@.@@@.@@@@@@@@@@@..@.@@@@@@.@@@.@@@@@.@@@.@.@.@@@@@@@@.@@@@.@@@@@@......@@...@@..@@.@
@.@.@.@.@@@@.@@@@.@@@@@.@@..@@@@...@@@.@@.@@.@.@@@.@@.@@@@....@@@@.@@@@@@@@@.@@@.@@@@@..@.@.@@.@.@..@.@..@@@@.@@.@@.@.@.@..@@@@@@@.@@@@@
@@..@...@@@@@@..@@@@@@@..@@@@.@@@@.@.@@..@.@....@@@@@@.@@.@.@@@.@@@@@@@@@.@.@@..@..@.@@..@@.@@...@@@.@.@..@@@@@@@.@@@@@@@.@@@@.@@.@@.@..
@@....@@@@@@@.@@.@@..@@@.@.@@.@@.@@@@@@@.@@@@@@@.@.@..@@@@.@...@.@@.@.@@@.@@@@.@@@@@.@..@@@..@@.@@@@@@@...@@@@@.@@@@.@.@@..@@..@..@@...@
@@.@..@@@..@.@@@@@@@@.@@.@..@@@@@@@.@@@@..@@.@@@@.@.@@@.@.@@@@.@...@.@@@@.@.@@@.@.@@..@.@@@@@@@@@@...@.@@.@@@@.@..@@@@@@@..@@.@.@.@@@@@@
@.@.@@@.@@@@@@@@@@.@.@.@.@..@@@@@@.@@@@@@@.@.@@@@@.@@@.@@@@@..@@@.@@@@@@@@@@@@@@@.@@@@@@.@@@@@@...@.@@@@@@..@..@..@.@@@@@@@.@.@@...@.@.@
.@@@@@.@..@.@.@@@@@.@@@@@@@.@@@..@@@..@@...@.@@@@@.@@..@.@@.@.@@@@@@@@@.@@.@@@@@@@..@@.@.@@..@@..@.@@.@@@@@@.@@.@@@@@@@@@@.@@@.@@@...@@.
@.@@..@@..@@@.@.@@@@.@@@.@@@@...@@@@@@.@@@@@.@@.@@..@.@.@@@.@@.@@@@..@...@.@@@..@@@@.@@@@..@.@..@@.@@.@.@...@.@...@@@@.@@@@.@.@.@@@@@.@@
@@.@@@.@@@@@.@@..@..@.@@..@..@@...@@@@....@@.@@@.@@@@@@@@.@..@@@.@.@@@.@@.@.@@.@@@@.@.@.@@@@@@@@@@.@@..@.@@@@@@...@@@@@@@@@@@@@@@@@...@@
.@.@@@@.@...@@@@...@@@..@@@@@..@@.@@..@@@.@@.@.@@@@@@@@@@@.@@.@@.@.@@@@.@@@....@.@@.@@.@@.@@.@.@..@@@.@..@..@.@@@@@..@.@@.@.@@.@@@@@@@@@
@@@..@@@.@@@@@@..@.@@@@.@@@@.@.@@@@@@...@@.@.@@@@..@@@.@.@@@.@.@.@@@..@@.@@@@@@@...@@.@@@@@@.@@@.@@@@.@@@.@@@@@.@@@@...@.@.@..@@@@@@@...
...@.@@@@@.@@@@@@@.@@@...@..@@@@@.@@@@.@@@..@@@@@.@@@@..@.@.@@@@.@@.@.@@.@@@.@...@.@.@@@@.@@.@..@@...@@..@.@.@@@@.@.@..@@@..@@.@..@.@@@@
@@.@.@@..@.@..@@@@@@@@@.@...@@@.@.@...@@@.@@@@.@@.@@...@@..@@.@@.@@.@.@.@@@@@@@@..@@@@..@@@@..@...@@.@.@@@@.@@@@@...@@@...@.@.@.@@.@@@@@
@.@@.@.@..@.@@@.@@@@.@@@@@@@.@.@.@@@@@.@@@.@@.@@@@..@..@@.@.@...@.@@@@@@@.@..@..@@@.@@.@.@@.@@.@@.@@@@@@@@@@@..@@..@.@@.@@@..@.@@@@@@@..
@@.@@.@.@@.@@.@@.@@@...@@@.@..@@.@.@@@@@@.@.@.@.@@..@.@@.@@@@@@..@@@@@.@..@.@.@@@@.@@.@@@@@@@@@.@@.@@@@@@@@..@@.@.@@@.@.@@.@@@.@.....@.@
@@..@@.@..@.@@@@..@@.@@..@@@@@@@@@@@.@@.@@..@.@@@....@..@.@@@@.@@@@@@@@..@@.@.@..@.@@@@@@@@@@@@.@@.@@.@@.@.@.@@@@...@..@@@.@@@@@@@@.@.@@
.@..@@@.@@@.@.@....@.@@.@@..@@@@@@@@@.@...@@.@@..@@@.@.@@..@.@@.@...@@...@@@@@.@@.@.@@@@@@.@.@@..@@@@@@.@@.@.@@@@.@@@@@.@.@@@.@...@@...@
@.@@..@@@@.@@.@@@@@.@@.@..@@@@.@@@@@@@@@.@@.@@@.@@@@.@@@.@@@@@.@@.@@...@.@@@@@@..@@@..@.@@@@@.@.@@@@...@@@@@.@.@@.@@@@@.@@@...@.@@@@@.@.
.@@...@@..@...@@@.@..@.@@@.@@.@..@@@.@@..@@@@@.@..@@@@.@@..@@@.@@..@@@...@@.@.@.@..@@@@@.@@@...@@@@.@.@.@.@..@.@.@..@@@@@@@..@@@@@.@.@@@
@@@@.@@@@...@.@@.@@@@.@@@@.@@@@@@@.@.@@@@@@..@.@.......@...@@.@..@.@@.@@..@.@@@@@.@@@@.@@...@@.@@...@.@@..@@@.@@.@.@@@@@@@@..@..@@@@@.@.
@.@@@@.@.@@@.@@@.@.@@.@@@@.@.@@@.@.@.@.@.@@@@@@..@.@.@@@..@.@@@@@@..@@@@@@@@@@@@.....@@...@..@@@@@@@@@@.@@@..@.@...@@.@@@...@@@.@@@@..@@
@@@..@.@@.@@@@@@.@@.@@.@@@@.@@@@@@@@.@.@.@@@@@.....@@@.@@.@@@@@@@@@@.@@.@@..@@@.@.@@@@.@.@@@@.@@...@.@..@@.@@@..@....@@@@@.@@@@.@@@.@@@@
....@@@@@..@@@.@@...@@.@@@@.@@@.@..@@..@@@@.@..@@@@.@@.@@.@@@@@@.@@...@@@@.@@@@.@.@@@@@.@@@@..@@.@@.@@@.@..@@@@@.@@@@@.@@@@@....@.@@@@@@
@.@.@@@@.@@..@@@@@@@@@.@.@@.@.@@@.@@@@.@@.@@..@@..@@@.@@.@@@@@@.@...@...@@..@.@@..@@@@@@.@@..@@@...@.@.@@..@@@@...@@.@@@..@@@@@@@@...@..
@@.@@.@.@@@@..@@..@@@@@@@@@@@.@@..@@.@@.@.@@.@.@@....@@@..@@..@@.@@@..@@.@@.@.@@@.@@..@@.@@@...@@@..@@..@@.@..@@..@.@@@.@@@..@.@@@@@@@..
.@@@.@@@@@@@@.@@..@.@.@@@@@@@@@@@@@.@@.@@.@@@..@....@@.@.@@@@@.@..@@..@@@@@@.@@@@..@@..@@..@@.@@@@..@.@@@.@.@.@@@@@@..@.@.@.@....@@@@@@@
@@@@.@.@@@@.@.@.@@@.@@@@@@@@@@@@...@@@.@.@.@@@.@@@@@@.@@.@@@.@@..@...@@@@@..@@.@@..@@@@....@@@.@@.@@@@@@@@.@.@@@@@@@..@@@@@@.@@..@@.@@@.
.@..@@@@.@.@@@@.@....@@..@@..@@@@..@@@@@@@@@@@@@@@@@.@@@..@@@@@@@@.@.@@@@@..@@@..@...@@.@...@@@@....@@@@@..@..@@@.@@@@.@..@@@....@@@...@
..@@@@@.@.@@@@@@@.@@.@@@@@@@@@@@@@..@@@@@@@..@@@..@.@.@@.@@@@@@@@@@@@@@@@...@@@.@@@@@@@@.@@@@@@@.@@.@.@.@..@.@..@@.@...@.@...@.@@.@.@@.@
@@@@....@.@.@@..@@@@@@.....@@@@.@.@.@@.@@@.@@@@@..@..@.@@@..@@.@@@@...@.@@.@@@..@.@.@@@@.@@@.@.@@@@@...@.@@@.@@..@@@@@.@@..@@@@..@.@@@..
@@..@@.@.@@.......@@@@@@@@@@@@@.@@@@..@@@@@@.@@@@.@.@@.@..@@@@@@..@@@...@@.@.@.@@@@...@@@@@@@.@.@@..@.@@@@@@@.@.@@.@.@@@@@.@@@@@@@@@@@.@
@@@@@..@..@@.@@@..@.@@@@.@.@@@@.@@@@@@@@@@...@@@@@@@..@.@@@..@@.@@@@.@@@...@...@@@@@.@@.@@@.@...@@@.@@@..@@@...@.@@@@..@@@@@@@@@@.@@@@@@
@@@@@..@@.@@@.@.@@@.@.@.@..@@@@..@@@@@..@@.@@@@@.@@.@.@@@@@@@@.@@@.@@..@..@@.@.@@.@....@@@.@..@..@.@@@.@@@.@..@.@@.@@@@.@@@@@@...@@@@@@.
.@@@@.@.@@.@@.@@@..@@@..@.@@...@@.@@.....@.@@@@@@@.@@@@.@.@@@.@@@..@@.@.@@@@@@.@.@@@....@...@@@.@..@@@@@@@@@.@...@....@@@@@@@@..@@.@..@.
@@.@@@@..@@@.@@@@.@..@@@..@.@..@@.@@@.@@..@.@@@@@@.@@@@.@@@.@@@.@.@@..@.@@@...@@@@..@..@@@..@@@@@@..@@@@@@@@@@.@@@@@@.@.@@.@@.@@.@..@@.@
@..@...@@.@@@@@@@@@@@@..@@....@@.@..@@@.@@......@.@@.@@@..@@@..@.@.@..@.@@@.@.@@@..@@@.@@@@@@@@@.@@.@@@@@.@@@@.@.@@.@.@@@@@@@@.@@@.@@.@.
.@.@@@@@@..@.@@@..@@@@@@@@.@.@@@@@@@..@@.@@.@.@.@@@@@.@@...@..@.@@@@@@@@.@.@@@@.@@...@@@@..@.@.@@..@..@.@..@..@..@@@@.@@@@@@@.@@.@.@.@@.
.@@@@@@.@@@.@@.@.@@@@@.@.@@@@@@@.@@@.@@@..@@@.@@@@@@@.@.@@@@.@@@.@@@.@.@@@...@.@@@@@...@@.@@@@@.@@@@@@@@@@.@@@@@@@@@@.@@@.@.@@@.@@@@.@@.
@@...@..@....@@@.@.@.@@..@.@...@..@.@.@@@@@.@.@@.@@@@@..@@@@@.@....@@@@..@@...@@...@..@@@@@@@@@@..@@@@.@@@@.@..@.@@@@....@.@@@@@.@@@.@@@
.@@@@..@@@.@.@@..@@@@@.@@..@@.@@@@@@.@@@@.@@@.@...@@@...@@@@@@@@@@.@@.@@@@.@@.....@@@@@..@...@......@....@@.@....@.@@@..@@.@@..@@.@..@@@
.@@..@@@....@@.@...@@@@..@@@@...@@.@@.@@.@@.@@.@@@@@@.@.@@@...@@.@@@@.@@....@@@..@.@@.@.@@@....@..@@.@@.@@@@@.@@@.@.@@@@@@@.@.@.@@.@.@@@
.@@@@@.@.@@.@@@.@@.@.@.@@@.@...@@@@.@@..@..@@@@@@@@.@@@..@@@..@@.@..@@@@@@.@@@@@...@...@.@..@@@@@.@@@@@@.@@.@..@@..@..@@.@..@@@..@.@.@..
.@@..@@@@......@@@..@@@@@@@@@@@@@.@.@..@@.@@@@@@.@@@.@@@..@@@.@...@@..@@@@.@@@...@@@@@@......@.@@@@@@.@..@.@@.@.@.@@@@@@.@.@@.@@@@.@.@.@
@@.....@.@@.@@@@@@.@.@@.@.@@.@.@@@@@@@.@.@@.@@@....@@.@@.@@@..@@.@@@@@...@@@@@@.@@.@..@@.@@..@.@@.@..@@@.@@@@@.@@@@@@.@@@@..@@..@@@@@...
..@..@@..@.@.@@@.@@@@@.@.@.@@@@..@@@...@@@@@.@@@.@.@@@.@@@@@@@@@@@@@...@.@.@@@.@@.@.@@.@@@@@@@@@@..@@.@@.@@@@@@@@@@@@.@..@.@@@.@@@...@@.
@@@...@.@.@@.@@..@@.@.@@@.@@@@@@@@@@.@@@@@@@@..@.@@@@.@@@@@@@..@@@@@@@..@@@.@.@@.@@@@.@.@...@@..@@@.@..@.@@@@@.@@@.@@@@@@.@@@@..@@.@..@@Analyzing the problem, we can clearly observe that the only thing we have to do is to iterate through every position, check if the current position holds a roll of paper and verify if there are fewer than 4 roll of papers in the 8 adjacent positions.
We start by saving the grid into a buffer matrix of length (assume that the grid is always a square). Let’s define dir_x and dir_y to indicate every possible shift from the current position in all 8 directions.
section .data
dir_x db 0, 0, 1, 1, -1, 1, -1, -1
dir_y db 1, -1, 1, -1, 0, 0, 1, -1For a roll of paper at to be accessible by the forklift, the following conditions must hold:
excluding the out-of-bound positions.
To determine if a shift in one specific direction is out-of-bound, normally we can use the following simple condition (for example, using Python):
nx = x + dir_x[i]
ny = y + dir_y[i]
if nx < 0 or ny < 0 or nx >= m or ny >= m:
# Skip this shiftHowever in x86 Assembly, any negative value will be wrapped to a very large integer based on the register/memory region size, for example:
xor rax, rax
dec rax ; rax now contains 0xFFFFFFFFFFFFFFFFTo effectively implement the condition for out-of-bound check, we should utilize movsx and jae instruction:
; r10 contains m - the grid height/width
movsx rax, byte [dir_x + r15]
add r8, rax
movsx rax, byte [dir_y + r15]
add r9, rax
cmp r8, r10
jae .next_pos
cmp r9, r10
jae .next_posThe trick here is that if we use jge instead of jae - in case either r8 or r9 is negative, the processor identifies that a negative value is smaller than r10 (a positive value), thus it does not jump to the label for continuing with the next position.
jae instead makes the processor registers the negative value as a very high positive value, and is definitely above the value in r10. Hence, jae will also catch the negative overflow case.
The rest of solver component is implemented like so:
; ----------------------------------------------------------------------
; Component: Solver
; Args: rdi, rsi
; Ret: result += 1 if matrix[x * m + y] == '@' and sum([matrix[(x + dir_x[i]) * m + (y + dir_y[i])] == '@' for each i in [0, 7]]) < 4
solver:
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
mov r10, rsi
mov r14, rdi
xor r13, r13
.read_loop:
cmp r13, r14
je .exit_loop
mov rax, r13
xor rdx, rdx
div r10
mov r11, rax
mov r12, rdx
movzx rax, byte [matrix + r13]
cmp rax, '@'
jne .continue_read_loop
mov qword [adjacent], 0
xor r15, r15
.dir_loop:
cmp r15, 8
je .check_result
mov r8, r11
mov r9, r12
movsx rax, byte [dir_x + r15]
add r8, rax
movsx rax, byte [dir_y + r15]
add r9, rax
cmp r8, r10
jae .next_pos
cmp r9, r10
jae .next_pos
mov rax, r8
mul r10
add rax, r9
movzx rax, byte [matrix + rax]
cmp rax, '@'
jne .next_pos
add qword [adjacent], 1
.next_pos:
inc r15
jmp .dir_loop
.check_result:
cmp qword [adjacent], 4
jge .continue_read_loop
add qword [result], 1
.continue_read_loop:
inc r13
jmp .read_loop
.exit_loop:
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
retAnd finally together with the rest of the file, we have the working solution for the challenge.
section .data
filename db "../problem/input.txt", 0
dir_x db 0, 0, 1, 1, -1, 1, -1, -1
dir_y db 1, -1, 1, -1, 0, 0, 1, -1
section .bss
input resb 100000
matrix resb 20000
output resb 20
result resq 1
adjacent resq 1
section .text
global _start
_start:
; Open input file
mov rax, 2
mov rdi, filename
xor rsi, rsi
xor rdx, rdx
syscall
mov rbx, rax
; Read input file
xor rax, rax
mov rdi, rbx
mov rsi, input
mov rdx, 100000
syscall
; Close the file
mov rax, 3
mov rdi, rbx
syscall
; Split the lines and process them one by one
mov r8, input
xor r9, r9
mov qword [result], 0
mov r15, matrix
reader:
movzx rax, byte [r8]
test rax, rax
je solve_part
cmp rax, 10
jne continue_reader
inc r8
inc r9
jmp reader
continue_reader:
mov [r15], rax
inc r8
inc r15
jmp reader
solve_part:
mov rdi, r15
sub rdi, matrix
mov rsi, r9
call solver
; Print result to stdout
mov rdi, qword [result]
call print_int
; Exit the program
mov rax, 60
xor rdi, rdi
syscall
; ----------------------------------------------------------------------
; Component: Print Integer
; Args: rdi
print_int:
mov rax, rdi
mov rcx, output
add rcx, 19
mov byte [rcx], 10
mov rbx, 10
.loop:
dec rcx
xor rdx, rdx
div rbx
add dl, '0'
mov [rcx], dl
test rax, rax
jnz .loop
; Calculate length
mov rdx, output
add rdx, 20
sub rdx, rcx
; Print
mov rax, 1
mov rdi, 1
mov rsi, rcx
syscall
ret
; ----------------------------------------------------------------------
; Component: Solver
; Args: rdi, rsi
; Ret: result += 1 if matrix[x * m + y] == '@' and sum([matrix[(x + dir_x[i]) * m + (y + dir_y[i])] == '@' for each i in [0, 7]]) < 4
solver:
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
mov r10, rsi
mov r14, rdi
xor r13, r13
.read_loop:
cmp r13, r14
je .exit_loop
mov rax, r13
xor rdx, rdx
div r10
mov r11, rax
mov r12, rdx
movzx rax, byte [matrix + r13]
cmp rax, '@'
jne .continue_read_loop
mov qword [adjacent], 0
xor r15, r15
.dir_loop:
cmp r15, 8
je .check_result
mov r8, r11
mov r9, r12
movsx rax, byte [dir_x + r15]
add r8, rax
movsx rax, byte [dir_y + r15]
add r9, rax
cmp r8, r10
jae .next_pos
cmp r9, r10
jae .next_pos
mov rax, r8
mul r10
add rax, r9
movzx rax, byte [matrix + rax]
cmp rax, '@'
jne .next_pos
add qword [adjacent], 1
.next_pos:
inc r15
jmp .dir_loop
.check_result:
cmp qword [adjacent], 4
jge .continue_read_loop
add qword [result], 1
.continue_read_loop:
inc r13
jmp .read_loop
.exit_loop:
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
retThe result of this puzzle is 1395.
Part 2
Now, the Elves just need help accessing as much of the paper as they can.
Once a roll of paper can be accessed by a forklift, it can be removed. Once a roll of paper is removed, the forklifts might be able to access more rolls of paper, which they might also be able to remove. How many total rolls of paper could the Elves remove if they keep repeating this process?
Starting with the same example as above, here is one way you could remove as many rolls of paper as possible, using highlighted @ to indicate that a roll of paper is about to be removed, and using x to indicate that a roll of paper was just removed:
Initial state:
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
Remove 13 rolls of paper:
..xx.xx@x.
x@@.@.@.@@
@@@@@.x.@@
@.@@@@..@.
x@.@@@@.@x
.@@@@@@@.@
.@.@.@.@@@
x.@@@.@@@@
.@@@@@@@@.
x.x.@@@.x.
Remove 12 rolls of paper:
.......x..
.@@.x.x.@x
x@@@@...@@
x.@@@@..x.
.@.@@@@.x.
.x@@@@@@.x
.x.@.@.@@@
..@@@.@@@@
.x@@@@@@@.
....@@@...
Remove 7 rolls of paper:
..........
.x@.....x.
.@@@@...xx
..@@@@....
.x.@@@@...
..@@@@@@..
...@.@.@@x
..@@@.@@@@
..x@@@@@@.
....@@@...
Remove 5 rolls of paper:
..........
..x.......
.x@@@.....
..@@@@....
...@@@@...
..x@@@@@..
...@.@.@@.
..x@@.@@@x
...@@@@@@.
....@@@...
Remove 2 rolls of paper:
..........
..........
..x@@.....
..@@@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@x.
....@@@...
Remove 1 roll of paper:
..........
..........
...@@.....
..x@@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@..
....@@@...
Remove 1 roll of paper:
..........
..........
...x@.....
...@@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@..
....@@@...
Remove 1 roll of paper:
..........
..........
....x.....
...@@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@..
....@@@...
Remove 1 roll of paper:
..........
..........
..........
...x@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@..
....@@@...
Stop once no more rolls of paper are accessible by a forklift. In this example, a total of 43 rolls of paper can be removed.
Start with your original diagram. How many rolls of paper in total can be removed by the Elves and their forklifts?TL;DR: Every time a roll of paper is accessible by the forklift, we remove such roll - which may enable accessibility for more rolls of paper.
To solve this problem is a very easy task. We just need to update our previous solver logic to loop until no more roll of paper is accessible, while replacing each to-be-removed roll of paper with . to indicate an empty position.
; ----------------------------------------------------------------------
; Component: Solver
; Args: rdi, rsi
; Ret: result += 1 if matrix[x * m + y] == '@' and sum([matrix[(x + dir_x[i]) * m + (y + dir_y[i])] == '@' for each i in [0, 7]]) < 4
solver:
push rbx
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
mov r10, rsi
mov r14, rdi
.forklift_again:
mov rbx, qword [result]
xor r13, r13
.read_loop:
cmp r13, r14
je .exit_loop
mov rax, r13
xor rdx, rdx
div r10
mov r11, rax
mov r12, rdx
movzx rax, byte [matrix + r13]
cmp rax, '@'
jne .continue_read_loop
mov qword [adjacent], 0
xor r15, r15
.dir_loop:
cmp r15, 8
je .check_result
mov r8, r11
mov r9, r12
movsx rax, byte [dir_x + r15]
add r8, rax
movsx rax, byte [dir_y + r15]
add r9, rax
cmp r8, r10
jae .next_pos
cmp r9, r10
jae .next_pos
mov rax, r8
mul r10
add rax, r9
movzx rax, byte [matrix + rax]
cmp rax, '@'
jne .next_pos
add qword [adjacent], 1
.next_pos:
inc r15
jmp .dir_loop
.check_result:
cmp qword [adjacent], 4
jge .continue_read_loop
add qword [result], 1
mov byte [matrix + r13], '.'
.continue_read_loop:
inc r13
jmp .read_loop
.exit_loop:
cmp rbx, qword [result]
jne .forklift_again
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rbx
retThis results in the following update full solution.
section .data
filename db "../problem/input.txt", 0
dir_x db 0, 0, 1, 1, -1, 1, -1, -1
dir_y db 1, -1, 1, -1, 0, 0, 1, -1
section .bss
input resb 100000
matrix resb 20000
output resb 20
result resq 1
adjacent resq 1
section .text
global _start
_start:
; Open input file
mov rax, 2
mov rdi, filename
xor rsi, rsi
xor rdx, rdx
syscall
mov rbx, rax
; Read input file
xor rax, rax
mov rdi, rbx
mov rsi, input
mov rdx, 100000
syscall
; Close the file
mov rax, 3
mov rdi, rbx
syscall
; Split the lines and process them one by one
mov r8, input
xor r9, r9
mov qword [result], 0
mov r15, matrix
reader:
movzx rax, byte [r8]
test rax, rax
je solve_part
cmp rax, 10
jne continue_reader
inc r8
inc r9
jmp reader
continue_reader:
mov [r15], rax
inc r8
inc r15
jmp reader
solve_part:
mov rdi, r15
sub rdi, matrix
mov rsi, r9
call solver
; Print result to stdout
mov rdi, qword [result]
call print_int
; Exit the program
mov rax, 60
xor rdi, rdi
syscall
; ----------------------------------------------------------------------
; Component: Print Integer
; Args: rdi
print_int:
mov rax, rdi
mov rcx, output
add rcx, 19
mov byte [rcx], 10
mov rbx, 10
.loop:
dec rcx
xor rdx, rdx
div rbx
add dl, '0'
mov [rcx], dl
test rax, rax
jnz .loop
; Calculate length
mov rdx, output
add rdx, 20
sub rdx, rcx
; Print
mov rax, 1
mov rdi, 1
mov rsi, rcx
syscall
ret
; ----------------------------------------------------------------------
; Component: Solver
; Args: rdi, rsi
; Ret: result += 1 if matrix[x * m + y] == '@' and sum([matrix[(x + dir_x[i]) * m + (y + dir_y[i])] == '@' for each i in [0, 7]]) < 4
solver:
push rbx
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
mov r10, rsi
mov r14, rdi
.forklift_again:
mov rbx, qword [result]
xor r13, r13
.read_loop:
cmp r13, r14
je .exit_loop
mov rax, r13
xor rdx, rdx
div r10
mov r11, rax
mov r12, rdx
movzx rax, byte [matrix + r13]
cmp rax, '@'
jne .continue_read_loop
mov qword [adjacent], 0
xor r15, r15
.dir_loop:
cmp r15, 8
je .check_result
mov r8, r11
mov r9, r12
movsx rax, byte [dir_x + r15]
add r8, rax
movsx rax, byte [dir_y + r15]
add r9, rax
cmp r8, r10
jae .next_pos
cmp r9, r10
jae .next_pos
mov rax, r8
mul r10
add rax, r9
movzx rax, byte [matrix + rax]
cmp rax, '@'
jne .next_pos
add qword [adjacent], 1
.next_pos:
inc r15
jmp .dir_loop
.check_result:
cmp qword [adjacent], 4
jge .continue_read_loop
add qword [result], 1
mov byte [matrix + r13], '.'
.continue_read_loop:
inc r13
jmp .read_loop
.exit_loop:
cmp rbx, qword [result]
jne .forklift_again
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rbx
retWhich, returns 8451 as the final result for the second part of this challenge.