Commit ea92af78 authored by Andrew Fontaine's avatar Andrew Fontaine

Complete Day 11

parent e349d0a0
import AdventOfCode
alias AdventOfCode.Y2019.Utils.Intcode
aoc 2019, 11 do
def p1(),
do:
input_stream()
|> p(0)
|> Map.keys()
|> length()
def p2(), do: input_stream() |> p(1) |> draw()
defp draw(panels) do
[{x1, y1}, {x2, y2}] =
panels
|> Map.keys()
|> Enum.reduce([{0, 0}, {0, 0}], fn {x, y}, [{x1, y1}, {x2, y2}] ->
[{min(x, x1), min(y, y1)}, {max(x, x2), max(y, y2)}]
end)
Enum.map(Range.new(x1, x2), fn x ->
Enum.map(Range.new(y1, y2), fn y ->
panels
|> Map.get({x, y}, :black)
|> write()
end)
end)
|> Enum.each(&IO.puts/1)
end
defp p(stream, input) do
robot =
Task.async(fn ->
loop(%{direction: :up, panel: {0, 0}, panels: %{}, action: :paint, computer: nil})
end)
computer =
Task.async(fn ->
stream
|> Enum.at(0)
|> String.split(",")
|> Enum.map(&String.to_integer/1)
|> Intcode.new(:server, robot.pid)
|> Intcode.run_all()
end)
send(robot.pid, {:computer, computer.pid})
send(computer.pid, {:message, input})
robot
|> Task.await()
|> Map.get(:panels)
end
defp loop(state) do
receive do
:halt -> state
{:message, i} -> state |> handle_message(i) |> loop()
{:computer, i} when is_pid(i) -> loop(%{state | computer: i})
end
end
defp handle_message(%{action: :paint, panel: panel, panels: panels} = state, i) do
%{state | action: :turn, panels: Map.put(panels, panel, paint(i))}
end
defp handle_message(%{action: :turn, direction: d, panel: p, computer: c} = state, i) do
d = turn(d, i)
state = %{state | action: :paint, direction: d, panel: move(d, p)}
state.panels
|> Map.get(state.panel, :black)
|> colour()
|> (&send_colour(c, &1)).()
state
end
defp send_colour(computer, colour), do: send(computer, {:message, colour})
defp turn(:up, 0), do: :left
defp turn(:up, 1), do: :right
defp turn(:right, 0), do: :up
defp turn(:right, 1), do: :down
defp turn(:down, 0), do: :right
defp turn(:down, 1), do: :left
defp turn(:left, 0), do: :down
defp turn(:left, 1), do: :up
defp move(:up, {x, y}), do: {x, y + 1}
defp move(:right, {x, y}), do: {x + 1, y}
defp move(:left, {x, y}), do: {x - 1, y}
defp move(:down, {x, y}), do: {x, y - 1}
defp paint(0), do: :black
defp paint(1), do: :white
defp colour(:black), do: 0
defp colour(:white), do: 1
defp write(colour), do: IO.ANSI.format_fragment([colour, "█", :reset])
end
3,8,1005,8,334,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,28,2,1108,5,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,55,1,102,18,10,1,2,5,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,84,1,106,11,10,2,1008,6,10,1,4,4,10,1006,0,55,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,121,1,107,9,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,147,2,1002,4,10,2,104,18,10,1,107,16,10,1,108,8,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,185,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,208,2,1009,16,10,1006,0,7,1006,0,18,1,1105,8,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,243,2,1105,20,10,2,106,10,10,1006,0,67,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,276,2,1103,5,10,2,1104,7,10,1006,0,35,2,1105,3,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,314,101,1,9,9,1007,9,1097,10,1005,10,15,99,109,656,104,0,104,1,21102,936995824532,1,1,21101,0,351,0,1105,1,455,21102,1,387508445964,1,21102,362,1,0,1106,0,455,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,1,235244973059,1,21101,409,0,0,1106,0,455,21102,179410541659,1,1,21101,0,420,0,1105,1,455,3,10,104,0,104,0,3,10,104,0,104,0,21101,868402070292,0,1,21102,1,443,0,1106,0,455,21102,1,709584749324,1,21102,454,1,0,1106,0,455,99,109,2,22102,1,-1,1,21101,40,0,2,21102,486,1,3,21101,0,476,0,1106,0,519,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,481,482,497,4,0,1001,481,1,481,108,4,481,10,1006,10,513,1101,0,0,481,109,-2,2106,0,0,0,109,4,2102,1,-1,518,1207,-3,0,10,1006,10,536,21102,0,1,-3,21202,-3,1,1,22102,1,-2,2,21102,1,1,3,21102,555,1,0,1106,0,560,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,583,2207,-4,-2,10,1006,10,583,21201,-4,0,-4,1106,0,651,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,602,1,0,1106,0,560,22102,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,621,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,643,21201,-1,0,1,21102,643,1,0,106,0,518,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment