[2016][ruby][13.0] maze
change orxtlnnrkpmqrzlplqxvzqxrlzpuylok
commit 2a8c5eabbcb4bc1a510d0a418f56c4f0b46c41ee
author Alpha Chen <alpha.chen@gmail.com>
date
parent tzvplpsm
diff --git a/2016/ruby/day_13.rb b/2016/ruby/day_13.rb
new file mode 100644
index 0000000..7613cfe
--- /dev/null
+++ b/2016/ruby/day_13.rb
@@ -0,0 +1,37 @@
+class Maze
+  attr_reader :walls
+
+  def initialize(seed)
+    @walls = Hash.new {|h,k|
+      x,y = k
+      num = x*x + 3*x + 2*x*y + y + y*y + seed
+      h[k] = num.to_s(2).chars.count {|c| c == ?1 }.odd?
+    }
+  end
+
+  def [](x,y)
+    walls[[x, y]]
+  end
+end
+
+require 'minitest/autorun'
+
+class TestMaze < Minitest::Test
+  def test_maze
+    maze = Maze.new(10)
+    maze_s = <<-MAZE.chomp.split("\n").map(&:chars)
+.#.####.##
+..#..#...#
+#....##...
+###.#.###.
+.##..#..#.
+..##....#.
+#...##.###
+    MAZE
+    maze_s.each.with_index do |row, y|
+      row.each.with_index do |c, x|
+        assert_equal c, maze[x,y] ??#:?.
+      end
+    end
+  end
+end