advent 0.1.1 → 0.1.3

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: 2c0c39c2988dbc38f5b3131ddf89f6bd401a40a43b1d9bfc6abdf1c95209f337
4
- data.tar.gz: 7e118fad809b0b052fc7bf3bb52f3076e610a083efba687f1474a5fe8904ee9f
3
+ metadata.gz: d20f95e7e3d0a52a78b93287b93c1864643d94abb70155af037434e254ef18b4
4
+ data.tar.gz: 1c4452e0c6b36b2cda5687398c18f819038b06e7cb71056293b8f427246542d9
5
5
  SHA512:
6
- metadata.gz: 23e3da22b885aab74015af7120f1645e4c069f960f68f5bf30d72c2559b815cd8ee03fdcbb6de4784ccb99c7e4e0d992a156e83dedd38905013e0f028e174de4
7
- data.tar.gz: d554cded9c2cf182aec0ef74d40638471c4f34994ca9e97e5d1d7c860d69b1f1ebad74d17be99d6764a5bc2dc5d437aff23420faa52807955ffe4a5b97bb3260
6
+ metadata.gz: 9e39e7bc77607f5cb2d056377b45a763f6564f3205314e9000a128a53faa6cd758e5c427f9e6255d57a495cf971793703ba45a5eb2dfbb85bf6e15d5e21b1143
7
+ data.tar.gz: 626cb6bb873ac8c5927a302b155c1f2239207e054b8eb186764a0e91929a8afe009af3cf5fb1abd5dd282de36043b9878d7d0d9266dcb4bb6be29b05fa0d8fee
@@ -3,46 +3,49 @@
3
3
  class Advent::CLI::Solver
4
4
  PARTS = [1, 2]
5
5
 
6
- def initialize(command, root_path:, 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
- @root_path = root_path
9
- @year = year
10
- @day = day
13
+ @path = path
11
14
  end
12
15
 
13
16
  def solve
14
- if in_year_directory?
15
- require @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
16
20
  else
17
- require @root_path.join(@year.to_s, solution_file_name)
21
+ require @path
22
+ solution = Object.const_get(solution_class_name).new
18
23
  end
19
24
 
20
- solution = Object.const_get(solution_class_name).new
21
-
22
25
  PARTS.each do |n|
23
26
  method_name = "part#{n}".to_sym
24
- result = if solution.respond_to?(method_name)
25
- solution.public_send(method_name)
26
- else
27
- "Missing"
27
+
28
+ result, colour = if solution.respond_to?(method_name)
29
+ [solution.public_send(method_name), :green]
28
30
  end
29
31
 
30
- @command.say "Part #{n}: #{result}"
32
+ result, colour = ["Missing", :red] if result.nil?
33
+
34
+ @command.say "Part #{n}: #{result}", colour, true
31
35
  end
32
36
  end
33
37
 
34
38
  private
35
39
 
36
- def solution_file_name
37
- "day#{@day}.rb"
40
+ def day
41
+ @_day ||= @path.basename.to_s.match(/day([0-9]+)\.rb/)[1]
38
42
  end
39
43
 
40
- def solution_class_name
41
- "Day#{@day}"
44
+ def solution_file_name
45
+ "day#{day}.rb"
42
46
  end
43
47
 
44
- def in_year_directory?
45
- dir = @root_path.basename.to_s
46
- dir =~ /^20[0-9]{2}/
48
+ def solution_class_name
49
+ "Day#{day}"
47
50
  end
48
51
  end
data/lib/advent/cli.rb CHANGED
@@ -1,35 +1,99 @@
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
 
7
8
  module Advent
8
9
  class CLI < Thor
10
+ include Thor::Actions
11
+
9
12
  class_option :root_path, default: Dir.pwd, hide: true, check_default_type: false
10
13
 
14
+ def initialize(*args)
15
+ super
16
+
17
+ self.destination_root = root_path
18
+ source_paths << File.expand_path("templates", __dir__)
19
+ end
20
+
21
+ # @return [Boolean] defines whether an exit status is set if a command fails
11
22
  def self.exit_on_failure?
12
23
  true
13
24
  end
14
25
 
15
- desc "solve YEAR DAY", "Solve your solution for YEAR and DAY"
16
- def solve(year, day)
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)
29
+ def in_year_directory?
30
+ dir = root_path.basename.to_s
31
+ dir =~ /^20[0-9]{2}/
32
+ end
33
+
34
+ def root_path
35
+ @_root_path ||= if options.root_path.is_a?(Pathname)
36
+ options.root_path
37
+ else
38
+ Pathname.new(options.root_path)
39
+ end
40
+ end
41
+ end
42
+
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.
47
+ def generate(year_or_day, day = nil)
48
+ year, day = if in_year_directory?
49
+ [root_path.basename.to_s, parse_number(year_or_day)]
50
+ else
51
+ [year_or_day, parse_number(day)]
52
+ end
53
+
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
67
+ end
68
+
69
+ desc "solve FILE", "Solve your solution"
70
+ # Runs a solution file, outputting both :part1 and :part2 method return values.
71
+ def solve(path)
17
72
  require "advent/cli/solver"
18
- Solver.new(self, root_path: root_path, year: year, day: day).solve
73
+ Solver.new(self, root_path.join(path)).solve
19
74
  end
20
75
 
21
- 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
22
78
  def version
23
79
  say Advent::VERSION
24
80
  end
25
81
 
26
82
  private
27
83
 
28
- def root_path
29
- @_root_path ||= if options.root_path.is_a?(Pathname)
30
- options.root_path
31
- else
32
- Pathname.new(options.root_path)
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)."
33
97
  end
34
98
  end
35
99
  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,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "advent"
4
+
5
+ class Day<%= day %> < Advent::Solution
6
+ def part1(input: load_input)
7
+ end
8
+
9
+ def part2(input: load_input)
10
+ end
11
+ end
@@ -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.1"
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.1
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
@@ -40,6 +40,9 @@ files:
40
40
  - lib/advent/cli.rb
41
41
  - lib/advent/cli/solver.rb
42
42
  - lib/advent/solution.rb
43
+ - lib/advent/templates/solution.rb.tt
44
+ - lib/advent/templates/solution_test.rb.tt
45
+ - lib/advent/test_case.rb
43
46
  - lib/advent/version.rb
44
47
  homepage: https://github.com/dnlgrv/advent-rb
45
48
  licenses: