advent 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4cdf5d96352bc47ad63cceb8ce3c796fe57a86a00174687ec998b3ad196b408a
4
+ data.tar.gz: 4e04dca6d1b461511df17894298587e2457470813c0b6a30f43229ba5e8fc098
5
+ SHA512:
6
+ metadata.gz: e4e083b0e213e4180716c2791b163ed510754bfa9b172a31986440e108ae54f68e88813a95aedc6df66ddce445cae340a7979b5ce4bb3907f8b4845906edfd9c
7
+ data.tar.gz: cc56db526c42d93f16869a2f05fdf1c8e4f8b2edd10d243fea72d4145cec6264ddc9ecbca1ebdd3901937e17a05d34fbeb7eb2a54458dac6190e2ab1a75bdd04
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Daniel Grieve
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Advent
2
+
3
+ Have fun with the [Advent of Code](https://rubygems.org) using Ruby.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add advent
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install advent
14
+
15
+ ## Usage
16
+
17
+ A list of commands and help is available using `advent`:
18
+
19
+ $ advent help
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/advent.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "standard/rake"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.options = "-p"
11
+ t.test_files = FileList["test/**/*_test.rb"]
12
+ end
13
+
14
+ task default: %i[test]
data/exe/advent ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+
5
+ require "advent/cli"
6
+
7
+ Advent::CLI.start
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Advent::CLI::Solver
4
+ PARTS = [1, 2]
5
+
6
+ def initialize(command, root_path:, year:, day:)
7
+ @command = command
8
+ @root_path = root_path
9
+ @year = year
10
+ @day = day
11
+ end
12
+
13
+ def solve
14
+ if in_year_directory?
15
+ require @root_path.join(solution_file_name).to_s
16
+ else
17
+ require @root_path.join(@year.to_s, solution_file_name)
18
+ end
19
+
20
+ solution = Object.const_get(solution_class_name).new
21
+
22
+ PARTS.each do |n|
23
+ method_name = "part#{n}".to_sym
24
+ result = if solution.respond_to?(method_name)
25
+ solution.public_send(method_name)
26
+ else
27
+ "Missing"
28
+ end
29
+
30
+ @command.say "Part #{n}: #{result}"
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def solution_file_name
37
+ "day#{@day}.rb"
38
+ end
39
+
40
+ def solution_class_name
41
+ "Day#{@day}"
42
+ end
43
+
44
+ def in_year_directory?
45
+ dir = @root_path.basename.to_s
46
+ dir =~ /^20[0-9]{2}/
47
+ end
48
+ end
data/lib/advent/cli.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "advent"
4
+ require "pathname"
5
+ require "thor"
6
+
7
+ module Advent
8
+ class CLI < Thor
9
+ class_option :root_path, default: Dir.pwd, hide: true, check_default_type: false
10
+
11
+ def self.exit_on_failure?
12
+ true
13
+ end
14
+
15
+ desc "solve YEAR DAY", "Solve your solution for YEAR and DAY"
16
+ def solve(year, day)
17
+ require "advent/cli/solver"
18
+ Solver.new(self, root_path: root_path, year: year, day: day).solve
19
+ end
20
+
21
+ desc "version", "Prints the current version of Advent"
22
+ def version
23
+ say Advent::VERSION
24
+ end
25
+
26
+ private
27
+
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)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module Advent
6
+ class Solution
7
+ attr_reader :year, :day
8
+
9
+ def initialize
10
+ year, day = infer_year_and_day_from_file_system
11
+
12
+ @year = year
13
+ @day = day
14
+ end
15
+
16
+ def load_input
17
+ dir = source_location.dirname
18
+ File.read(dir.join(".day#{@day}.input.txt"))
19
+ end
20
+
21
+ private
22
+
23
+ def infer_year_and_day_from_file_system
24
+ path, day = source_location.split
25
+
26
+ day = /day([0-9]+)/.match(day.to_s)[1]
27
+ year = path.basename.to_s
28
+
29
+ [year, day].map(&:to_i)
30
+ rescue
31
+ raise Error, "Failed to infer year/day of solution."
32
+ end
33
+
34
+ def source_location
35
+ class_path, _ = Object.const_source_location(self.class.to_s)
36
+ Pathname.new(class_path)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Advent
4
+ VERSION = "0.1.0"
5
+ end
data/lib/advent.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "advent/solution"
4
+ require_relative "advent/version"
5
+
6
+ module Advent
7
+ class Error < StandardError; end
8
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: advent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Grieve
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ description:
28
+ email:
29
+ - dnlgrv@hey.com
30
+ executables:
31
+ - advent
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - exe/advent
39
+ - lib/advent.rb
40
+ - lib/advent/cli.rb
41
+ - lib/advent/cli/solver.rb
42
+ - lib/advent/solution.rb
43
+ - lib/advent/version.rb
44
+ homepage: https://github.com/dnlgrv/advent
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.7.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.3.7
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Have fun with the Advent of Code.
67
+ test_files: []