Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
807 B
12
Indexable
Never
def get_char_value(char)
  return char.ord - 38 if /[[:upper:]]/ =~ char
  char.ord - 96
end

def solve
  stored_lines = []
  sum1 = 0
  sum2 = 0

  File.read('day3/input.txt').split("\n").map do |line|
    line_arr = line.split('')
    stored_lines << line_arr

    if stored_lines.length == 3
      match_part_two = stored_lines[0] & stored_lines[1] & stored_lines[2]
      char_part_two = match_part_two.first
      sum2 += get_char_value(char_part_two)
      stored_lines.clear
    end

    first_line_half = line_arr[0..(line_arr.length / 2)]
    second_line_half = line_arr[(line_arr.length / 2)..line_arr.length]
    match_part_one = first_line_half & second_line_half
    char_part_one = match_part_one.first
    sum1 += get_char_value(char_part_one)
  end

  { sum1: sum1, sum2: sum2 }
end

p solve