part1 (3651B)
1--- Day 11: Space Police --- 2 3On the way to Jupiter, you're pulled over by the [1m[97mSpace Police[0m. 4 5"Attention, unmarked spacecraft! You are in violation of Space Law! All spacecraft must have a 6clearly visible [1m[97mregistration identifier[0m! You have 24 hours to comply or be sent to Space Jail!" 7 8Not wanting to be sent to Space Jail, you radio back to the Elves on Earth for help. Although it 9takes almost three hours for their reply signal to reach you, they send instructions for how to 10power up the [1m[97memergency hull painting robot[0m and even provide a small Intcode program (your puzzle 11input) that will cause it to paint your ship appropriately. 12 13There's just one problem: you don't have an emergency hull painting robot. 14 15You'll need to build a new emergency hull painting robot. The robot needs to be able to move around 16on the grid of square panels on the side of your ship, detect the color of its current panel, and 17paint its current panel [1m[97mblack[0m or [1m[97mwhite[0m. (All of the panels are currently [1m[97mblack[0m.) 18 19The Intcode program will serve as the brain of the robot. The program uses input instructions to 20access the robot's camera: provide 0 if the robot is over a [1m[97mblack[0m panel or 1 if the robot is over a 21[1m[97mwhite[0m panel. Then, the program will output two values: 22 23 24 - First, it will output a value indicating the [1m[97mcolor to paint the panel[0m the robot is over: 0 means 25to paint the panel [1m[97mblack[0m, and 1 means to paint the panel [1m[97mwhite[0m. 26 27 - Second, it will output a value indicating the [1m[97mdirection the robot should turn[0m: 0 means it should 28turn [1m[97mleft 90 degrees[0m, and 1 means it should turn [1m[97mright 90 degrees[0m. 29 30 31After the robot turns, it should always move [1m[97mforward exactly one panel[0m. The robot starts facing 32[1m[97mup[0m. 33 34The robot will continue running for a while like this and halt when it is finished drawing. Do not 35restart the Intcode computer inside the robot during this process. 36 37For example, suppose the robot is about to start running. Drawing black panels as ., white panels 38as #, and the robot pointing the direction it is facing (< ^ > v), the initial state and region near 39the robot looks like this: 40 41..... 42..... 43..^.. 44..... 45..... 46 47The panel under the robot (not visible here because a ^ is shown instead) is also black, and so any 48input instructions at this point should be provided 0. Suppose the robot eventually outputs 1 (paint 49white) and then 0 (turn left). After taking these actions and moving forward one panel, the region 50now looks like this: 51 52..... 53..... 54.<#.. 55..... 56..... 57 58Input instructions should still be provided 0. Next, the robot might output 0 (paint black) and then 590 (turn left): 60 61..... 62..... 63..#.. 64.v... 65..... 66 67After more outputs (1,0, 1,0): 68 69..... 70..... 71..^.. 72.##.. 73..... 74 75The robot is now back where it started, but because it is now on a white panel, input instructions 76should be provided 1. After several more outputs (0,1, 1,0, 1,0), the area looks like this: 77 78..... 79..<#. 80...#. 81.##.. 82..... 83 84Before you deploy the robot, you should probably have an estimate of the area it will cover: 85specifically, you need to know the [1m[97mnumber of panels it paints at least once[0m, regardless of color. In 86the example above, the robot painted [1m[97m6 panels[0m at least once. (It painted its starting panel twice, 87but that panel is still only counted once; it also never painted the panel it ended on.) 88 89Build a new emergency hull painting robot and run the Intcode program on it. [1m[97mHow many panels does it 90paint at least once?[0m 91 92