Day 11: Space Police
text = File.read!("input/2019/11.txt")
Space Police
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.
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. Then, the program will output two values:
- First, it will output a value indicating the color to paint the
panel the robot is over:
0means to paint the panel black, and1means to paint the panel white. - Second, it will output a value indicating the direction the robot
should turn:
0means it should turn left 90 degrees, and1means it should turn right 90 degrees.
After the robot turns, it should always move forward exactly one panel. The robot starts facing up.
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:
.....
.....
..^..
.....
.....
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:
.....
..<#.
...#.
.##..
.....
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.)
Build a new emergency hull painting robot and run the Intcode program on it. How many panels does it paint at least once?
To begin, get your puzzle input.
defmodule Intcode do
use GenServer
import Enum, only: [at: 2]
defstruct(:position, :data, :relbase)
def handle_cast(:run, state) do
{:noreply, state, {:continue, :step}}
end
def handle_continue(:step, s) do
s.position
|> get()
|> Kernel.+(100_000)
|> Integer.digits()
|> case do
[_, _, _, _, 9, 9] -> nil
[_, m3, m2, m1, _, 1] -> {:add, [m1, m2, m3]}
[_, m3, m2, m1, _, 2] -> {:mul, [m1, m2, m3]}
[_, _, _, m1, _, 3] -> {:in, [m1]}
[_, _, _, m1, _, 4] -> {:out, [m1]}
[_, _, m2, m1, _, 5] -> {:jif, [m1, m2]}
[_, _, m2, m1, _, 6] -> {:jun, [m1, m2]}
[_, m3, m2, m1, _, 7] -> {:lt, [m1, m2, m3]}
[_, m3, m2, m1, _, 8] -> {:eq, [m1, m2, m3]}
[_, _, _, m1, _, 9] -> {:reg, [m1]}
end
|> case do
nil ->
{:noreply, s}
{cmd, modes} ->
args =
modes
|> Enum.with_index(s.position + 1)
|> Enum.map(fn
{0, n} -> get(n)
{1, n} -> n
{2, n} -> get(n) + get(:base)
end)
execute(cmd, args)
s = update_in(s.position, &(&1 + length(modes) + 1))
{:noreply, s, {:continue, run}}
end
end
def execute(:add, [a, b, c]), do: put(c, get(a) + get(b))
def execute(:mul, [a, b, c]), do: put(c, get(a) * get(b))
def execute(:in, [a]) do
receive do
n -> put(a, n)
end
end
def execute(:out, args) do
{:noreply, state}
end
def execute(:jif, [a, b]), do: if(get(a) != 0, do: {:jump, get(b)})
def execute(:jun, [a, b]), do: if(get(a) == 0, do: {:jump, get(b)})
def execute(:lt, [a, b, c]), do: put(c, if(get(a) < get(b), do: 1, else: 0))
def execute(:eq, [a, b, c]), do: put(c, if(get(a) == get(b), do: 1, else: 0))
def execute(:reg, [a]), do: put(:base, get(:base) + get(a))
defp get(key), do: Process.get(key, 0)
defp put(key, value), do: Process.put(key, value)
end