[2024][ruby][17.1]
change prrnzuzwlyoszluquyqnruszmmmqpnpr
commit f8ff5f2ad706497081d1f0f854a9f21d751dc314
author Alpha Chen <alpha@kejadlen.dev>
date
parent sswlqpmo
diff --git a/2024/ruby/day_16.rb b/2024/ruby/day_16.rb
index 30b96f2..cb3d6e8 100644
--- a/2024/ruby/day_16.rb
+++ b/2024/ruby/day_16.rb
@@ -43,20 +43,3 @@ end
 pp scores.find {|(yx,_),_| yx == stop }[1]
 
 __END__
-#################
-#...#...#...#..E#
-#.#.#.#.#.#.#.#^#
-#.#.#.#...#...#^#
-#.#.#.#.###.#.#^#
-#>>v#.#.#.....#^#
-#^#v#.#.#.#####^#
-#^#v..#.#.#>>>>^#
-#^#v#####.#^###.#
-#^#v#..>>>>^#...#
-#^#v###^#####.###
-#^#v#>>^#.....#.#
-#^#v#^#####.###.#
-#^#v#^........#.#
-#^#v#^#########.#
-#S#>>^..........#
-#################
diff --git a/2024/ruby/day_17.rb b/2024/ruby/day_17.rb
new file mode 100644
index 0000000..e190a47
--- /dev/null
+++ b/2024/ruby/day_17.rb
@@ -0,0 +1,57 @@
+registers, program = DATA.read.split("\n\n")
+
+a, b, c = registers.scan(/\d+/).map(&:to_i)
+program = program.scan(/\d+/).map(&:to_i)
+
+combo = ->(operand) {
+  case operand
+  when 0, 1, 2, 3 then operand
+  when 4 then a
+  when 5 then b
+  when 6 then c
+  else
+    fail
+  end
+}
+
+ip = 0
+out = []
+while (0...program.size).cover?(ip)
+  opcode = program.fetch(ip)
+  operand = program.fetch(ip+1)
+  case opcode
+  when 0
+    a = (a / 2**combo[operand]).to_i
+    ip += 2
+  when 1
+    b  = b ^ operand
+    ip += 2
+  when 2
+    b = combo[operand] % 8
+    ip += 2
+  when 3
+    if a.zero?
+      ip += 2
+    else
+      ip = operand
+    end
+  when 4
+    b  = b ^ c
+    ip += 2
+  when 5
+    out << combo[operand] % 8
+    ip += 2
+  when 6
+    b = (a / 2**combo[operand]).to_i
+    ip += 2
+  when 7
+    c = (a / 2**combo[operand]).to_i
+    ip += 2
+  else
+    fail
+  end
+end
+
+puts out.join(?,)
+
+__END__