adding gem file executable
This commit is contained in:
27
lib/tasks/new_day.rb
Normal file
27
lib/tasks/new_day.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
require "tty-prompt"
|
||||
require "tty-file"
|
||||
require "ostruct"
|
||||
|
||||
require_relative("templates")
|
||||
|
||||
def new_day(prompt, day)
|
||||
input = prompt.multiline("Example input").join("")
|
||||
print input
|
||||
result = prompt.ask("Example result")
|
||||
|
||||
day_name = "day_#{day}"
|
||||
|
||||
TTY::File.create_dir(day_name)
|
||||
TTY::File.create_file("#{day_name}/data")
|
||||
TTY::File.create_file("#{day_name}/impl.rb", IMPL_RB)
|
||||
TTY::File.create_file("#{day_name}/run.rb", RUN_RB)
|
||||
TTY::File.create_file("#{day_name}/test.rb", TEST_RB)
|
||||
|
||||
TTY::File.replace_in_file "#{day_name}/run.rb", /%day_name%/, day_name
|
||||
TTY::File.replace_in_file "#{day_name}/test.rb", /%day_name%/, day_name
|
||||
TTY::File.replace_in_file "#{day_name}/test.rb", /%example_result%/, result
|
||||
TTY::File.replace_in_file "#{day_name}/test.rb", /%example_input%/, input
|
||||
|
||||
puts "Done - run with:"
|
||||
puts "ruby #{day_name}/test.rb"
|
||||
end
|
||||
5
lib/tasks/part_two.rb
Normal file
5
lib/tasks/part_two.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
require "tty-prompt"
|
||||
|
||||
def part_two(prompt, day)
|
||||
prompt.say("part two not yet supported")
|
||||
end
|
||||
67
lib/tasks/templates.rb
Normal file
67
lib/tasks/templates.rb
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
TEST_RB = <<~'TEST'
|
||||
require_relative "impl"
|
||||
|
||||
INPUT = <<~IN
|
||||
%example_input%
|
||||
IN
|
||||
|
||||
RESULT = %example_result%
|
||||
|
||||
def test_example
|
||||
parsed = parse(INPUT)
|
||||
result = calculate(parsed)
|
||||
|
||||
if result == RESULT
|
||||
puts "Test successful. Now run with real input"
|
||||
puts "ruby %day_name%/run.rb"
|
||||
else
|
||||
puts "Test failed"
|
||||
puts "expected \"#{RESULT}\" got \"#{result}\""
|
||||
end
|
||||
end
|
||||
|
||||
test_example
|
||||
TEST
|
||||
|
||||
RUN_RB = <<~'RUN'
|
||||
require_relative "impl"
|
||||
|
||||
def print(result)
|
||||
if result.is_a? Array
|
||||
result.map! {|l| "#{l}\n" }
|
||||
end
|
||||
|
||||
puts result
|
||||
end
|
||||
|
||||
def run
|
||||
puts "running your implementation"
|
||||
input = File.new("%day_name%/data").read
|
||||
data = parse(input)
|
||||
result = calculate(data)
|
||||
print(result)
|
||||
end
|
||||
|
||||
run
|
||||
RUN
|
||||
|
||||
IMPL_RB = <<~'IMPL'
|
||||
#prepare the input which is a string containing new lines
|
||||
def parse(input)
|
||||
data = []
|
||||
input.each_line do |line|
|
||||
data << line
|
||||
end
|
||||
data
|
||||
end
|
||||
|
||||
# result should a single string or integer
|
||||
def calculate(data)
|
||||
result = ""
|
||||
data.each do |d|
|
||||
result = d
|
||||
end
|
||||
result
|
||||
end
|
||||
IMPL
|
||||
Reference in New Issue
Block a user