[2019][ruby][2.x]
change xxrvwsnqznrwwrmpozpxpkpnpzvtmosm
commit c23fbe7dfe2dd95d8ff97dec92bda56493590f1e
author Alpha Chen <alpha@kejadlen.dev>
date
parent zmqxyxql
diff --git a/2019/ruby/day_02.rb b/2019/ruby/day_02.rb
new file mode 100644
index 0000000..35dc040
--- /dev/null
+++ b/2019/ruby/day_02.rb
@@ -0,0 +1,47 @@
+OPCODES = ARGF.read.split(?,).map(&:to_i)
+
+# opcodes[1] = 12
+# opcodes[2] = 2
+
+# pc = 0
+# loop do
+#   p opcodes
+#   case opcodes[pc]
+#   when 1
+#     opcodes[opcodes[pc+3]] = opcodes[opcodes[pc+1]] + opcodes[opcodes[pc+2]]
+#     pc += 4
+#   when 2
+#     opcodes[opcodes[pc+3]] = opcodes[opcodes[pc+1]] * opcodes[opcodes[pc+2]]
+#     pc += 4
+#   when 99
+#     puts opcodes[0]
+#     exit
+#   end
+# end
+
+def run(noun, verb)
+  memory = OPCODES.dup
+  memory[1] = noun
+  memory[2] = verb
+
+  pc = 0
+  loop do
+    case memory[pc]
+    when 1
+      memory[memory[pc+3]] = memory[memory[pc+1]] + memory[memory[pc+2]]
+      pc += 4
+    when 2
+      memory[memory[pc+3]] = memory[memory[pc+1]] * memory[memory[pc+2]]
+      pc += 4
+    when 99
+      return memory[0]
+    end
+  end
+end
+
+(0..99).each do |noun|
+  (0..99).each do |verb|
+    value = run(noun, verb)
+    puts 100 * noun + verb and exit if value == 19690720
+  end
+end