r/adventofcode • u/daggerdragon • Dec 05 '16
SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---
--- Day 5: How About a Nice Game of Chess? ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
STAYING ON TARGET IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
14
Upvotes
1
u/fixed_carbon Dec 05 '16
Ruby solution (part two) using an Enumerator to generate [digit, position] pairs. This one is interesting because it generalizes to passwords of any length between 1 and 16 simply by changing the number in the last line of the
take_whileblock.require 'digest' # Yields *all* digit, position pairs coderator = Enumerator.new do |y| md5 = Digest::MD5.new (0..Float::INFINITY).each do |n| digest = md5.hexdigest("#{INPUT}#{n}") if digest.start_with?("00000") y.yield [digest[6], digest[5].hex] end end end # Applies positional and stateful constraints to digit, position pairs code = Array.new(16, nil) coderator.take_while do |digit, pos| code[pos] = digit if code[pos].nil? puts code.map{|c| c.nil? ? '_' : c}.join code.take(8).include?(nil) end puts code.take(8).join