advent 0.1.2 → 0.1.3

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: 44488f23b0a385b7060d3a2d64d6538dc1e738bbd8600bcc405e1735ace44ff2
4
- data.tar.gz: aa26c72769ec2cc0eeb05bf4ed67e3619470e7e1e7e77465c0b19c69b65abe2b
3
+ metadata.gz: d20f95e7e3d0a52a78b93287b93c1864643d94abb70155af037434e254ef18b4
4
+ data.tar.gz: 1c4452e0c6b36b2cda5687398c18f819038b06e7cb71056293b8f427246542d9
5
5
  SHA512:
6
- metadata.gz: 8be6f552d4fd5930143da51281cf204b7e93f680358578927094f97dde8443d1d5c4307641286edf14fa2454fde7058abd2a2422afe107d4a857b1bdfdd5129b
7
- data.tar.gz: 51575d6fd1d772627c00e199c69f07c4069429787345fa239312effbeac090604c35bf9ab3c1dada8a13de0e75c8a41c83afccfe91723507eb06c36ee6692f8d
6
+ metadata.gz: 9e39e7bc77607f5cb2d056377b45a763f6564f3205314e9000a128a53faa6cd758e5c427f9e6255d57a495cf971793703ba45a5eb2dfbb85bf6e15d5e21b1143
7
+ data.tar.gz: 626cb6bb873ac8c5927a302b155c1f2239207e054b8eb186764a0e91929a8afe009af3cf5fb1abd5dd282de36043b9878d7d0d9266dcb4bb6be29b05fa0d8fee
@@ -3,21 +3,25 @@
3
3
  class Advent::CLI::Solver
4
4
  PARTS = [1, 2]
5
5
 
6
- def initialize(command, year:, day:)
6
+ if RUBY_VERSION >= "3.1"
7
+ module Solutions
8
+ end
9
+ end
10
+
11
+ def initialize(command, path)
7
12
  @command = command
8
- @year = year
9
- @day = day
13
+ @path = path
10
14
  end
11
15
 
12
16
  def solve
13
- if @command.in_year_directory?
14
- require @command.root_path.join(solution_file_name).to_s
17
+ if RUBY_VERSION >= "3.1"
18
+ load @path, Solutions
19
+ solution = Solutions.const_get(solution_class_name).new
15
20
  else
16
- require @command.root_path.join(@year.to_s, solution_file_name)
21
+ require @path
22
+ solution = Object.const_get(solution_class_name).new
17
23
  end
18
24
 
19
- solution = Object.const_get(solution_class_name).new
20
-
21
25
  PARTS.each do |n|
22
26
  method_name = "part#{n}".to_sym
23
27
 
@@ -33,11 +37,15 @@ class Advent::CLI::Solver
33
37
 
34
38
  private
35
39
 
40
+ def day
41
+ @_day ||= @path.basename.to_s.match(/day([0-9]+)\.rb/)[1]
42
+ end
43
+
36
44
  def solution_file_name
37
- "day#{@day}.rb"
45
+ "day#{day}.rb"
38
46
  end
39
47
 
40
48
  def solution_class_name
41
- "Day#{@day}"
49
+ "Day#{day}"
42
50
  end
43
51
  end
data/lib/advent/cli.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "advent"
4
+ require "date"
4
5
  require "pathname"
5
6
  require "thor"
6
7
 
@@ -17,11 +18,14 @@ module Advent
17
18
  source_paths << File.expand_path("templates", __dir__)
18
19
  end
19
20
 
21
+ # @return [Boolean] defines whether an exit status is set if a command fails
20
22
  def self.exit_on_failure?
21
23
  true
22
24
  end
23
25
 
24
26
  no_commands do
27
+ # @return [Boolean] whether the current root_path option is in a
28
+ # directory that looks like a year (eg. 2015)
25
29
  def in_year_directory?
26
30
  dir = root_path.basename.to_s
27
31
  dir =~ /^20[0-9]{2}/
@@ -36,26 +40,61 @@ module Advent
36
40
  end
37
41
  end
38
42
 
39
- desc "generate YEAR DAY or generate DAY in a year directory", "Generate a new solution for YEAR and DAY"
43
+ desc "generate YEAR DAY", "Generate a new solution for YEAR and DAY"
44
+ # Generates a new solution file. If within a year directory, only the day
45
+ # is used, otherwise both the year and day will be required to generate the
46
+ # output.
40
47
  def generate(year_or_day, day = nil)
