Solution in Python for the day 11 puzzle of the 2019 edition of the Advent of Code annual programming challenge.
On the way to Jupiter, you're pulled over by the Space Police.
Attention, unmarked spacecraft! You are in violation of Space Law! All spacecraft must have a clearly visible registration identifier! You have 24 hours to comply or be sent to Space Jail!
Not wanting to be sent to Space Jail, you radio back to the Elves on Earth for help. Although it takes almost three hours for their reply signal to reach you, they send instructions for how to power up the emergency hull painting robot and even provide a small Intcode program (your puzzle input) that will cause it to paint your ship appropriately.
Intcode it is!
There's just one problem: you don't have an emergency hull painting robot.
You'll need to build a new emergency hull painting robot. The robot needs to be able to move around on the grid of square panels on the side of your ship, detect the color of its current panel, and paint its current panel black or white. (All of the panels are currently black.)
The Intcode program will serve as the brain of the robot. The program uses input instructions to access the robot's camera: provide 0 if the robot is over a black panel or 1 if the robot is over a white panel.
Input Value | Panel Color |
---|---|
0 |
Black |
1 |
White |
Then, the program will output two values:
- First, it will output a value indicating the color to paint the panel the robot is over: 0 means to paint the panel black, and 1 means to paint the panel white.
- Second, it will output a value indicating the direction the robot should turn: 0 means it should turn left 90 degrees, and 1 means it should turn right 90 degrees.
Output Values | Applied Paint | Direction |
---|---|---|
0, 0 |
Black | Left 90 degrees |
0, 1 |
Black | Right 90 degrees |
1, 0 |
White | Left 90 degrees |
1, 1 |
White | Right 90 degrees |
After the robot turns, it should always move forward exactly one panel. The robot starts facing up.
Initial robot position is (0, 0)
heading is North.
The robot will continue running for a while like this and halt when it is finished drawing. Do not restart the Intcode computer inside the robot during this process.
For example, suppose the robot is about to start running. Drawing black panels as ., white panels as #, and the robot pointing the direction it is facing (< ^ > v), the initial state and region near the robot looks like this:
..... ..... ..^.. ..... .....
Memory storage relying on dict
are a good fit for situations where there may be an arbitrary number of items and quickly lookup and updates are required.
The robot's heading can be represented by the pointy side as follows:
Heading | Char |
---|---|
North | ^ |
East | > |
South | v |
West | < |
The panel under the robot (not visible here because a ^ is shown instead) is also black, and so any input instructions at this point should be provided 0. Suppose the robot eventually outputs 1 (paint white) and then 0 (turn left). After taking these actions and moving forward one panel, the region now looks like this:
..... ..... .<#.. ..... .....
Input instructions should still be provided 0. Next, the robot might output 0 (paint black) and then 0 (turn left):
..... ..... ..#.. .v... .....
After more outputs (1,0, 1,0):
..... ..... ..^.. .##.. .....
The robot is now back where it started, but because it is now on a white panel, input instructions should be provided 1. After several more outputs (0,1, 1,0, 1,0), the area looks like this:
..... ..<#. ...#. .##.. .....
Ok, nothing too fancy.
Before you deploy the robot, you should probably have an estimate of the area it will cover: specifically, you need to know the number of panels it paints at least once, regardless of color. In the example above, the robot painted 6 panels at least once. (It painted its starting panel twice, but that panel is still only counted once; it also never painted the panel it ended on.)
Ok, so we are going to keep a list of all the panels painted at least once.
Build a new emergency hull painting robot and run the Intcode program on it. How many panels does it paint at least once?
Sounds fun!
Looking down from the top-level yields the following tree:
- shell
main()
load_contents()
solve()
step()
paint_panel()
The main() method remains unchanged from the previous puzzles.
def main() -> int:
args = parse_arguments()
configure_logger(verbose=args.verbose)
compute_part_one = not args.part or 1 == args.part
compute_part_two = not args.part or 2 == args.part
if compute_part_one:
contents = next(load_contents(filename=args.filename))
answer = solve(contents=contents)
print(f'part one: {answer=}')
if compute_part_two:
answer = -1 # TODO
print(f'part two: {answer=}')
return EXIT_SUCCESS
The puzzle input being an Intcode program, we can go ahead and shamelessly ripoff the load_contents()
method of day-9, with a notable change being that a map with an incremental index is returned instead of a simple list of integers.
def load_contents(filename: str) -> Iterator[map]:
lines = open(filename).read().strip().split(os.linesep)
for line in lines:
yield {i: int(token) for i, token in enumerate(line.split(','))}
The solve()
method is responsible for setting the initial parameters and computing the answer:
def solve(contents: map) -> int:
robot = {
'position': (0, 0),
'heading': Directions.NORTH,
'trail': [],
}
panels = dict()
regs = {'pc': 0, 'rb': 0}
try:
while True:
color = panels.get(robot['position'], Colors.BLACK)
outputs = step(ram=contents, regs=regs, inputs=[color])
new_color = Colors(outputs[0])
turn = Turns(outputs[1])
paint_panel(panels=panels, color=new_color, robot=robot, turn=turn)
except HaltOpcode:
...
answer = len(set(robot['trail']))
return answer
Contents | Command | Answer |
---|---|---|
input.txt |
./day-11.py input.txt -p 1 |
2172 |
You're not sure what it's trying to paint, but it's definitely not a registration identifier. The Space Police are getting impatient.
Checking your external ship cameras again, you notice a white panel marked "emergency hull painting robot starting panel". The rest of the panels are still black, but it looks like the robot was expecting to start on a white panel, not a black one.
Initial configuration differs.
Based on the Space Law Space Brochure that the Space Police attached to one of your windows, a valid registration identifier is always eight capital letters. After starting the robot on a single white panel instead, what registration identifier does it paint on your hull?
Will have to visualize the panels.
The different initial configuration change requires adding an extra parameter: start_panel_color
.
def solve(contents: map, start_panel_color: int = Colors.BLACK) -> int:
"""Solve puzzle part one
:param contents: puzzle input contents
:param start_panel_color: color of the start panel
:return: puzzle answer
"""
Printing the contents is managed by a new print_panels
method.
def print_panels(panels: dict) -> None:
"""Print panels
:param panels:
:return:
"""
min_x = min(x for x, _ in panels.keys())
max_x = max(x for x, _ in panels.keys()) + 1
min_y = min(y for _, y in panels.keys())
max_y = max(y for _, y in panels.keys()) + 1
panel_list = [['I' if (x, y) in panels.keys() else ' ' for x in range(min_x, max_x)] for y in range(min_y, max_y)]
for l in reversed(panel_list):
print(''.join(l))
Contents | Command | Answer |
---|---|---|
input.txt |
./day-11.py input.txt -p 2 |
JELEFGHP |
The print_panels()
could be improved.