advent_of_code_generator 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51f8c6cf08b0d959d77d63605692be3fdc52836a595d104ae2bd300329d9c728
4
- data.tar.gz: 75fac0e3ca5546c6401ff5827a3c7b5d69977421f74c11d68a249f04c8ac5f72
3
+ metadata.gz: 53cbc2ad0eaa1a550c11ffd1c135628a2350e55adcf50c9aa9787e002d25beb4
4
+ data.tar.gz: 5d643fc40114d44d51d9c3e824b1357d6070d15019a19af75dbe3fd221e02e09
5
5
  SHA512:
6
- metadata.gz: 29e91e5d1c5807af5eef5866fc016b6bdc841cf6f17184f7c69ce36a96488e8b561826392214189c68e741b2690093f5ba9e6c10a854795024371dbc5cb138b4
7
- data.tar.gz: 9001b4cb32201da0769064005f69bf736d39a99ce57b317e2bf19d3d55f960a0cc87e8896f3a535b4d1f3e2724b6abafc4fc5a72a33952cfbdabb115c6ab4120
6
+ metadata.gz: fbcbb2ed2920d92f1e03a4dfbc05a6ad3aad07f64ef0ff50a06707208916e356516a710f795d1aa6b4eb81dea3e37fcbc4578d2308ba42e18ad11fc3a7b21806
7
+ data.tar.gz: 9953c28ad6b9931cf098325044f38d962baf0305057c0143481ea2133469d181cc53a99f5ad719d6aa81a7137cbafba370ed7f7bbd06d6cfbbd86ba811fb5f5a
data/README.md CHANGED
@@ -15,7 +15,28 @@ Then run bundle install and your up to go !
15
15
 
16
16
  ## Usage
17
17
 
18
- TODO: Write usage instructions here
18
+ ### Generate
19
+
20
+ For this gem to work, we need to use you Advent of code session cookie in order to fetch your input. You can find
21
+ it in the cookies of https://adventofcode.com/2022/day/DAY/input. Add it to you environment variables as:
22
+
23
+ ```
24
+ export AOC_COOKIE=COOKIE
25
+ ```
26
+
27
+ To generate a day of the current year, run:
28
+
29
+ $ bundle exec aoc generate --day DAY
30
+
31
+ This will create a directory for the day DAY with the ruby file class to solve the puzzle and the input file.
32
+
33
+ ### Solve
34
+
35
+ Incomming command
36
+
37
+ ### Publish
38
+
39
+ Incomming command
19
40
 
20
41
  ## Contributing
21
42
 
@@ -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
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Day<%= day %>
3
+ class Day<%= day_name %>
4
4
  def initialize
5
- @input = File.readlines('./<%= day %>/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
@@ -3,7 +3,7 @@
3
3
  module AdventOfCodeGenerator
4
4
  module Parser
5
5
  def parse_day(day)
6
- return day_to_string(day) if day > 0 && day < 26
6
+ return day if day > 0 && day < 26
7
7
 
8
8
  raise InvalidDayError
9
9
  end
@@ -15,13 +15,5 @@ module AdventOfCodeGenerator
15
15
 
16
16
  raise InvalidYearError
17
17
  end
18
-
19
- private
20
-
21
- def day_to_string(day)
22
- return day.to_s if day > 9
23
-
24
- "0#{day}"
25
- end
26
18
  end
27
19
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdventOfCode
4
- VERSION = "1.0.0"
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"
@@ -21,12 +21,28 @@ module AdventOfCodeGenerator
21
21
  day = parse_day(options[:day])
22
22
  year = parse_year(options[:year])
23
23
 
24
- invoke 'generateDay', :day => day, :year => year
24
+ invoke 'generateDay', [], :day => day, :year => year
25
25
  rescue AdventOfCodeGenerator::Error => e
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.0
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-02 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,57 +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
10
- class_option :year
11
-
12
- def self.source_root
13
- File.dirname(__FILE__)
14
- end
15
-
16
- def create_day_directory
17
- unless Dir.exist?(day)
18
- say "Creating day #{day} directory", :green
19
- Dir.mkdir(day)
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}/#{day}.rb")
26
- end
27
-
28
- def create_input_file
29
- say "Creating day #{day} input file", :green
30
- create_file "#{day}/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
- RestClient.get(
41
- "https://adventofcode.com/#{year}/day/#{day.to_i}/input",
42
- cookies: { session: ENV.fetch('AOC_COOKIE', '') },
43
- 'User-Agent' => "github.com/Tyflomate/advent_of_code_generator by florian@tymate.com"
44
- ).body
45
- rescue RestClient::BadRequest, RestClient::ExceptionWithResponse => e
46
- e.response
47
- end
48
-
49
- def day
50
- options[:day]
51
- end
52
-
53
- def year
54
- options[:year]
55
- end
56
- end
57
- end