From 29cf85dbfd78a5071490249757019b5f1337641d Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 13 Dec 2016 08:46:06 -0800 Subject: [PATCH] [2016][ruby][13.0] maze --- 2016/ruby/day_13.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2016/ruby/day_13.rb 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