Class: Arb::Cli::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/arb/cli/shared/runner.rb

Constant Summary collapse

SolutionNotFoundError =
Class.new(StandardError)
SolutionArgumentError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.run_part(year:, day:, part:, correct_answer:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/arb/cli/shared/runner.rb', line 7

def self.run_part(year:, day:, part:, correct_answer:)
  solution_class = solution_class(year:, day:)
  base_method_name = "part_#{part}"
  variant_method_names = solution_class
      .instance_methods(false)
      .filter { _1.to_s.start_with?(base_method_name)}
      .sort

  if variant_method_names.empty?
    puts PASTEL.red("🤔 Couldn't find the method #{PASTEL.bold("Year#{year}::Day#{day}##{base_method_name}")}.")
    return
  end

  answers = []

  variant_method_names.each do |variant_method_name|
    solution = solution_class.new
    input_file = File.new(Files::Input.path(year:, day:))
    answer = nil

    begin
      run_time = Benchmark.realtime do
        answer = solution.send(variant_method_name, input_file)
      end
      variant = variant_method_name.to_s.delete_prefix(base_method_name).delete_prefix("_")
      variant_note = " `#{PASTEL.blue.bold(variant)}`" unless variant.empty?
    rescue ArgumentError
      raise SolutionArgumentError
    end

    part_name = "#{year}##{day}.#{part}#{variant_note}"

    if answer
      puts "Result for #{part_name}:"

      if correct_answer
        if answer.to_s == correct_answer
          puts PASTEL.green.bold("#{answer}")
        else
          puts PASTEL.red.bold("#{answer} -- should be #{correct_answer}")
        end
      else
        puts PASTEL.bright_white.bold(answer)
      end
    else
      puts "❌ No result for #{part_name}"
    end

    if answer && run_time
      if variant_method_names.count > 1
        puts "(obtained in #{PASTEL.cyan("%.6f" % run_time)} seconds)"
      else
        puts "(obtained in #{"%.6f" % run_time} seconds)"
      end
    end

    answers << answer

    puts
  end

  answers
end