AoC2020/day04/solution01.rb

56 lines
889 B
Ruby
Executable File

#!/bin/env ruby
def valid_passport? (passport, fields)
fields.each do |field|
return false if !(passport.has_key?(field.to_sym))
end
true
end
text = File.read('./input')
passports = Hash.new
key = 0
passport = Hash.new
required_fields = [
"byr",
"iyr",
"eyr",
"hgt",
"hcl",
"ecl",
"pid",
# "cid"
]
results = {
:valid => 0,
:invalid => 0
}
text.each_line do |line|
line.strip!
if line == ''
passports[key] = passport
passport = Hash.new
key += 1
next
end
line.split(' ').each do |pair|
pair = pair.split(":")
passport[pair[0].to_sym] = pair[1]
end
end
passports[key] = passport
passports.each_value do |passport|
if valid_passport?(passport, required_fields)
results[:valid] += 1
else
results[:invalid] += 1
end
end
pp results