Class: DotMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/aoc_rb_helpers/dot_matrix.rb

Overview

Parses and decodes ASCII art text from puzzles. Can output to STDOUT or return the result to your code.

Constant Summary collapse

DICTIONARY =
{
  a: [
    " XX ",
    "X  X",
    "X  X",
    "XXXX",
    "X  X",
    "X  X"
  ],
  b: [
    "XXX ",
    "X  X",
    "XXX ",
    "X  X",
    "X  X",
    "XXX "
  ],
  c: [
    " XX ",
    "X  X",
    "X   ",
    "X   ",
    "X  X",
    " XX "
  ],
  d: [
    "XXX ",
    "X  X",
    "X  X",
    "X  X",
    "X  X",
    "XXX "
  ],
  e: [
    "XXXX",
    "X   ",
    "XXX ",
    "X   ",
    "X   ",
    "XXXX"
  ],
  f: [
    "XXXX",
    "X   ",
    "XXX ",
    "X   ",
    "X   ",
    "X   "
  ],
  g: [
    " XX ",
    "X  X",
    "X   ",
    "X XX",
    "X  X",
    " XXX"
  ],
  h: [
    "X  X",
    "X  X",
    "XXXX",
    "X  X",
    "X  X",
    "X  X",
  ],
  i: [
    "XXX",
    " X ",
    " X ",
    " X ",
    " X ",
    "XXX"
  ],
  j: [
    "  XX",
    "   X",
    "   X",
    "   X",
    "X  X",
    " XX ",
  ],
  k: [
    "X  X",
    "X X ",
    "XX  ",
    "X X ",
    "X X ",
    "X  X",
  ],
  l: [
    "X   ",
    "X   ",
    "X   ",
    "X   ",
    "X   ",
    "XXXX",
  ],
  o: [
    " XX ",
    "X  X",
    "X  X",
    "X  X",
    "X  X",
    " XX "
  ],
  p: [
    "XXX ",
    "X  X",
    "X  X",
    "XXX ",
    "X   ",
    "X   ",
  ],
  r: [
    "XXX ",
    "X  X",
    "X  X",
    "XXX ",
    "X X ",
    "X  X",
  ],
  s: [
    " XXX",
    "X   ",
    "X   ",
    " XX ",
    "   X",
    "XXX ",
  ],
  t: [
    "XXX",
    " X ",
    " X ",
    " X ",
    " X ",
    " X "
  ],
  u: [
    "X  X",
    "X  X",
    "X  X",
    "X  X",
    "X  X",
    " XX ",
  ],
  y: [
    "X   X",
    "X   X",
    " X X ",
    "  X  ",
    "  X  ",
    "  X  "
  ],
  z: [
    "XXXX",
    "   X",
    "  X ",
    " X  ",
    "X   ",
    "XXXX",
  ]
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, on = "X", off = " ") ⇒ DotMatrix

Returns a new instance of DotMatrix.

Parameters:

  • input (Array<Array<String>>)

    a two-dimensional array of characters

  • on (String) (defaults to: "X")

    specifies the character representing the “on” state, or printed text

  • off (String) (defaults to: " ")

    specifies the character representing the “off” state, or whitespace



180
181
182
183
184
185
186
# File 'lib/aoc_rb_helpers/dot_matrix.rb', line 180

def initialize(input, on = "X", off = " ")
  @input = input
  @on = on
  @off = off

  convert_characters
end

Class Method Details

.decode(input, on: "X", off: " ") ⇒ String

Returns the decoded input using the specified characters for printed text (on) and whitespace (off).

Parameters:

  • input (Array<Array<String>>)

    a two-dimensional array of characters

  • on (String) (defaults to: "X")

    specifies the character representing the “on” state, or printed text

  • off (String) (defaults to: " ")

    specifies the character representing the “off” state, or whitespace

Returns:

  • (String)

    the decoded string



173
174
175
# File 'lib/aoc_rb_helpers/dot_matrix.rb', line 173

def self.decode(input, on: "X", off: " ")
  new(input, on, off).to_s
end

Instance Method Details

Prints the input array to STDOUT and returns self.

Returns:



190
191
192
193
194
195
# File 'lib/aoc_rb_helpers/dot_matrix.rb', line 190

def print(input = @input)
  input.each do |row|
    puts row.is_a?(String) ? row : row.join
  end
  self
end

Prints the decoded input to STDOUT and returns self.

Returns:



199
200
201
202
# File 'lib/aoc_rb_helpers/dot_matrix.rb', line 199

def print_decoded
  puts to_s
  self
end

#to_sString

Returns the decoded input.

Returns:

  • (String)

    the decoded input



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/aoc_rb_helpers/dot_matrix.rb', line 206

def to_s
  cursor = 0
  decoded = ""
  while cursor < max_cursor
    begin
      cursor += 1 while @input.all? { |row| row[cursor] == ' ' }
      cursor_end = cursor_range(cursor)
      char_map = @input.map do |row|
        row[cursor...cursor_end].join
      end
      cursor = cursor_end

      decoded += DICTIONARY.find { |_, map| map == char_map }[0].to_s.upcase
    rescue NoMethodError
      puts "ERROR: Missing character in dictionary:\n\n"
      print(char_map)
      puts "\nReverting to ASCII:\n\n"
      print
      break
    end
  end

  decoded
end