Class: AocInput
- Inherits:
-
Object
- Object
- AocInput
- Defined in:
- lib/aoc_rb_helpers/aoc_input.rb
Overview
Provides input manipulation helper methods. Methods are chainable, and directly modify the parsed view of the input data within the @data instance variable.
Once manipulated as required, the input is accessable using the #data method.
Instance Method Summary collapse
-
#columns_of_numbers(delimiter = nil) ⇒ AocInput
Splits each string in the data array into an array of numbers.
-
#data ⇒ Object
Returns the parsed puzzle input.
-
#initialize(puzzle_input) ⇒ AocInput
constructor
Returns a new AocInput initialized with the given puzzle input.
-
#multiple_lines ⇒ AocInput
Splits the input string into an array of lines.
-
#process_each_line {|line| ... } ⇒ self, Enumerator
Processes each line of the data using the provided block.
-
#sections(delimiter = "\n\n") ⇒ Array<AocInput>
Returns a new
AocInput
for each section of the raw input, split by the given delimiter. -
#single_line ⇒ AocInput
Strips newline characters from the data, leaving a single line of input.
-
#sort_arrays ⇒ AocInput
Sorts each array within the @data array.
-
#to_grid ⇒ Grid
Returns a new
Grid
object from the parsed input. -
#transpose ⇒ AocInput
Transposes the data array.
Constructor Details
#initialize(puzzle_input) ⇒ AocInput
Returns a new AocInput initialized with the given puzzle input.
11 12 13 14 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 11 def initialize(puzzle_input) @raw_input = puzzle_input configure_method_access end |
Instance Method Details
#columns_of_numbers(delimiter = nil) ⇒ AocInput
Splits each string in the data array into an array of numbers.
This method processes @data by splitting each string in the array using the specified delimiter, then converting each resulting element to an integer. It modifies @data directly and enables chaining by returning self
.
83 84 85 86 87 88 89 90 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 83 def columns_of_numbers(delimiter = nil) can_call?(:columns_of_numbers, "call .multiple_lines first") @data = data.map { |line| line.split(delimiter).map(&:to_i) } allow(:sort_arrays) allow(:transpose) revoke(:columns_of_numbers) self end |
#data ⇒ Object
Returns the parsed puzzle input.
If the data has not been parsed yet, it defaults to the raw input provided in #initialize.
21 22 23 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 21 def data @data ||= @raw_input end |
#multiple_lines ⇒ AocInput
Splits the input string into an array of lines.
This method processes @data by splitting the input string into multiple lines, removing trailing newline characters. It modifies @data directly and returns self
to enable method chaining.
43 44 45 46 47 48 49 50 51 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 43 def multiple_lines can_call?(:multiple_lines) @data = data.lines(chomp: true) allow(:columns_of_numbers) allow(:process_each_line) revoke(:multiple_lines) revoke(:single_line) self end |
#process_each_line {|line| ... } ⇒ self, Enumerator
Processes each line of the data using the provided block.
This method applies the given block to each line in the @data array, replacing the original @data with the results of the block. The method returns self
to allow method chaining.
Returns an enumerator if no block is given.
65 66 67 68 69 70 71 72 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 65 def process_each_line can_call?(:process_each_line, "call .multiple_lines first") return to_enum(__callee__) unless block_given? @data = @data.map do |line| yield line end self end |
#sections(delimiter = "\n\n") ⇒ Array<AocInput>
Returns a new AocInput
for each section of the raw input, split by the given delimiter.
127 128 129 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 127 def sections(delimiter = "\n\n") @sections = @raw_input.split(delimiter).map { |section_input| AocInput.new(section_input) } end |
#single_line ⇒ AocInput
Strips newline characters from the data, leaving a single line of input.
28 29 30 31 32 33 34 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 28 def single_line can_call?(:single_line) @data = data.chomp('') revoke(:multiple_lines) revoke(:single_line) self end |
#sort_arrays ⇒ AocInput
Sorts each array within the @data array.
This method processes @data by sorting each nested array in ascending order. It directly modifies @data and returns self
to enable method chaining.
112 113 114 115 116 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 112 def sort_arrays can_call?(:sort_arrays, "call .columns_of_numbers first") @data = data.map { |ary| ary.sort } self end |
#to_grid ⇒ Grid
Returns a new Grid
object from the parsed input
120 121 122 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 120 def to_grid Grid.from_input(@raw_input) end |
#transpose ⇒ AocInput
Transposes the data array.
This method can only be called after #columns_of_numbers. It directly modifies @data by transposing it and returns self
to allow method chaining.
99 100 101 102 103 |
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 99 def transpose can_call?(:transpose, "call .columns_of_numbers first") @data = data.transpose self end |