[2016][ruby][9.0]
change ovzkprqupwnpkmnyomnysxossqlossxn
commit 5f2f1280b7dd4c00b6a30fb61d97d88341608039
author Alpha Chen <alpha.chen@gmail.com>
date
parent uxkwqwwo
diff --git a/2016/ruby/day_09.rb b/2016/ruby/day_09.rb
new file mode 100644
index 0000000..3819515
--- /dev/null
+++ b/2016/ruby/day_09.rb
@@ -0,0 +1,37 @@
+def decompress(input)
+  output = ''
+  ss = StringScanner.new(input)
+  until ss.eos?
+    if ss.scan(/(.*?)\((\d+)x(\d+)\)/)
+      output << ss[1]
+
+      times = ss[3].to_i
+      seq = ss.scan(/.{#{ss[2].to_i}}/)
+
+      output << (seq * times)
+    else
+      output << ss.scan(/.*/)
+    end
+  end
+  output
+end
+
+if __FILE__ == $0
+  puts decompress(DATA.read.chomp).length
+end
+
+require 'minitest'
+# require 'minitest/autorun'
+
+class TestDecompress < Minitest::Test
+  def test_decompress
+    assert_equal 'ADVENT', decompress('ADVENT')
+    assert_equal 'ABBBBBC', decompress('A(1x5)BC')
+    assert_equal 'XYZXYZXYZ', decompress('(3x3)XYZ')
+    assert_equal 'ABCBCDEFEFG', decompress('A(2x2)BCD(2x2)EFG')
+    assert_equal '(1x3)A', decompress('(6x1)(1x3)A')
+    assert_equal 'X(3x3)ABC(3x3)ABCY', decompress('X(8x2)(3x3)ABCY')
+  end
+end
+
+__END__