Class: Kira::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/kira/group.rb

Overview

Represents a group of cells, the sum of which must be equal to a certain fixed number. It is initialized with an equation having the following form (whitespaces are ignored):

(r1, c1) + (r2, c2) + ... = s

where:

rn - row number
cn - column number
s - sum of the values at the given indexes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(equation) ⇒ Group

Returns a new instance of Group.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kira/group.rb', line 14

def initialize(equation)
  equation.delete!(" \t\n\r")

  unless equation.match /(\(\d,\d\)+)*\(\d,\d\)=\d+/
    raise ArgumentError.new("Equation has invalid format")
  end

  *str_idxs, @sum = equation.split(/[=+]/)
  @sum = @sum.to_i
  @indexes = []
  str_idxs.each do |idx|
    row, col = idx.scan(/\d+/).map(&:to_i)
    indexes.push(row*9 + col)
  end
end

Instance Attribute Details

#indexesObject (readonly)

Returns the value of attribute indexes.



30
31
32
# File 'lib/kira/group.rb', line 30

def indexes
  @indexes
end

#sumObject (readonly)

Returns the value of attribute sum.



30
31
32
# File 'lib/kira/group.rb', line 30

def sum
  @sum
end

Instance Method Details

#to_sObject



32
33
34
# File 'lib/kira/group.rb', line 32

def to_s
  (@indexes.map { |i| "(#{i / 9}, #{i % 9})" }).join(" + ") + " = #{@sum}"
end