Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/advent_of_code_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "thor"
require_relative "advent_of_code_generator/utils"
require_relative "advent_of_code_generator/generate_day"
require_relative "advent_of_code_generator/commands"

module AdventOfCodeGenerator
class Error < StandardError; end
Expand All @@ -11,7 +11,8 @@ class App < Thor
include Thor::Actions
include AdventOfCodeGenerator::Parser

map "-g" => :generate
map "g" => :generate
map "s" => :solve

option :day, :type => :numeric, :required => true, :aliases => "-d"
option :year, :type => :numeric, :aliases => "-y"
Expand All @@ -25,7 +26,23 @@ def generate
say e.message, :red
end

register(AdventOfCodeGenerator::GenerateDay, "generateDay", "generateDay", "Generate days files")
option :day, :type => :numeric, :required => true, :aliases => "-d"
option :year, :type => :numeric, :aliases => "-y"
option :part, :type => :numeric
desc "solve", "Print results of your solution"
def solve
day = parse_day(options[:day])
year = parse_year(options[:year])

invoke 'solveDay', ['start'], :day => day, :year => year, :part => options[:part]
rescue AdventOfCodeGenerator::Error => e
say e.message, :red
end

register(AdventOfCodeGenerator::Commands::GenerateDay, "generateDay", "generateDay", "Generate days files")

desc "solveDay", 'Prints results for your solution'
subcommand "solveDay", AdventOfCodeGenerator::Commands::SolveDay

def self.exit_on_failure?
true
Expand Down
5 changes: 5 additions & 0 deletions lib/advent_of_code_generator/commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require_relative 'commands/base_command'
require_relative 'commands/generate_day'
require_relative 'commands/solve_day'
29 changes: 29 additions & 0 deletions lib/advent_of_code_generator/commands/base_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module AdventOfCodeGenerator
module Commands
module BaseCommand
def day
@day ||= options[:day]
end

def day_name
return day.to_s if day > 9

"0#{day}"
end

def solution_file_path
"#{day_name}/day#{day_name}.rb"
end

def input_file_path
"#{day_name}/input.txt"
end

def year
@year ||= options[:year]
end
end
end
end
81 changes: 35 additions & 46 deletions lib/advent_of_code_generator/commands/generate_day.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,50 @@
require 'rest-client'

module AdventOfCodeGenerator
class GenerateDay < Thor::Group
include Thor::Actions
module Commands
class GenerateDay < Thor::Group
include Thor::Actions
include AdventOfCodeGenerator::Commands::BaseCommand

class_option :day, :type => :numeric
class_option :year, :type => :numeric
class_option :day, :type => :numeric
class_option :year, :type => :numeric

def self.source_root
File.dirname(__FILE__)
end

def create_day_directory
unless Dir.exist?(day_name)
say "Creating day #{day} directory", :green
Dir.mkdir(day_name)
def self.source_root
File.dirname(__FILE__)
end
end

def create_solution_file
say "Creating day #{day} solution file", :green
template('templates/empty_day.tt', "#{day_name}/day#{day_name}.rb")
end

def create_input_file
say "Creating day #{day} input file", :green
create_file "#{day_name}/input.txt", fetch_input
end

def prints_done
say 'Generation done, enjoy !', :blue
end

private
def create_day_directory
unless Dir.exist?(day_name)
say "Creating day #{day} directory"
Dir.mkdir(day_name)
end
end

def fetch_input
RestClient.get(
"https://adventofcode.com/#{year}/day/#{day}/input",
cookies: { session: ENV.fetch('AOC_COOKIE', '') },
'User-Agent' => "github.com/Tyflomate/advent_of_code_generator by florian@tymate.com"
).body
rescue RestClient::BadRequest, RestClient::ExceptionWithResponse => e
e.response
end
def create_solution_file
say "Creating day #{day} solution file"
template('templates/empty_day.tt', solution_file_path)
end

def day
@day ||= options[:day]
end
def create_input_file
say "Creating day #{day} input file"
create_file input_file_path, fetch_input
end

def day_name
return day.to_s if day > 9
def prints_done
say 'Generation done, enjoy !', :blue
end

"0#{day}"
end
private

def year
@year ||= options[:year]
def fetch_input
RestClient.get(
"https://adventofcode.com/#{year}/day/#{day}/input",
cookies: { session: ENV.fetch('AOC_COOKIE', '') },
'User-Agent' => "github.com/Tyflomate/advent_of_code_generator by florian@tymate.com"
).body
rescue RestClient::BadRequest, RestClient::ExceptionWithResponse => e
e.response
end
end
end
end
57 changes: 57 additions & 0 deletions lib/advent_of_code_generator/commands/solve_day.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module AdventOfCodeGenerator
module Commands
class SolveDay < Thor
include Thor::Actions
include AdventOfCodeGenerator::Commands::BaseCommand

map "s" => :solve_day

option :day, :type => :numeric
option :year, :type => :numeric
option :part, :type => :numeric
desc "start", "Prints your solutions to today parts"
def start
raise MissingInputFileError unless File.exist?(input_file_path)
raise MissingSolutionFileError unless File.exist?(solution_file_path)

say "Loading solution file..."
load(solution_file_path)

solve_part_one if solve_part_one?
solve_part_two if solve_part_two?
end

private

def part_option
@part ||= options[:part]
end

def solve_part_one?
part_option == 1 || part_option.nil?
end

def solve_part_two?
part_option == 2 || part_option.nil?
end

def solve_part_one
say "Resolving part 1..."
solve_part(1)
end

def solve_part_two
say "Resolving part 2..."
solve_part(2)
end

def solve_part(part)
result = Object.const_get("Day#{day_name}").new.send("part#{part}")

say "Part #{part} result: #{result}\n"
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Day<%= day_name %>
def initialize
@input = File.readlines('./<%= day_name %>/input.txt')
@input = File.readlines('./<%= day_name %>/input.txt', chomp: true)
end

def part1
Expand Down
12 changes: 12 additions & 0 deletions lib/advent_of_code_generator/utils/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ def message
"Please insert a valid year between the first iteration of 2015 and current year."
end
end

class MissingSolutionFileError < Error
def message
"There is no solution file to execute. Please run generate command first"
end
end

class MissingInputFileError < Error
def message
"There is no input file to read. Please run generate command first"
end
end
end
2 changes: 1 addition & 1 deletion lib/advent_of_code_generator/utils/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module AdventOfCode
VERSION = "1.0.1"
VERSION = "1.1.0"
end