41
- destination = if in_year_directory?
42
- "day#{year_or_day}.rb"
48
+ year, day = if in_year_directory?
49
+ [root_path.basename.to_s, parse_number(year_or_day)]
43
50
  else
44
- "#{year_or_day}/day#{day}.rb"
51
+ [year_or_day, parse_number(day)]
45
52
  end
46
53
 
47
- template "solution.rb.tt", destination, context: binding
54
+ if (error_message = validate(year, day))
55
+ say_error error_message, :red
56
+ return
57
+ end
58
+
59
+ subpath = if in_year_directory?
60
+ ""
61
+ else
62
+ "#{year}/"
63
+ end
64
+
65
+ template "solution.rb.tt", "#{subpath}day#{day}.rb", context: binding
66
+ template "solution_test.rb.tt", "#{subpath}test/day#{day}_test.rb", context: binding
48
67
  end
49
68
 
50
- desc "solve YEAR DAY", "Solve your solution for YEAR and DAY"
51
- def solve(year, day)
69
+ desc "solve FILE", "Solve your solution"
70
+ # Runs a solution file, outputting both :part1 and :part2 method return values.
71
+ def solve(path)
52
72
  require "advent/cli/solver"
53
- Solver.new(self, year: year, day: day).solve
73
+ Solver.new(self, root_path.join(path)).solve
54
74
  end
55
75
 
56
- desc "version", "Prints the current version of Advent"
76
+ desc "version", "Prints the current version of the gem"
77
+ # Prints the current version of the gem
57
78
  def version
58
79
  say Advent::VERSION
59
80
  end
81
+
82
+ private
83
+
84
+ def parse_number(str)
85
+ if (m = str.match(/[0-9]+/))
86
+ m[0]
87
+ end
88
+ end
89
+
90
+ def validate(year, day)
91
+ if year.to_i < 2014
92
+ "Advent of Code only started in 2014!"
93
+ elsif year.to_i > Date.today.year
94
+ "Future years are not supported."
95
+ elsif !(1..25).cover? day.to_i
96
+ "Day must be between 1 and 25 (inclusive)."
97
+ end
98
+ end
60
99
  end
61
100
  end
@@ -3,8 +3,13 @@
3
3
  require "pathname"
4
4
 
5
5
  module Advent
6
+ # Baes class for an Advent of Code solution attempt. If subclass is in a
7
+ # directory matching a year (eg. 2015) and the filename is for a particular
8
+ # day (eg. day1.rb) then the @year and @day instance variables are
9
+ # automatically inferred.
6
10
  class Solution
7
- attr_reader :year, :day
11
+ attr_reader :year # @return [Numeric] the year this solution is from
12
+ attr_reader :day # @return [Numeric] the day this solution is for
8
13
 
9
14
  def initialize
10
15
  year, day = infer_year_and_day_from_file_system
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "advent"
4
+ require "minitest/autorun"
5
+
6
+ require_relative "../day<%= day %>"
7
+
8
+ class Day<%= day %>Test < Advent::TestCase
9
+ def setup
10
+ @solution = Day<%= day %>.new
11
+ end
12
+
13
+ # def test_part1
14
+ # assert_equal 123, @solution.part1
15
+ # end
16
+
17
+ # def test_part2
18
+ # assert_equal 123, @solution.part2
19
+ # end
20
+ end
@@ -0,0 +1,6 @@
1
+ require "minitest"
2
+
3
+ module Advent
4
+ class TestCase < Minitest::Test
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Advent
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/advent.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "advent/solution"
4
+ require_relative "advent/test_case"
4
5
  require_relative "advent/version"
5
6
 
6
7
  module Advent
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: advent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Grieve
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-21 00:00:00.000000000 Z
11
+ date: 2022-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -41,6 +41,8 @@ files:
41
41
  - lib/advent/cli/solver.rb
42
42
  - lib/advent/solution.rb
43
43
  - lib/advent/templates/solution.rb.tt
44
+ - lib/advent/templates/solution_test.rb.tt
45
+ - lib/advent/test_case.rb
44
46
  - lib/advent/version.rb
45
47
  homepage: https://github.com/dnlgrv/advent-rb
46
48
  licenses: