class Passphrase attr_reader :words def initialize(words) @words = words end def self.for(string) new string.split end def valid? words.group_by{ |x| x }.values.map(&:length).none? { |group_length| group_length > 1 } end def more_valid? words.map{|word| word.split("").sort.join("") }.group_by{ |x| x }.values.map(&:length).none? { |group_length| group_length > 1 } end end passphrase_list = STDIN.read.chomp.split("\n").map { |line| Passphrase.for line } part_1 = passphrase_list.select(&:valid?).length puts "Part 1: #{part_1}" part_2 = passphrase_list.select(&:more_valid?).length puts "Part 2: #{part_2}"