Class: AocInput

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(puzzle_input) ⇒ AocInput

Returns a new AocInput initialized with the given puzzle input.

Parameters:

  • puzzle_input (String)

    the puzzle input as a single string with embedded newline characters



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.

Parameters:

  • delimiter (String, nil) (defaults to: nil)

    the delimiter to be passed to String#split

Returns:

Raises:



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

#dataObject

Returns the parsed puzzle input.

If the data has not been parsed yet, it defaults to the raw input provided in #initialize.

Returns:

  • (Object)

    the parsed puzzle input



21
22
23
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 21

def data
  @data ||= @raw_input
end

#multiple_linesAocInput

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.

Returns:



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.

Yield Parameters:

  • line (Object, Array<Object>)

    a single line of the data being processed

Yield Returns:

  • (Object, Array<Object>)

    the result of processing the line

Returns:

  • (self)

    the instance itself, for method chaining

  • (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.

Parameters:

  • delimiter (String) (defaults to: "\n\n")

    the string used to split sections

Returns:

  • (Array<AocInput>)

    an array of new AocInput instances initialised with each section of the raw input from self



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_lineAocInput

Strips newline characters from the data, leaving a single line of input.

Returns:



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_arraysAocInput

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.

Returns:

Raises:



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_gridGrid

Returns a new Grid object from the parsed input

Returns:

  • (Grid)

    the new grid



120
121
122
# File 'lib/aoc_rb_helpers/aoc_input.rb', line 120

def to_grid
  Grid.from_input(@raw_input)
end

#transposeAocInput

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.

Returns:

Raises:



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