advent 0.1.3 → 0.1.4

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: d20f95e7e3d0a52a78b93287b93c1864643d94abb70155af037434e254ef18b4
4
- data.tar.gz: 1c4452e0c6b36b2cda5687398c18f819038b06e7cb71056293b8f427246542d9
3
+ metadata.gz: 360b1871cc09fd664dd8562fd1a29afd9b2e8bbc7a83d9d711c7933a6c62b5ec
4
+ data.tar.gz: 2f9cde07fa43650354a3f11c7cb869efa7c969d4254083ca807bd7a855fcf9d0
5
5
  SHA512:
6
- metadata.gz: 9e39e7bc77607f5cb2d056377b45a763f6564f3205314e9000a128a53faa6cd758e5c427f9e6255d57a495cf971793703ba45a5eb2dfbb85bf6e15d5e21b1143
7
- data.tar.gz: 626cb6bb873ac8c5927a302b155c1f2239207e054b8eb186764a0e91929a8afe009af3cf5fb1abd5dd282de36043b9878d7d0d9266dcb4bb6be29b05fa0d8fee
6
+ metadata.gz: e27b9323e1ad7a07484fe9302dfd49b430bd4fc5433ef0e412e1af14f734bdd1c0a8c719803e108e4b7b3aec144ec942119bc6a07bc93381f4aae9ef634324b1
7
+ data.tar.gz: b8e99258c57406a1cf945e868a1642d8ed8636c983fd960fd3f889bc6760c170bb346e3cdb0cc82e547f880dbd8c6f71898e941defdfd41b97a73974bc4cdec0
data/README.md CHANGED
@@ -16,6 +16,26 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
16
 
17
17
  ## Usage
18
18
 
19
+ Advent expects you to have a working directory resembling something like:
20
+
21
+ $ tree
22
+ .
23
+ ├── 2015
24
+ └── 2016
25
+
26
+ Some commands can be run from within a directory for a specific year, but it's
27
+ better to run from the parent directory where possible.
28
+
29
+ The typical flow for tackling a daily challenge would be:
30
+
31
+ $ advent generate 2015 1 # generate files to work in
32
+ $ advent download 2015 1 # download the input file
33
+
34
+ $ vim 2015/day1.rb 2015/day1_test.rb # do your work
35
+
36
+ $ ruby 2015/day1_test.rb # run any tests you may have
37
+ $ advent solve 2015/day1.rb # get your answers to submit
38
+
19
39
  A list of commands and help is available using `advent`:
20
40
 
21
41
  $ advent help
data/lib/advent/cli.rb CHANGED
@@ -10,6 +10,7 @@ module Advent
10
10
  include Thor::Actions
11
11
 
12
12
  class_option :root_path, default: Dir.pwd, hide: true, check_default_type: false
13
+ class_option :http_module, default: Net::HTTP, check_default_type: false
13
14
 
14
15
  def initialize(*args)
15
16
  super
@@ -30,13 +31,37 @@ module Advent
30
31
  dir = root_path.basename.to_s
31
32
  dir =~ /^20[0-9]{2}/
32
33
  end
34
+ end
35
+
36
+ desc "download YEAR DAY", "Download the input for YEAR and DAY"
37
+ def download(year_or_day, day = nil)
38
+ year, day = determine_year_and_day(year_or_day, day)
39
+
40
+ if (error_message = validate(year, day))
41
+ say_error error_message, :red
42
+ return
43
+ end
44
+
45
+ subpath = if in_year_directory?
46
+ ""
47
+ else
48
+ "#{year}/"
49
+ end
50
+
51
+ unless Advent.session.exist?
52
+ session = ask "What is your Advent of Code session cookie value?", echo: false
53
+ Advent.session.value = session
54
+
55
+ say "\n\nThanks. Psst, we're going to save this for next time. It's in .advent_session if you need to update or delete it.\n\n"
56
+ end
33
57
 
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
58
+ input = Advent::Input.new(root_path.join(subpath), day: day.to_i)
59
+
60
+ if input.download(Advent.session.value, options.http_module)
61
+ say "Input downloaded to #{input.file_path}.", :green
62
+ say "\nUsing #load_input in your daily solution will load the input file for you."
63
+ else
64
+ say_error "Something went wrong, maybe an old session cookie?", :red
40
65
  end
41
66
  end
