Created
December 12, 2015 12:38
-
-
Save wconrad/234e15a2b9725b84bfca to your computer and use it in GitHub Desktop.
Advent of Code, day 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# http://adventofcode.com/day/11 | |
INPUT = "hepxcrrq" | |
THREE_LETTER_RUN = Regexp.union(("a".."z").each_cons(3).map(&:join)) | |
ILLEGAL_LETTER = /[iol]/ | |
def unique_pairs(s) | |
s.scan(/(.)\1/).uniq | |
end | |
def good_password?(password) | |
unique_pairs(password).count > 1 && | |
password =~ THREE_LETTER_RUN && | |
password !~ ILLEGAL_LETTER | |
end | |
def next_good_password(password) | |
loop do | |
password = password.succ | |
return password if good_password?(password) | |
end | |
end | |
password = INPUT | |
2.times do | |
password = next_good_password(password) | |
puts password | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment