Module: AtCoderFriends::Parser::Constraints

Defined in:
lib/at_coder_friends/parser/constraints.rb

Overview

parses constraints

Constant Summary collapse

SECTIONS =
[
  Problem::SECTION_IN_FMT,
  Problem::SECTION_IO_FMT,
  Problem::SECTION_CONSTRAINTS
].freeze

Class Method Summary collapse

Class Method Details

.parse(str) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/at_coder_friends/parser/constraints.rb', line 23

def parse(str)
  str
    .gsub(/[,\\(){}|]/, '')
    .gsub(/(≤|leq?)/i, '≦')
    .scan(/([\da-z_]+)\s*≦\s*(\d+)(?:\^(\d+))?/i)
    .map do |v, sz, k|
      sz = sz.to_i
      sz **= k.to_i if k
      Problem::Constraint.new(v, :max, sz)
    end
end

.process(pbm) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/at_coder_friends/parser/constraints.rb', line 15

def process(pbm)
  str = SECTIONS.reduce('') do |m, key|
    m + (pbm.sections[key]&.content || '')
  end
  constraints = parse(str)
  pbm.constraints = constraints
end