Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
585 B
11
Indexable
Never
require 'set'

def solve
  buffer_arr = File.read('day6/input.txt').split('')
  start_of_packet = 0
  start_of_message = 0
  p1_set = Set.new
  p2_set = Set.new

  buffer_arr.each_with_index do |c, i|
    p1_set.clear
    p2_set.clear
    start_of_packet = i + 1 if start_of_packet.zero? && i > 2 && p1_set.merge(buffer_arr[i - 3..i]).length == 4
    start_of_message = i + 1 if start_of_message.zero? && i > 12 && p2_set.merge(buffer_arr[i - 13..i]).length == 14
    return [start_of_packet, start_of_message] unless start_of_packet.zero? || start_of_message.zero?
  end
end

p solve