finished solution 1
parent
1c03113454
commit
9409fe5275
|
@ -0,0 +1,55 @@
|
|||
#!/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
|
||||
|
Loading…
Reference in New Issue