Sha256: c18187921fbfb925b97d8200166a19ef8dbb8b092db746beae783d802304c5f6

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

module Aocli
  module Commands
    class StartDay
      def self.run!(options)
        new(
          date: options[:date],
          output_destination: options[:output_destination],
        ).run!
      end

      def initialize(date:, output_destination:)
        @date = date
        @output_destination = output_destination
        @main_file_path = File.join(output_destination, date.year.to_s, "day_#{date.day}", "main.rb")
        @input_file_path = File.join(output_destination, date.year.to_s, "day_#{date.day}", "input.txt")
      end

      def run!
        Aocli::FileUtils.touch_file(main_file_path)
        Aocli::FileUtils.touch_file(input_file_path)
        File.write(main_file_path, main_content)
        File.write(input_file_path, input_content)
      end

      private
      attr_reader :date, :output_destination, :main_file_path, :input_file_path

      def main_content
        problem_description = Aocli::AdventOfCode.get_problem_description(
          year: date.year,
          day: date.day,
        )

        content = Aocli::FileUtils.insert_lines(
          Aocli::FileUtils.wrap_lines(problem_description).split("\n").map { _1 == "" ? "#" : "# #{_1}" },
          into: File.read(File.join(__dir__, "../content/main.txt")),
          after: "##### Part One Description #####",
        )

        Aocli::FileUtils.replace_line(
          content,
          "# __load_input__",
          'input = File.read(__FILE__.gsub("main.rb", "input.txt"))'
        )
      end

      def input_content
        Aocli::AdventOfCode.get_problem_inputs(
          year: date.year,
          day: date.day,
        )
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
aocli-1.4.5 lib/aocli/commands/start_day.rb
aocli-1.4.4 lib/aocli/commands/start_day.rb
aocli-1.4.3 lib/aocli/commands/start_day.rb
aocli-1.4.2 lib/aocli/commands/start_day.rb
aocli-1.4.1 lib/aocli/commands/start_day.rb