advent_of_code_generator 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7edb73d2264ff39da5a3eccb4b3cef4766f920bb5f8c27d798585c6ce2e945ed
4
- data.tar.gz: '059f62d54f1049df523518674d56dcb7db5ab6f96dd5309da149b4a19e203b95'
3
+ metadata.gz: 53cbc2ad0eaa1a550c11ffd1c135628a2350e55adcf50c9aa9787e002d25beb4
4
+ data.tar.gz: 5d643fc40114d44d51d9c3e824b1357d6070d15019a19af75dbe3fd221e02e09
5
5
  SHA512:
6
- metadata.gz: dffc5cd6a6c0aa711532ce643ede0f5fd8dc2ab0e30773e41244c6e722989fb6f16c212b39b870f2bd91de64af320e4b5b31b08ae1062111e5972e36b1c32065
7
- data.tar.gz: 4ac450d516794e5bfd80eecaaf4a0e3196f578941e85eb9e69ee82626f0d8c5c43cf2889a4f5fa8118ab25f4a1e98eb05d1ed1c6c661243b5203110930332054
6
+ metadata.gz: fbcbb2ed2920d92f1e03a4dfbc05a6ad3aad07f64ef0ff50a06707208916e356516a710f795d1aa6b4eb81dea3e37fcbc4578d2308ba42e18ad11fc3a7b21806
7
+ data.tar.gz: 9953c28ad6b9931cf098325044f38d962baf0305057c0143481ea2133469d181cc53a99f5ad719d6aa81a7137cbafba370ed7f7bbd06d6cfbbd86ba811fb5f5a
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdventOfCodeGenerator
4
+ module Commands
5
+ module BaseCommand
6
+ def day
7
+ @day ||= options[:day]
8
+ end
9
+
10
+ def day_name
11
+ return day.to_s if day > 9
12
+
13
+ "0#{day}"
14
+ end
15
+
16
+ def solution_file_path
17
+ "#{day_name}/day#{day_name}.rb"
18
+ end
19
+
20
+ def input_file_path
21
+ "#{day_name}/input.txt"
22
+ end
23
+
24
+ def year
25
+ @year ||= options[:year]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rest-client'
4
+
5
+ module AdventOfCodeGenerator
6
+ module Commands
7
+ class GenerateDay < Thor::Group
8
+ include Thor::Actions
9
+ include AdventOfCodeGenerator::Commands::BaseCommand
10
+
11
+ class_option :day, :type => :numeric
12
+ class_option :year, :type => :numeric
13
+
14
+ def self.source_root
15
+ File.dirname(__FILE__)
16
+ end
17
+
18
+ def create_day_directory
19
+ unless Dir.exist?(day_name)
20
+ say "Creating day #{day} directory"
21
+ Dir.mkdir(day_name)
22
+ end
23
+ end
24
+
25
+ def create_solution_file
26
+ say "Creating day #{day} solution file"
27
+ template('templates/empty_day.tt', solution_file_path)
28
+ end
29
+
30
+ def create_input_file
31
+ say "Creating day #{day} input file"
32
+ create_file input_file_path, fetch_input
33
+ end
34
+
35
+ def prints_done
36
+ say 'Generation done, enjoy !', :blue
37
+ end
38
+
39
+ private
40
+
41
+ def fetch_input
42
+ RestClient.get(
43
+ "https://adventofcode.com/#{year}/day/#{day}/input",
44
+ cookies: { session: ENV.fetch('AOC_COOKIE', '') },
45
+ 'User-Agent' => "github.com/Tyflomate/advent_of_code_generator by florian@tymate.com"
46
+ ).body
47
+ rescue RestClient::BadRequest, RestClient::ExceptionWithResponse => e
48
+ e.response
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdventOfCodeGenerator
4
+ module Commands
5
+ class SolveDay < Thor
6
+ include Thor::Actions
7
+ include AdventOfCodeGenerator::Commands::BaseCommand
8
+
9
+ map "s" => :solve_day
10
+
11
+ option :day, :type => :numeric
12
+ option :year, :type => :numeric
13
+ option :part, :type => :numeric
14
+ desc "start", "Prints your solutions to today parts"
15
+ def start
16
+ raise MissingInputFileError unless File.exist?(input_file_path)
17
+ raise MissingSolutionFileError unless File.exist?(solution_file_path)
18
+
19
+ say "Loading solution file..."
20
+ load(solution_file_path)
21
+
22
+ solve_part_one if solve_part_one?
23
+ solve_part_two if solve_part_two?
24
+ end
25
+
26
+ private
27
+
28
+ def part_option
29
+ @part ||= options[:part]
30
+ end
31
+
32
+ def solve_part_one?
33
+ part_option == 1 || part_option.nil?
34
+ end
35
+
36
+ def solve_part_two?
37
+ part_option == 2 || part_option.nil?
38
+ end
39
+
40
+ def solve_part_one
41
+ say "Resolving part 1..."
42
+ solve_part(1)
43
+ end
44
+
45
+ def solve_part_two
46
+ say "Resolving part 2..."
47
+ solve_part(2)
48
+ end
49
+
50
+ def solve_part(part)
51
+ result = Object.const_get("Day#{day_name}").new.send("part#{part}")
52
+
53
+ say "Part #{part} result: #{result}\n"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Day<%= day_name %>
4
4
  def initialize
5
- @input = File.readlines('./<%= day_name %>/input.txt')
5
+ @input = File.readlines('./<%= day_name %>/input.txt', chomp: true)
6
6
  end
