Class: Arb::Api::OtherSolutions

Inherits:
Object
  • Object
show all
Defined in:
lib/arb/api/other_solutions.rb

Constant Summary collapse

UI_URI =
"https://github.com"
PATHS =
{
  "eregon" => ->(year:, day:, part:) {
    if part == "1"
      [
        "adventofcode/tree/master/#{year}/#{day.delete_prefix("0")}.rb",
        "adventofcode/tree/master/#{year}/#{day.delete_prefix("0")}a.rb",
      ]
    elsif part == "2"
      ["adventofcode/tree/master/#{year}/#{day.delete_prefix("0")}b.rb"]
    end
  },
  "gchan" => ->(year:, day:, part:) {
    ["advent-of-code-ruby/tree/main/#{year}/day-#{day}/day-#{day}-part-#{part}.rb"]
  },
  "ahorner" => ->(year:, day:, part:) {
    return [] if part == "1"
    ["advent-of-code/tree/main/lib/#{year}/#{day}.rb"]
  },
  "ZogStriP" => ->(year:, day:, part:) {
    return [] if part == "1"

    puzzle_name = File.read(Files::Instructions.path(year:, day:))
      .match(/## --- Day \d\d?: (.+)(?= ---)/)
      .captures
      .first
      .downcase
      .gsub(" ", "_")

    ["adventofcode/tree/master/#{year}/#{day}_#{puzzle_name}.rb"]
  },
  "erikw" => ->(year:, day:, part:) {
    ["advent-of-code-solutions/tree/main/#{year}/#{day}/part#{part}.rb"]
  },
}
EDITS =
{
  "gchan" => ->(file_str) {
    # Remove the first 5 lines (boilerplate).
    file_str.lines[5..].join
  },
  "ZogStriP" => ->(file_str) {
    # Remove input at the end of the file.
    file_str.split("\n__END__").first
  },
  "erikw" => ->(file_str) {
    # Remove the first 3 lines (boilerplate).
    file_str.lines[3..].join
  },
}

Instance Method Summary collapse

Constructor Details

#initializeOtherSolutions

Returns a new instance of OtherSolutions.



58
59
60
61
62
63
64
65
# File 'lib/arb/api/other_solutions.rb', line 58

def initialize
  @connection = Faraday.new(
    url: "https://raw.githubusercontent.com",
    headers: {
      "User-Agent" => "github.com/fpsvogel/advent_of_ruby by [email protected]",
    }
  )
end

Instance Method Details

#other_solutions(year:, day:, part:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/arb/api/other_solutions.rb', line 67

def other_solutions(year:, day:, part:)
  "# #{year} Day #{day} Part #{part}\n\n" +
    PATHS
    .map { |username, path_builder|
      actual_path = nil
      solution = nil
      paths = path_builder.call(year:, day:, part:)

      paths.each do |path|
        next if solution
        response = connection.get("/#{username}/#{path.sub("/tree/", "/")}")
        next if response.status == 404

        actual_path = path
        solution = (EDITS[username] || :itself.to_proc).call(response.body)
      end

      if solution
        <<~SOLUTION
          # ------------------------------------------------------------------------------
          # #{username}: #{UI_URI}/#{username}/#{actual_path}
          # ------------------------------------------------------------------------------

          #{solution}

        SOLUTION
      end
    }
    .compact
    .join
    .strip + "\n"
end