Advent of Code (Day 4)

Hot Diggity, this one DIDN’T take 4 hours. Merry Freaking Christmas!

Check out out day 4 here.

For the first one you had lines of ‘passwords’, and you couldn’t have repeat words. You just had to count how many valid passwords there are.

The second one, you couldn’t have any words as anagrams of each other.

I created a letter_spectrum, an array of the letters of each word sorted so two anagram words would have the same letter_spectrum.

def no_repeats?(string)
  words_count =
  words = string.split(' ')

  words.each do |word|
    words_count[word] += 1

  words_count.values.all? {|count| count == 1}

def letter_spectrum(word)

def no_anagrams?(string)
  spectrum_count =
  words = string.split(' ')

  words.each do |word|
    spectrum = letter_spectrum(word)
    spectrum_count[spectrum] += 1

  spectrum_count.values.all? {|count| count == 1}

passwords ='passwords.txt').split("\n")

valid_passwords = { |password| no_repeats?(password) }
unique_passwords = { |password| no_anagrams?(password) }

puts "valid passwords #{valid_passwords.size}"
puts "unique passwords #{unique_passwords.size}"