7
7
 
8
8
  def part1
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'commands/base_command'
4
+ require_relative 'commands/generate_day'
5
+ require_relative 'commands/solve_day'
@@ -17,4 +17,16 @@ module AdventOfCodeGenerator
17
17
  "Please insert a valid year between the first iteration of 2015 and current year."
18
18
  end
19
19
  end
20
+
21
+ class MissingSolutionFileError < Error
22
+ def message
23
+ "There is no solution file to execute. Please run generate command first"
24
+ end
25
+ end
26
+
27
+ class MissingInputFileError < Error
28
+ def message
29
+ "There is no input file to read. Please run generate command first"
30
+ end
31
+ end
20
32
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdventOfCode
4
- VERSION = "1.0.1"
4
+ VERSION = "1.1.0"
5
5
  end
@@ -1,9 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "thor"
4
- require "pry"
5
4
  require_relative "advent_of_code_generator/utils"
6
- require_relative "advent_of_code_generator/generate_day"
5
+ require_relative "advent_of_code_generator/commands"
7
6
 
8
7
  module AdventOfCodeGenerator
9
8
  class Error < StandardError; end
@@ -12,7 +11,8 @@ module AdventOfCodeGenerator
12
11
  include Thor::Actions
13
12
  include AdventOfCodeGenerator::Parser
14
13
 
15
- map "-g" => :generate
14
+ map "g" => :generate
15
+ map "s" => :solve
16
16
 
17
17
  option :day, :type => :numeric, :required => true, :aliases => "-d"
18
18
  option :year, :type => :numeric, :aliases => "-y"
@@ -26,7 +26,23 @@ module AdventOfCodeGenerator
26
26
  say e.message, :red
27
27
  end
28
28
 
29
- register(AdventOfCodeGenerator::GenerateDay, "generateDay", "generateDay", "Generate days files")
29
+ option :day, :type => :numeric, :required => true, :aliases => "-d"
30
+ option :year, :type => :numeric, :aliases => "-y"
31
+ option :part, :type => :numeric
32
+ desc "solve", "Print results of your solution"
33
+ def solve
34
+ day = parse_day(options[:day])
35
+ year = parse_year(options[:year])
36
+
37
+ invoke 'solveDay', ['start'], :day => day, :year => year, :part => options[:part]
38
+ rescue AdventOfCodeGenerator::Error => e
39
+ say e.message, :red
40
+ end
41
+
42
+ register(AdventOfCodeGenerator::Commands::GenerateDay, "generateDay", "generateDay", "Generate days files")
43
+
44
+ desc "solveDay", 'Prints results for your solution'
45
+ subcommand "solveDay", AdventOfCodeGenerator::Commands::SolveDay
30
46
 
31
47
  def self.exit_on_failure?
32
48
  true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: advent_of_code_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Lecointe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-03 00:00:00.000000000 Z
11
+ date: 2022-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -59,8 +59,11 @@ files:
59
59
  - Rakefile
60
60
  - exe/aoc
61
61
  - lib/advent_of_code_generator.rb
62
- - lib/advent_of_code_generator/generate_day.rb
63
- - lib/advent_of_code_generator/templates/empty_day.tt
62
+ - lib/advent_of_code_generator/commands.rb
63
+ - lib/advent_of_code_generator/commands/base_command.rb
64
+ - lib/advent_of_code_generator/commands/generate_day.rb
65
+ - lib/advent_of_code_generator/commands/solve_day.rb
66
+ - lib/advent_of_code_generator/commands/templates/empty_day.tt
64
67
  - lib/advent_of_code_generator/utils.rb
65
68
  - lib/advent_of_code_generator/utils/error.rb
66
69
  - lib/advent_of_code_generator/utils/parser.rb
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rest-client'
4
-
5
- module AdventOfCodeGenerator
6
- class GenerateDay < Thor::Group
7
- include Thor::Actions
8
-
9
- class_option :day, :type => :numeric
10
- class_option :year, :type => :numeric
11
-
12
- def self.source_root
13
- File.dirname(__FILE__)
14
- end
15
-
16
- def create_day_directory
17
- unless Dir.exist?(day_name)
18
- say "Creating day #{day} directory", :green
19
- Dir.mkdir(day_name)
20
- end
21
- end
22
-
23
- def create_solution_file
24
- say "Creating day #{day} solution file", :green
25
- template('templates/empty_day.tt', "#{day_name}/day#{day_name}.rb")
26
- end
27
-
28
- def create_input_file
29
- say "Creating day #{day} input file", :green
30
- create_file "#{day_name}/input.txt", fetch_input
31
- end
32
-
33
- def prints_done
34
- say 'Generation done, enjoy !', :blue
35
- end
36
-
37
- private
38
-
39
- def fetch_input
40
- binding.pry
41
- RestClient.get(
42
- "https://adventofcode.com/#{year}/day/#{day}/input",
43
- cookies: { session: ENV.fetch('AOC_COOKIE', '') },
44
- 'User-Agent' => "github.com/Tyflomate/advent_of_code_generator by florian@tymate.com"
45
- ).body
46
- rescue RestClient::BadRequest, RestClient::ExceptionWithResponse => e
47
- e.response
48
- end
49
-
50
- def day
51
- @day ||= options[:day]
52
- end
53
-
54
- def day_name
55
- return day.to_s if day > 9
56
-
57
- "0#{day}"
58
- end
59
-
60
- def year
61
- @year ||= options[:year]
62
- end
63
- end
64
- end