42
67
 
@@ -45,11 +70,7 @@ module Advent
45
70
  # is used, otherwise both the year and day will be required to generate the
46
71
  # output.
47
72
  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
73
+ year, day = determine_year_and_day(year_or_day, day)
53
74
 
54
75
  if (error_message = validate(year, day))
55
76
  say_error error_message, :red
@@ -81,6 +102,22 @@ module Advent
81
102
 
82
103
  private
83
104
 
105
+ def determine_year_and_day(year_or_day, day)
106
+ if in_year_directory?
107
+ [root_path.basename.to_s, parse_number(year_or_day)]
108
+ else
109
+ [year_or_day, parse_number(day)]
110
+ end
111
+ end
112
+
113
+ def root_path
114
+ @_root_path ||= if options.root_path.is_a?(Pathname)
115
+ options.root_path
116
+ else
117
+ Pathname.new(options.root_path)
118
+ end
119
+ end
120
+
84
121
  def parse_number(str)
85
122
  if (m = str.match(/[0-9]+/))
86
123
  m[0]
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi/cookie"
4
+ require "fileutils"
5
+ require "net/http"
6
+ require "uri"
7
+
8
+ module Advent
9
+ class Input
10
+ def initialize(dir, day:)
11
+ @dir = dir
12
+ @day = day
13
+ end
14
+
15
+ def file_path
16
+ @dir.join(".day#{@day}.input.txt")
17
+ end
18
+
19
+ def exist?
20
+ File.exist? file_path
21
+ end
22
+
23
+ def download(session, http = Net::HTTP)
24
+ session_cookie = CGI::Cookie.new("session", session)
25
+ response = http.get_response(input_url, {"Cookie" => session_cookie.to_s})
26
+
27
+ if success?(response)
28
+ FileUtils.mkdir_p file_path.dirname
29
+ File.write file_path, response.body
30
+ true
31
+ else
32
+ false
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def input_url
39
+ URI("https://adventofcode.com/#{year}/day/#{@day}/input")
40
+ end
41
+
42
+ def year
43
+ @_year ||= @dir.basename
44
+ end
45
+
46
+ def success?(response)
47
+ response.code >= "200" && response.code < "300"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,28 @@
1
+ module Advent
2
+ # Simple class for handling the session cookie file
3
+ class Session
4
+ FILE_NAME = ".advent_session"
5
+
6
+ attr_reader :file_name
7
+
8
+ def initialize(file_name = FILE_NAME)
9
+ @file_name = file_name
10
+ end
11
+
12
+ def clear
13
+ File.delete file_name if exist?
14
+ end
15
+
16
+ def exist?
17
+ File.exist? file_name
18
+ end
19
+
20
+ def value=(val)
21
+ File.write file_name, val
22
+ end
23
+
24
+ def value
25
+ File.read file_name if exist?
26
+ end
27
+ end
28
+ end
@@ -16,11 +16,12 @@ module Advent
16
16
 
17
17
  @year = year
18
18
  @day = day
19
+
20
+ @input = Input.new(source_location.dirname, day: day)
19
21
  end
20
22
 
21
23
  def load_input
22
- dir = source_location.dirname
23
- File.read(dir.join(".day#{@day}.input.txt"))
24
+ File.read(@input.file_path)
24
25
  end
25
26
 
26
27
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Advent
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
data/lib/advent.rb CHANGED
@@ -1,9 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "advent/input"
4
+ require_relative "advent/session"
3
5
  require_relative "advent/solution"
4
6
  require_relative "advent/test_case"
5
7
  require_relative "advent/version"
6
8
 
7
9
  module Advent
8
10
  class Error < StandardError; end
11
+
12
+ class << self
13
+ def session
14
+ @_session = Session.new
15
+ end
16
+ end
9
17
  end
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.3
4
+ version: 0.1.4
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-22 00:00:00.000000000 Z
11
+ date: 2022-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -39,6 +39,8 @@ files:
39
39
  - lib/advent.rb
40
40
  - lib/advent/cli.rb
41
41
  - lib/advent/cli/solver.rb
42
+ - lib/advent/input.rb
43
+ - lib/advent/session.rb
42
44
  - lib/advent/solution.rb
43
45
  - lib/advent/templates/solution.rb.tt
44
46
  - lib/advent/templates/solution_test.rb.tt