-
-
Save QuinnWilton/e6fe3506240bf3abc4ef66c2f820a7e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Aoc.Year2019.Day07.AmplificationCircuit do | |
import Aoc | |
defmodule CPU do | |
alias __MODULE__ | |
defstruct pc: 0, memory: %{}, output: nil, last: nil | |
def execute(%CPU{pc: pc} = cpu) do | |
cpu | |
|> read(pc) | |
|> decode() | |
|> do_execute(cpu) | |
|> case do | |
{:done, %CPU{} = cpu} -> {:ok, cpu} | |
{:step, %CPU{} = cpu, offset} -> cpu |> step(offset) |> execute() | |
{:jump, %CPU{} = cpu, address} -> cpu |> jump(address) |> execute() | |
end | |
end | |
def step(%CPU{pc: pc} = cpu, by) do | |
%CPU{cpu | pc: pc + by} | |
end | |
def jump(%CPU{} = cpu, pc) do | |
%CPU{cpu | pc: pc} | |
end | |
def read(%CPU{memory: memory} = cpu, address, mode \\ :immediate) do | |
value = Map.get(memory, address) | |
case mode do | |
:immediate -> value | |
:position -> read(cpu, value, :immediate) | |
end | |
end | |
def write(%CPU{memory: memory} = cpu, address, value) do | |
%CPU{cpu | memory: Map.put(memory, address, value)} | |
end | |
defp decode(instruction) do | |
opcode = rem(instruction, 100) | |
modes = | |
div(instruction, 100) | |
|> Integer.digits() | |
|> Enum.reverse() | |
|> (fn modes -> modes ++ [0, 0] end).() | |
|> Enum.map(&decode_mode/1) | |
{decoded, arity} = | |
case opcode do | |
99 -> {:exit, 0} | |
01 -> {:add, 2} | |
02 -> {:multiply, 2} | |
03 -> {:save, 0} | |
04 -> {:output, 1} | |
05 -> {:jnz, 2} | |
06 -> {:jz, 2} | |
07 -> {:lt, 2} | |
08 -> {:eq, 2} | |
end | |
{decoded, Enum.take(modes, arity)} | |
end | |
defp decode_mode(0), do: :position | |
defp decode_mode(1), do: :immediate | |
defp do_execute({:exit, []}, %CPU{} = cpu) do | |
{:done, cpu} | |
end | |
defp do_execute({:save, []}, %CPU{pc: pc} = cpu) do | |
value = | |
receive do | |
x -> x | |
end | |
dest = read(cpu, pc + 1) | |
cpu = write(cpu, dest, value) | |
{:step, cpu, 2} | |
end | |
defp do_execute({:add, [m1, m2]}, %CPU{pc: pc} = cpu) do | |
arg1 = read(cpu, pc + 1, m1) | |
arg2 = read(cpu, pc + 2, m2) | |
dest = read(cpu, pc + 3) | |
{:step, write(cpu, dest, arg1 + arg2), 4} | |
end | |
defp do_execute({:multiply, [m1, m2]}, %CPU{pc: pc} = cpu) do | |
arg1 = read(cpu, pc + 1, m1) | |
arg2 = read(cpu, pc + 2, m2) | |
dest = read(cpu, pc + 3) | |
{:step, write(cpu, dest, arg1 * arg2), 4} | |
end | |
defp do_execute({:output, [m1]}, %CPU{pc: pc, output: output} = cpu) do | |
value = read(cpu, pc + 1, m1) | |
try do | |
send(output, value) | |
rescue | |
e -> :ok | |
end | |
{:step, %{cpu | last: value}, 2} | |
end | |
defp do_execute({:jnz, [m1, m2]}, %CPU{pc: pc} = cpu) do | |
check = read(cpu, pc + 1, m1) | |
if check != 0 do | |
dest = read(cpu, pc + 2, m2) | |
{:jump, cpu, dest} | |
else | |
{:step, cpu, 3} | |
end | |
end | |
defp do_execute({:jz, [m1, m2]}, %CPU{pc: pc} = cpu) do | |
check = read(cpu, pc + 1, m1) | |
if check == 0 do | |
dest = read(cpu, pc + 2, m2) | |
{:jump, cpu, dest} | |
else | |
{:step, cpu, 3} | |
end | |
end | |
defp do_execute({:lt, [m1, m2]}, %CPU{pc: pc} = cpu) do | |
arg1 = read(cpu, pc + 1, m1) | |
arg2 = read(cpu, pc + 2, m2) | |
to = read(cpu, pc + 3) | |
if arg1 < arg2 do | |
{:step, write(cpu, to, 1), 4} | |
else | |
{:step, write(cpu, to, 0), 4} | |
end | |
end | |
defp do_execute({:eq, [m1, m2]}, %CPU{pc: pc} = cpu) do | |
arg1 = read(cpu, pc + 1, m1) | |
arg2 = read(cpu, pc + 2, m2) | |
to = read(cpu, pc + 3) | |
if arg1 == arg2 do | |
{:step, write(cpu, to, 1), 4} | |
else | |
{:step, write(cpu, to, 0), 4} | |
end | |
end | |
end | |
def input do | |
File.read!("priv/inputs/year_2019/day_07.txt") | |
end | |
def solve(input \\ input()) do | |
memory = | |
input | |
|> String.split(",") | |
|> as_i | |
|> Enum.with_index() | |
|> Enum.reduce(%{}, fn {value, address}, acc -> | |
Map.put(acc, address, value) | |
end) | |
for [a, b, c, d, e] = perm <- permutations([5, 6, 7, 8, 9]) do | |
me = self() | |
spawn(fn -> | |
Process.register(self(), :a) | |
CPU.execute(%CPU{output: :b, memory: memory}) | |
end) | |
spawn(fn -> | |
Process.register(self(), :b) | |
CPU.execute(%CPU{output: :c, memory: memory}) | |
end) | |
spawn(fn -> | |
Process.register(self(), :c) | |
CPU.execute(%CPU{output: :d, memory: memory}) | |
end) | |
spawn(fn -> | |
Process.register(self(), :d) | |
CPU.execute(%CPU{output: :e, memory: memory}) | |
end) | |
spawn(fn -> | |
Process.register(self(), :e) | |
{:ok, %CPU{last: last}} = CPU.execute(%CPU{output: :a, memory: memory}) | |
send(me, last) | |
end) | |
:timer.sleep(10) | |
send(:a, a) | |
send(:b, b) | |
send(:c, c) | |
send(:d, d) | |
send(:e, e) | |
send(:a, 0) | |
receive do | |
out -> | |
{perm, out} | |
end | |
end | |
|> Enum.max_by(fn {_, result} -> result end) | |
end | |
def permutations([]), do: [[]] | |
def permutations(list), | |
do: for(elem <- list, rest <- permutations(list -- [elem]), do: [elem | rest]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment