[2016][ruby][7.0]
change uqsrluvvktqwuszvrwlvwrkzkstnutvr
commit 1b0909b7658f41ad899e6f635ef9719eabb8c9ab
author Alpha Chen <alpha.chen@gmail.com>
date
parent ptxxrssy
diff --git a/2016/ruby/day_07.rb b/2016/ruby/day_07.rb
new file mode 100644
index 0000000..50b5f09
--- /dev/null
+++ b/2016/ruby/day_07.rb
@@ -0,0 +1,26 @@
+def abba?(s)
+  s.chars.each_cons(4).any? {|a,b,c,d| (a != b) && (a == d) && (b == c) }
+end
+
+def tls?(s)
+  hypernets, s = s.split(/\[|\]/).inject([[],[]]) {|n,x| n[0] << x; [n[1], n[0]]}
+  s.any? {|x| abba?(x) } && hypernets.none? {|x| abba?(x) }
+end
+
+if __FILE__ == $0
+  p DATA.each_line.count {|line| tls?(line) }
+end
+
+require 'minitest'
+# require 'minitest/autorun'
+
+class TestDay7 < Minitest::Test
+  def test_tls
+    assert tls?('abba[mnop]qrst')
+    refute tls?('abcd[bddb]xyyx')
+    refute tls?('aaaa[qwer]tyui')
+    assert tls?('ioxxoj[asdfgh]zxcvbn')
+  end
+end
+
+__END__