-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce commands to create and run examples (#5)
- Loading branch information
Showing
7 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "example/new" | ||
require_relative "example/solve" | ||
|
||
module AdventOfCode | ||
module Commands | ||
module Example | ||
class CLI < Thor | ||
desc "new DAY NAME", "creates an example file with name NAME for day DAY" | ||
def new(day, name) | ||
New.new(day: day.to_i, name: name).execute | ||
rescue ExampleAlreadyExistsError => e | ||
say(e.message, :red) | ||
rescue InvalidDayError | ||
rescue_invalid_day_error | ||
end | ||
|
||
desc "solve DAY NAME", "runs the example with name NAME for day DAY" | ||
def solve(day, name) | ||
Solve.new(day: day.to_i, name: name).execute | ||
rescue InvalidDayError | ||
rescue_invalid_day_error | ||
rescue MissingExampleError | ||
say "Error: Cannot find example file.", :red | ||
rescue AdventOfCode::MissingSolutionError | ||
say "Error: Cannot find solution file.", :red | ||
end | ||
|
||
private | ||
|
||
def rescue_invalid_day_error | ||
say "Error: The day argument must be an integer between 1 and 25.", :red | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module AdventOfCode | ||
module Commands | ||
module Example | ||
class New < Command | ||
def initialize(day:, name:) | ||
@name = name | ||
super(day: day) | ||
end | ||
|
||
def execute | ||
unless Dir.exist?("examples") | ||
say("Creating examples directory...") | ||
Dir.mkdir("examples") | ||
end | ||
|
||
unless Dir.exist?("examples/#{day_string}") | ||
say("Creating examples/#{day_string} directory...") | ||
Dir.mkdir("examples/#{day_string}") | ||
end | ||
|
||
if File.exist?(example_file_name) | ||
raise ExampleAlreadyExistsError, | ||
"could not create example file because file #{example_file_name} already exists" | ||
end | ||
|
||
say("Creating #{example_file_name}...") | ||
create_file(example_file_name) | ||
|
||
say("Creating #{example_expected_file_name}...") | ||
create_file(example_expected_file_name, expected_file_contents) | ||
|
||
say "Done!", :green | ||
end | ||
|
||
private | ||
|
||
def expected_file_contents | ||
<<~RUBY | ||
part_one: ~ | ||
part_two: ~ | ||
RUBY | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require "yaml" | ||
|
||
module AdventOfCode | ||
module Commands | ||
module Example | ||
class Solve < Command | ||
def initialize(day:, name:) | ||
@name = name | ||
super(day: day) | ||
end | ||
|
||
def execute | ||
raise MissingExampleError unless File.exist?(example_file_name) | ||
raise MissingExampleError unless File.exist?(example_expected_file_name) | ||
raise MissingSolutionError unless File.exist?(solution_file_name) | ||
|
||
say "Reading input..." | ||
input = File.readlines(example_file_name, chomp: true) | ||
|
||
say "Loading solution..." | ||
load(solution_file_name) | ||
|
||
module_name = "Day#{day_string}" | ||
|
||
say "\nRunning part one with example #{@name}..." | ||
solution(module_name, "one", input) | ||
|
||
say "\nRunning part two with example #{@name}..." | ||
solution(module_name, "two", input) | ||
|
||
say "\nDone!", :green | ||
end | ||
|
||
private | ||
|
||
def solution(module_name, part, input) | ||
start_time = Time.now | ||
result = Object.const_get(module_name).send("part_#{part}", input) | ||
end_time = Time.now | ||
|
||
expected_result = expected_answers["part_#{part}"] | ||
|
||
if expected_result.nil? | ||
say "Part #{part} result: #{result} ⚠️ (no expectation provided)" | ||
elsif result == expected_result | ||
say "Part #{part} result: #{result} ✅" | ||
else | ||
say "Part #{part} result: #{result} ❌ (expected #{expected_result})" | ||
end | ||
|
||
say "Took #{end_time - start_time} seconds to solve" | ||
end | ||
|
||
def expected_answers | ||
@expected_answers ||= YAML.safe_load(File.read(example_expected_file_name)) | ||
end | ||
end | ||
end | ||
end | ||
end |