From b933c30beaca2fc3517fd8c86316b0ddb0563956 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 6 Dec 2019 10:49:25 -0500 Subject: [PATCH] Intcode assembler: make immediate mode the default --- lib/intcode/assembler.ex | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/intcode/assembler.ex b/lib/intcode/assembler.ex index 6468e78..0c1ba2d 100644 --- a/lib/intcode/assembler.ex +++ b/lib/intcode/assembler.ex @@ -1,22 +1,23 @@ defmodule Assembler do @asm """ - in #res - clt res #8 #31 - jnz 31 #lessThan - ceq res #8 #31 - jnz 31 #equal - out #1001 + in res + clt $res 8 cmpRes + jnz $cmpRes lessThan + ceq $res 8 cmpRes + jnz $cmpRes equal + out 1001 hlt lessThan: - out #999 + out 999 hlt equal: - out #1000 + out 1000 hlt res: 0 + cmpRes: 0 """ def test do @asm @@ -83,20 +84,26 @@ defmodule Assembler do |> Enum.with_index() |> Enum.map_reduce(0, fn {{param, type}, index}, modes -> val = - case Regex.run(~r/^#?(\d+)$/, param) do + case Regex.run(~r/^\$?(\d+)$/, param) do [_, digits] -> String.to_integer(digits) _ -> - [_, name] = Regex.run(~r/^#?(\w+)$/, param) + [_, name] = Regex.run(~r/^\$?(\w+)$/, param) {:label, name} end modes = - if type == :read && String.starts_with?(param, "#") do - modes + pow(10, index + 2) - else - modes + case type do + :read -> + if String.starts_with?(param, "$") do + modes + else + modes + pow(10, index + 2) + end + + _ -> + modes end {val, modes}