Module: AocRb::Puzzle
Instance Method Summary collapse
- #create_puzzle(year, day) ⇒ Object
- #create_required_directories(year, day) ⇒ Object
- #create_source(year, day) ⇒ Object
- #create_spec(year, day) ⇒ Object
- #create_templates(year, day) ⇒ Object
- #fetch_instructions(year, day) ⇒ Object
- #instructions_exist?(year, day, part) ⇒ Boolean
- #padded(day) ⇒ Object
- #parse_and_save_instructions(year, day, content) ⇒ Object
- #process_article(year, day, article, index) ⇒ Object
- #process_children(f, children, indent_level = 0, strip_em = false) ⇒ Object
- #process_page_content(f, child, indent_level = 0, strip_em = false) ⇒ Object
Instance Method Details
#create_puzzle(year, day) ⇒ Object
[View source]
56 57 58 |
# File 'lib/aoc_rb/puzzle.rb', line 56 def create_puzzle(year, day) end |
#create_required_directories(year, day) ⇒ Object
[View source]
65 66 67 68 |
# File 'lib/aoc_rb/puzzle.rb', line 65 def create_required_directories(year, day) year_directory = File.join("challenges", year.to_s, padded(day)) FileUtils.mkdir_p(year_directory) unless Dir.exist?(year_directory) end |
#create_source(year, day) ⇒ Object
[View source]
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/aoc_rb/puzzle.rb', line 19 def create_source(year, day) source_dir = File.join("challenges", year.to_s, padded(day)) source_path = File.join(source_dir, "solution.rb") FileUtils.mkdir_p(source_dir) unless Dir.exist?(source_dir) PuzzleInput.skip_if_exists(source_path) do template = File.read(File.join(File.dirname(__FILE__), "../../templates", "solution.rb.erb")) @year = year.to_s @day = padded(day) File.open(source_path, "w") do |f| f.write ERB.new(template).result(binding) end end end |
#create_spec(year, day) ⇒ Object
[View source]
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/aoc_rb/puzzle.rb', line 33 def create_spec(year, day) spec_dir = File.join("spec", year.to_s, padded(day)) spec_path = File.join(spec_dir, "solution_spec.rb") FileUtils.mkdir_p(spec_dir) unless Dir.exist?(spec_dir) PuzzleInput.skip_if_exists(spec_path) do template = File.read(File.join(File.dirname(__FILE__), "../../templates", "solution_spec.rb.erb")) @year = year.to_s @day = padded(day) File.open(spec_path, "w") do |f| f.write ERB.new(template).result(binding) end end end |
#create_templates(year, day) ⇒ Object
[View source]
10 11 12 13 |
# File 'lib/aoc_rb/puzzle.rb', line 10 def create_templates(year, day) create_source(year, day) create_spec(year, day) end |
#fetch_instructions(year, day) ⇒ Object
[View source]
47 48 49 50 51 52 53 54 |
# File 'lib/aoc_rb/puzzle.rb', line 47 def fetch_instructions(year, day) create_required_directories year, day api = AocRb::AocApi.new(ENV['AOC_COOKIE']) content = api.puzzle_instructions(year, day) parse_and_save_instructions(year, day, content.body) end |
#instructions_exist?(year, day, part) ⇒ Boolean
60 61 62 63 |
# File 'lib/aoc_rb/puzzle.rb', line 60 def instructions_exist?(year, day, part) filename = File.join("challenges", year.to_s, padded(day), "#{part}.md") File.exist?(filename) end |
#padded(day) ⇒ Object
[View source]
15 16 17 |
# File 'lib/aoc_rb/puzzle.rb', line 15 def padded(day) day.to_s.rjust(2, "0") end |
#parse_and_save_instructions(year, day, content) ⇒ Object
[View source]
70 71 72 73 74 |
# File 'lib/aoc_rb/puzzle.rb', line 70 def parse_and_save_instructions(year, day, content) doc = Nokogiri::HTML(content) articles = doc.css("article.day-desc") articles.each_with_index { |article, index| process_article(year, day, article, index) } end |
#process_article(year, day, article, index) ⇒ Object
[View source]
76 77 78 79 80 81 82 83 84 |
# File 'lib/aoc_rb/puzzle.rb', line 76 def process_article(year, day, article, index) part_num = index + 1 filename = File.join("challenges", year.to_s, padded(day), "part_#{part_num}.md") File.open(filename, "w") do |f| process_page_content(f, article) f.close end end |
#process_children(f, children, indent_level = 0, strip_em = false) ⇒ Object
[View source]
124 125 126 127 128 |
# File 'lib/aoc_rb/puzzle.rb', line 124 def process_children(f, children, indent_level = 0, strip_em = false) children.each do |child| process_page_content f, child, indent_level, strip_em end end |
#process_page_content(f, child, indent_level = 0, strip_em = false) ⇒ Object
[View source]
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/aoc_rb/puzzle.rb', line 86 def process_page_content(f, child, indent_level = 0, strip_em = false) # byebug return unless child.respond_to?(:name) case child.name when "text" f.write child.content.chomp.gsub("\n", "\n" + (" " * indent_level)) when "h2" f.write "## " process_children f, child.children, indent_level, strip_em f.write "\n\n" when "p" process_children f, child.children, indent_level, strip_em f.write "\n\n" when "ul" process_children f, child.children, indent_level, strip_em f.write "\n" when "li" f.write "* " process_children f, child.children, indent_level, strip_em f.write "\n" when "em" f.write "**" unless strip_em process_children f, child.children f.write "**" unless strip_em when "code" f.write " " * indent_level f.write "``" unless indent_level > 0 process_children f, child.children, indent_level, true f.write "``" unless indent_level > 0 when "pre" process_children f, child.children, 4, strip_em f.write "\n\n" else process_children f, child.children, indent_level, strip_em # byebug end end |