require 'debug' # Generic FileHandler class FileHandler def initialize(file_path) @file_path = file_path end # Usage: # FileHandler.new('/path_to_file').read_lines do |line| # # process line # end def read_lines File.foreach(@file_path, chomp: true).map do |line| yield(line) end end # Usage: # file_content = FileHandler.new('/path_to_file').read_file( def read_file File.read(@file_path) end end class PuzzleSolver def initialize(file_handler, debug: false) @handler = file_handler @debug = debug end def debug(message) puts message if @debug end def print_debug(message) print message if @debug end def solve raise NotImplementedError, 'Please implement this method in subclasses.' end end class HomeworkSolver < PuzzleSolver def calculate(operator, int_array) int_array.reduce(operator) end def solve parsed_lines = @handler.read_file.split("\n").map {|line| line.split(" ")} number_lines = parsed_lines[0...-1].map {|numbers| numbers.map(&:to_i)} operator_line = parsed_lines[-1].map(&:to_sym) solutions = operator_line.map.with_index do |operator, i| ints = number_lines.map {|numbers| numbers[i]} calculate(operator, ints) end solutions.sum end end if ARGV[0].nil? || ARGV[0].empty? puts "Usage: ruby #{__FILE__} [debug]" exit 1 end file_path = ARGV[0] debug = (ARGV[1] == "debug") file_handler = FileHandler.new(file_path) ## Puzzle-specific cls = HomeworkSolver ## puts "The answer is: #{cls.new(file_handler, debug:).solve}"