aboutsummaryrefslogtreecommitdiff
path: root/challenge-084/roger-bell-west/ruby/ch-2.rb
blob: e0212cf30d000caf2d92eba1f9929a9a21f5320e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! /usr/bin/ruby

def fs(*s)
  t=0
  maxx=s.length-1
  maxy=s[0].length-1
  0.upto(maxx-1) do |x|
    0.upto(maxy-1) do |y|
      if s[x][y] == 1
        1.upto([maxx-x,maxy-y].min) do |d|
          if s[x+d][y] == 1 and
            s[x][y+d] == 1 and
            s[x+d][y+d] == 1
            t += 1
          end
        end
      end
    end
  end
  return t
end

#sub fs(@s) {
#  my $t=0;
#  my $maxx=@s.elems-1;;
#  my $maxy=@s[0].elems-1;
#  for (0..$maxx-1) -> $x {
#    for (0..$maxy-1) -> $y {
#      if (@s[$x][$y]==1) {
#        for (1..min($maxx-$x,$maxy-$y)) -> $d {
#          if (@s[$x+$d][$y]==1 &&
#                @s[$x][$y+$d]==1 &&
#                @s[$x+$d][$y+$d]==1
#                  ) {
#            $t++;
#          }
#        }
#      }
#    }
#  }
#  return $t;
#}

require 'test/unit'

class TestFs < Test::Unit::TestCase

  def test_ex1
    assert_equal(1,
                 fs(
                      [0,1,0,1],
                      [0,0,1,0],
                      [1,1,0,1],
                      [1,0,0,1]
                    ),
                 'example 1')
  end

  def test_ex2
    assert_equal(4,
                 fs(
                      [1,1,0,1],
                      [1,1,0,0],
                      [0,1,1,1],
                      [1,0,1,1]
                    ),
                 'example 2')
  end

  def test_ex3
    assert_equal(0,
                 fs(
                      [0,1,0,1],
                      [1,0,1,0],
                      [0,1,0,0],
                      [1,0,0,1]
                    ),
                 'example 3')
  end

end