Class: Kira::Group
- Inherits:
-
Object
- Object
- Kira::Group
- 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
-
#indexes ⇒ Object
readonly
Returns the value of attribute indexes.
-
#sum ⇒ Object
readonly
Returns the value of attribute sum.
Instance Method Summary collapse
-
#initialize(equation) ⇒ Group
constructor
A new instance of Group.
- #to_s ⇒ Object
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
#indexes ⇒ Object (readonly)
Returns the value of attribute indexes.
30 31 32 |
# File 'lib/kira/group.rb', line 30 def indexes @indexes end |
#sum ⇒ Object (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_s ⇒ Object
32 33 34 |
# File 'lib/kira/group.rb', line 32 def to_s (@indexes.map { |i| "(#{i / 9}, #{i % 9})" }).join(" + ") + " = #{@sum}" end |