You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2016/ruby/day_14.rb

34 lines
669 B

require 'digest/md5'
class Keys
attr_reader :seed, :hash
def initialize(seed)
@seed = seed
@hash = Hash.new {|h,k|
hash = Digest::MD5.hexdigest("#{seed}#{k}")
2016.times do
hash = Digest::MD5.hexdigest(hash)
end
h[k] = hash
}
end
def each
return enum_for(__method__) unless block_given?
(0..Float::INFINITY).each do |index|
next unless /(.)\1\1/ =~ hash[index]
needle = $1 * 5
stream = (1..1000).map {|i| hash[index + i] }
next unless stream.any? {|h| h.include?(needle) }
yield [index, hash[index]]
end
end
end
keys = Keys.new('qzyelonm')
p keys.each.take(64).last