[2022][ruby][10.x]
change qlrnpvlproyktslwopmqqztllutznulv
commit 3eee1af357a15f72fea94a00020e5f3da00382c1
author Alpha Chen <alpha@kejadlen.dev>
date
parent upkkxxzr
diff --git a/2022/ruby/day_10.rb b/2022/ruby/day_10.rb
new file mode 100644
index 0000000..1fafd2f
--- /dev/null
+++ b/2022/ruby/day_10.rb
@@ -0,0 +1,25 @@
+instructions = ARGF.read.lines(chomp: true)
+
+adds = {}
+cycle = 1
+instructions.each do |instruction|
+  cycle += case instruction
+           when /noop/
+             1
+           when /addx (-?\d+)/
+             adds[cycle+1] = $1.to_i
+             2
+           else fail instruction
+           end
+end
+
+xs = (0..adds.keys.max).inject([1]) { _1 << _1.last + adds.fetch(_2, 0) }
+p 20.step(by: 40, to: 220).map { [_1, xs[_1]] }.sum { _1 * _2 }
+
+puts 6.times.map {|y|
+  40.times.map {|x|
+    cycle = 40*y + x + 1
+    xx = xs.fetch(cycle, xs.last)
+    (-1..1).cover?(x - xx) ? ?# : ?.
+  }.join
+}.join("\n")