Part 2 solution
#!/usr/bin/env ruby def find_valid_passwords(min, max) total = 0 current = min while current <= max str = current.to_s if has_exactly_two_adjacent(str) && digits_increasing(str) total += 1 end current += 1 end return total end def has_exactly_two_adjacent(str) 10.times do |n| double = n.to_s * 2 triple = n.to_s * 3 if str.include?(double) && !str.include?(triple) return true end end return false end def digits_increasing(str) last_val = nil str.split('').each do |c| val = c.to_i if last_val != nil && last_val > val return false end last_val = val end return true end if __FILE__ == $0 positions = [] File.open(ARGV[0], "r") do |file_handle| file_handle.each_line do |line| range = line.split('-') puts find_valid_passwords(range[0].to_i, range[1].to_i) end end end
not a fan of my has_exactly_two_adjacent method but, hey, it works ¯\_(ツ)_/¯
has_exactly_two_adjacent
my part 1's has_two_adjacent method was a lot nicer:
has_two_adjacent
def has_two_adjacent(str) last_c = nil str.split('').each do |c| if last_c != nil && last_c == c return true end last_c = c end return false end
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Part 2 solution
not a fan of my
has_exactly_two_adjacent
method but, hey, it works ¯\_(ツ)_/¯my part 1's
has_two_adjacent
method was a lot nicer: