Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.4 kB · 66 lines
Ruby
at rust
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667#!/usr/bin/env ruby# frozen_string_literal: true
require 'digest'require 'ostruct'
input = $stdin.readline.strip
def gen(salt, count) Digest::MD5.hexdigest "#{salt}#{count}"end
def gen_extra(value) 2016.times { value = Digest::MD5.hexdigest value } valueend
counter = 0
keys = []keys2 = []
to_check = {}to_check2 = {}
def verify_keys!(to_check, count, key, keys) valid = to_check.select { |c, d| count - c <= 1000 && !d.finished && key =~ d.pattern }
return if valid.empty?
valid.each do |c, data| puts "#{data.key} from #{c} matches #{key} at #{count}" data.finished = true keys.push data.key endend
def check_match!(key, to_check, count) match = key.match(/([a-z0-9])\1\1/)
return unless match
# puts "Found initial hash: #{key}"
to_check[count] = OpenStruct.new( # rubocop:disable Style/OpenStructUse pattern: /(#{match[1]})\1\1\1\1/, key: key, finished: false )end
loop do key = gen input, counter key2 = gen_extra key
verify_keys! to_check, counter, key, keys verify_keys! to_check2, counter, key2, keys2
break if keys.size >= 64 && keys2.size >= 64
check_match! key, to_check, counter check_match! key2, to_check2, counter
counter += 1end
puts "(1) #{to_check.select { |_, d| d.finished }.keys.first(64).max}"puts "(2) #{to_check2.select { |_, d| d.finished }.keys.first(64).max}"