AoC19/lib/intcode/debugger.ex

68 lines
1.3 KiB
Elixir

defmodule Debugger do
def run() do
program = [1, 1, 2, 3, 1102, 4, 5, 0, 99]
memory_view_length = floor(columns() / 7) * floor(rows() / 2 - 10)
memory = Day9.expand_memory(program, memory_view_length)
parent = self()
pid =
spawn(fn ->
Day9.run(memory, parent, 0, true)
end)
step(pid)
end
def columns do
{:ok, cols} = :io.columns()
cols
end
def rows do
{:ok, rows} = :io.rows()
rows
end
def step(pid) do
IO.write([IO.ANSI.clear(), IO.ANSI.home()])
receive do
{:debug, msg, params, {memory, _}} ->
print_memory(memory)
IO.puts("#{msg}: #{inspect(params)}")
IO.gets("")
send(pid, :step)
step(pid)
{msg, _, {memory, _}} when msg in [:halt, :abort, :ok] ->
print_memory(memory)
IO.gets("")
end
end
def print_memory(memory, row_size \\ nil)
def print_memory([], _) do
end
def print_memory(memory, row_size) when is_nil(row_size) do
print_memory(memory, floor(columns() / 7))
end
def print_memory(memory, row_size) do
memory
|> Enum.take(row_size)
|> Enum.each(fn val ->
val
|> to_string()
|> String.pad_leading(7)
|> IO.write()
end)
IO.write("\n\n")
print_memory(Enum.drop(memory, row_size), row_size)
end
end