AoC2020/day04/solution01.rb

54 lines
887 B
Ruby
Raw Normal View History

2020-12-04 13:49:28 +00:00
#!/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
2020-12-04 15:10:40 +00:00
pp results