Module: AtCoderFriends::Parser::Binary

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

Overview

detect binary problem

Class Method Summary collapse

Class Method Details

.binary_values?(vs) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/at_coder_friends/parser/binary.rb', line 33

def binary_values?(vs)
  return false unless vs.size == 2
  return false if vs.any? { |v| v.include?("\n") }
  return false if vs.any? { |v| v =~ /\A[0-9\s]*\z/ }

  true
end

.exp_values(pbm) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/at_coder_friends/parser/binary.rb', line 25

def exp_values(pbm)
  pbm
    .samples
    .select { |smp| smp.ext == :exp }
    .map { |smp| smp.txt.chomp }
    .uniq
end

.output_format(pbm) ⇒ Object



41
42
43
# File 'lib/at_coder_friends/parser/binary.rb', line 41

def output_format(pbm)
  pbm.sections[Problem::SECTION_OUT_FMT]&.content || ''
end

.process(pbm) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/at_coder_friends/parser/binary.rb', line 9

def process(pbm)
  vs = exp_values(pbm)
  return unless binary_values?(vs)

  out_fmt = output_format(pbm)
  re1, re2 = vs.map { |v| Regexp.escape(v) }

  pbm.options.binary_values =
    case out_fmt
    when /#{re1}.+#{re2}/m
      vs
    when /#{re2}.+#{re1}/m
      vs.reverse
    end
end