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 +4 -4
- data/lib/advent/cli/solver.rb +24 -21
- data/lib/advent/cli.rb +73 -9
- data/lib/advent/solution.rb +6 -1
- data/lib/advent/templates/solution.rb.tt +11 -0
- data/lib/advent/templates/solution_test.rb.tt +20 -0
- data/lib/advent/test_case.rb +6 -0
- data/lib/advent/version.rb +1 -1
- data/lib/advent.rb +1 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d20f95e7e3d0a52a78b93287b93c1864643d94abb70155af037434e254ef18b4
|
4
|
+
data.tar.gz: 1c4452e0c6b36b2cda5687398c18f819038b06e7cb71056293b8f427246542d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e39e7bc77607f5cb2d056377b45a763f6564f3205314e9000a128a53faa6cd758e5c427f9e6255d57a495cf971793703ba45a5eb2dfbb85bf6e15d5e21b1143
|
7
|
+
data.tar.gz: 626cb6bb873ac8c5927a302b155c1f2239207e054b8eb186764a0e91929a8afe009af3cf5fb1abd5dd282de36043b9878d7d0d9266dcb4bb6be29b05fa0d8fee
|
data/lib/advent/cli/solver.rb
CHANGED
@@ -3,46 +3,49 @@
|
|
3
3
|
class Advent::CLI::Solver
|
4
4
|
PARTS = [1, 2]
|
5
5
|
|
6
|
-
|
6
|
+
if RUBY_VERSION >= "3.1"
|
7
|
+
module Solutions
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(command, path)
|
7
12
|
@command = command
|
8
|
-
@
|
9
|
-
@year = year
|
10
|
-
@day = day
|
13
|
+
@path = path
|
11
14
|
end
|
12
15
|
|
13
16
|
def solve
|
14
|
-
if
|
15
|
-
|
17
|
+
if RUBY_VERSION >= "3.1"
|
18
|
+
load @path, Solutions
|
19
|
+
solution = Solutions.const_get(solution_class_name).new
|
16
20
|
else
|
17
|
-
require @
|
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
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
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
|
37
|
-
|
40
|
+
def day
|
41
|
+
@_day ||= @path.basename.to_s.match(/day([0-9]+)\.rb/)[1]
|
38
42
|
end
|
39
43
|
|
40
|
-
def
|
41
|
-
"
|
44
|
+
def solution_file_name
|
45
|
+
"day#{day}.rb"
|
42
46
|
end
|
43
47
|
|
44
|
-
def
|
45
|
-
|
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
|
-
|
16
|
-
|
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
|
73
|
+
Solver.new(self, root_path.join(path)).solve
|
19
74
|
end
|
20
75
|
|
21
|
-
desc "version", "Prints the current version of
|
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
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
data/lib/advent/solution.rb
CHANGED
@@ -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
|
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
|
data/lib/advent/version.rb
CHANGED
data/lib/advent.rb
CHANGED
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.
|
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-
|
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:
|