Sha256: a5292da219d35a48358e1aa6c2956b381143307e3d365d24ce6abe8c1efa1d75

Contents?: true

Size: 1.51 KB

Versions: 90

Compression:

Stored size: 1.51 KB

Contents

require 'exercise_cases'

class AlphameticsCase < OpenStruct
  def test_name
    "test_#{description.tr(' ', '_')}"
  end

  def skipped
    index.zero? ? '# skip' : 'skip'
  end

  def input
    "'#{puzzle}'"
  end

  def expect
    expected.nil? ? {} : expected_values
  end

  def workload
    body = 
      "input = %s\n" % input,
      "expected = %s\n" % expect,
      "assert_equal expected, Alphametics.solve(input)"
    indent(body, 4)
  end

  def runtime_comment
    if slow?
      comments = 
        '',
        "# The obvious algorithm can take a long time to solve this puzzle,\n",
        "# but an optimised solution can solve it fairly quickly.\n",
        "# (It's OK to submit your solution without getting this test to pass.)\n"
      indent(comments, 2)
    end
  end

  private

  def slow?
    (expected||{}).size > 7 
  end

  def expected_values
    "{ #{indent(expected_values_as_lines, 17)} }"
  end

  def expected_values_as_lines
    lines = expected_values_as_strings.each_slice(4).map { |line| line.join(', ') }
    add_trailing_comma_and_newline(lines)
  end

  def expected_values_as_strings
    expected.sort.map { |(key, value)| "'#{key}' => #{value}" }
  end

  def add_trailing_comma_and_newline(lines)
    lines[0...-1].map { |line| "#{line},\n" }.push(lines.last)
  end

  def indent(lines, spaces)
    lines.join(' ' * spaces)
  end

end

AlphameticsCases = proc do |data|
  JSON.parse(data)['solve']['cases'].map.with_index do |row, i|
    row = row.merge('index' => i)
    AlphameticsCase.new(row)
  end
end

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
trackler-2.1.0.1 tracks/ruby/lib/alphametics_cases.rb
trackler-2.1.0.0 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.55 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.54 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.53 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.52 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.51 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.50 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.49 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.48 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.47 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.46 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.45 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.44 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.43 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.42 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.41 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.40 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.39 tracks/ruby/lib/alphametics_cases.rb
trackler-2.0.8.38 tracks/ruby/lib/alphametics_cases.rb