advent_of_code_cli 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: 40a3d28987a670e64da5dccfaf7f85188048177fcfc71f66da9549a2ada459c1
4
+ data.tar.gz: 4c971d73e6938e037554be47b30d08c332bdc3660260bcd75bdb98d63923f238
5
+ SHA512:
6
+ metadata.gz: bfd5752831e6b2e1e4966f8ad238171ff0295f74265d09a9be531a81d0546cd78f2953c46fcf81ee38c444a97e3e7c9099767f41c7d9064782cb9cc5bb300bfb
7
+ data.tar.gz: 176c43dd61b0a5eaeccec0b784ffe6a3e52d7d2c91eebbce108a60412dd4f90cfba4de11ac6d001cb1811018c19dfa7b59748d974b6529ddbc534ee6a0c66b96
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ Enabled: true
13
+ EnforcedStyle: double_quotes
14
+
15
+ Layout/LineLength:
16
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in advent_of_code_cli.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "thor"
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ advent_of_code_cli (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ json (2.6.2)
11
+ minitest (5.16.3)
12
+ parallel (1.22.1)
13
+ parser (3.1.3.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.6.1)
18
+ rexml (3.2.5)
19
+ rubocop (1.39.0)
20
+ json (~> 2.3)
21
+ parallel (~> 1.10)
22
+ parser (>= 3.1.2.1)
23
+ rainbow (>= 2.2.2, < 4.0)
24
+ regexp_parser (>= 1.8, < 3.0)
25
+ rexml (>= 3.2.5, < 4.0)
26
+ rubocop-ast (>= 1.23.0, < 2.0)
27
+ ruby-progressbar (~> 1.7)
28
+ unicode-display_width (>= 1.4.0, < 3.0)
29
+ rubocop-ast (1.24.0)
30
+ parser (>= 3.1.1.0)
31
+ ruby-progressbar (1.11.0)
32
+ thor (1.2.1)
33
+ unicode-display_width (2.3.0)
34
+
35
+ PLATFORMS
36
+ arm64-darwin-21
37
+
38
+ DEPENDENCIES
39
+ advent_of_code_cli!
40
+ minitest (~> 5.0)
41
+ rake (~> 13.0)
42
+ rubocop (~> 1.21)
43
+ thor
44
+
45
+ BUNDLED WITH
46
+ 2.3.24
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Emily Samp
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
+ # AdventOfCodeCli
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/advent_of_code_cli`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add advent_of_code_cli
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install advent_of_code_cli
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
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_of_code_cli.
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,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "advent_of_code_cli"
5
+
6
+ AdventOfCode::CLI.start(ARGV)
data/exe/aoc_cli ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "advent_of_code_cli"
5
+
6
+ AdventOfCode::CLI.start(ARGV)
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdventOfCode
4
+ module Commands
5
+ class Command
6
+ include Thor::Base
7
+
8
+ def initialize(day:)
9
+ raise InvalidDayError unless day.is_a?(Integer) && day.positive? && day <= 25
10
+
11
+ @day = day
12
+ end
13
+
14
+ private
15
+
16
+ def day_string
17
+ @day < 10 ? "0#{@day}" : @day.to_s
18
+ end
19
+
20
+ def solution_file_name
21
+ "#{day_string}.rb"
22
+ end
23
+
24
+ def input_file_name
25
+ "inputs/#{day_string}.txt"
26
+ end
27
+
28
+ def create_file(file_name, contents = nil)
29
+ File.open(file_name, "w") do |file|
30
+ file.puts contents if contents
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdventOfCode
4
+ module Commands
5
+ class Cookie
6
+ def initialize(value:)
7
+ @value = value
8
+ end
9
+
10
+ def execute
11
+ say "Creating cookie.txt file..."
12
+ File.open("cookie.txt", "w") do |file|
13
+ file.puts @value
14
+ end
15
+
16
+ say "Done!", :green
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+
6
+ module AdventOfCode
7
+ module Commands
8
+ class Download < Command
9
+ def initialize(year:, day:)
10
+ @year = year
11
+ super(day: day)
12
+ end
13
+
14
+ def execute
15
+ raise MissingCookieError unless cookie_present? && cookie
16
+
17
+ say("Fetching input...")
18
+ input = fetch_input
19
+
20
+ unless Dir.exist?("inputs")
21
+ say("Creating inputs directory...")
22
+ Dir.mkdir("inputs")
23
+ end
24
+
25
+ say("Writing input to #{input_file_name}...")
26
+ create_file(input_file_name, input)
27
+
28
+ say("Done!", :green)
29
+ end
30
+
31
+ private
32
+
33
+ def cookie
34
+ @cookie ||= File.read("cookie.txt").strip
35
+ end
36
+
37
+ def cookie_present?
38
+ File.exist?("cookie.txt")
39
+ end
40
+
41
+ def fetch_input
42
+ url = URI("https://adventofcode.com/#{@year}/day/#{@day}/input")
43
+
44
+ https = Net::HTTP.new(url.host, url.port)
45
+ https.use_ssl = true
46
+
47
+ request = Net::HTTP::Get.new(url)
48
+ request["Cookie"] = "session=#{cookie}"
49
+
50
+ response = https.request(request)
51
+ response.read_body
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdventOfCode
4
+ module Commands
5
+ class Scaffold < Command
6
+ def execute
7
+ say("Creating file: #{solution_file_name}...")
8
+ create_file(solution_file_name, solution_file_contents)
9
+
10
+ unless Dir.exist?("inputs")
11
+ say("Creating inputs directory...")
12
+ Dir.mkdir("inputs")
13
+ end
14
+
15
+ say("Creating file: #{input_file_name}...")
16
+ create_file(input_file_name)
17
+
18
+ unless Dir.exist?("examples")
19
+ say("Creating examples directory...")
20
+ Dir.mkdir("examples")
21
+ end
22
+
23
+ unless Dir.exist?("examples/#{day_string}")
24
+ say("Creating examples/#{day_string} directory...")
25
+ Dir.mkdir("examples/#{day_string}")
26
+ end
27
+
28
+ say "Done!", :green
29
+ end
30
+
31
+ private
32
+
33
+ def solution_file_contents
34
+ <<~RUBY
35
+ module Day#{day_string}
36
+ class << self
37
+ def part_one(input)
38
+ raise NotImplementedError
39
+ end
40
+
41
+ def part_two(input)
42
+ raise NotImplementedError
43
+ end
44
+ end
45
+ end
46
+ RUBY
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'benchmark'
4
+
5
+ module AdventOfCode
6
+ module Commands
7
+ class Solve < Command
8
+ def execute
9
+ raise MissingInputError unless File.exist?(input_file_name)
10
+ raise MissingSolutionError unless File.exist?(solution_file_name)
11
+
12
+ say "Reading input..."
13
+ input = File.read(input_file_name).strip
14
+
15
+ say "Loading solution..."
16
+ load(solution_file_name)
17
+
18
+ module_name = "Day#{day_string}"
19
+
20
+ say "\nRunning part one..."
21
+ solution(module_name, "one", input)
22
+
23
+ say "\nRunning part two..."
24
+ solution(module_name, "two", input)
25
+
26
+ say "\nDone!", :green
27
+ end
28
+
29
+ private
30
+
31
+ def solution(module_name, part, input)
32
+ start_time = Time.now
33
+ result = Object.const_get(module_name).send("part_#{part}", input)
34
+ end_time = Time.now
35
+
36
+ say "Part #{part} result: #{result}"
37
+ say "Took #{end_time-start_time} seconds to solve"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "commands/command"
4
+ require_relative "commands/cookie"
5
+ require_relative "commands/download"
6
+ require_relative "commands/scaffold"
7
+ require_relative "commands/solve"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdventOfCode
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+
5
+ require_relative "advent_of_code_cli/version"
6
+ require_relative "advent_of_code_cli/commands"
7
+
8
+ module AdventOfCode
9
+ class Error < StandardError; end
10
+ class InvalidDayError < Error; end
11
+ class MissingCookieError < Error; end
12
+ class MissingInputError < Error; end
13
+ class MissingSolutionError < Error; end
14
+
15
+ class CLI < Thor
16
+ desc "scaffold DAY", "generate files for day DAY"
17
+ def scaffold(day)
18
+ AdventOfCode::Commands::Scaffold.new(day: day.to_i).execute
19
+ rescue AdventOfCode::InvalidDayError
20
+ rescue_invalid_day_error
21
+ end
22
+
23
+ desc "cookie VALUE", "store your Advent of Code cookie with value VALUE in the cookie.txt file"
24
+ def cookie(value)
25
+ AdventOfCode::Commands::Cookie.new(value: value).execute
26
+ end
27
+
28
+ desc "download DAY", "download your input for day DAY"
29
+ option :year, default: Time.now.year.to_s
30
+ def download(day)
31
+ AdventOfCode::Commands::Download.new(day: day.to_i, year: options[:year].to_i).execute
32
+ rescue AdventOfCode::InvalidDayError
33
+ rescue_invalid_day_error
34
+ rescue AdventOfCode::MissingCookieError
35
+ say "Error: Cannot find cookie in cookie.txt file.", :red
36
+ end
37
+
38
+ desc "solve DAY", "run your solutions for day DAY"
39
+ def solve(day)
40
+ AdventOfCode::Commands::Solve.new(day: day.to_i).execute
41
+ rescue AdventOfCode::InvalidDayError
42
+ rescue_invalid_day_error
43
+ rescue AdventOfCode::MissingInputError
44
+ say "Error: Cannot find input file.", :red
45
+ rescue AdventOfCode::MissingSolutionError
46
+ say "Error: Cannot find solution file.", :red
47
+ end
48
+
49
+ private
50
+
51
+ def rescue_invalid_day_error
52
+ say "Error: The day argument must be an integer between 1 and 25.", :red
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: advent_of_code_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emily Samp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - emily.samp@icloud.com
16
+ executables:
17
+ - advent_of_code_cli
18
+ - aoc_cli
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".rubocop.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - exe/advent_of_code_cli
29
+ - exe/aoc_cli
30
+ - lib/advent_of_code_cli.rb
31
+ - lib/advent_of_code_cli/commands.rb
32
+ - lib/advent_of_code_cli/commands/command.rb
33
+ - lib/advent_of_code_cli/commands/cookie.rb
34
+ - lib/advent_of_code_cli/commands/download.rb
35
+ - lib/advent_of_code_cli/commands/scaffold.rb
36
+ - lib/advent_of_code_cli/commands/solve.rb
37
+ - lib/advent_of_code_cli/version.rb
38
+ homepage: https://github.com/egiurleo/advent_of_code_cli
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://github.com/egiurleo/advent_of_code_cli
43
+ source_code_uri: https://github.com/egiurleo/advent_of_code_cli
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.6.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.3.7
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: A CLI tool to scaffold and run Advent of Code solutions.
63
+ test_files: []