-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4-1.rb
47 lines (41 loc) · 1.06 KB
/
day4-1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
data = File.open("day4.txt").readlines.map(&:chomp)
# convert the data to hash datatype
data_hash = {}
each_info = []
tag = 1
data.each do |info|
if info == ""
data_hash[tag.to_s] = each_info
tag += 1
each_info = []
else
each_info.push(info)
end
end
# add the last piece of data to hash, it is missing in the loop because the fault of " if info == "" ".
data_hash[tag.to_s] = each_info
# convert the inner data to flatten array
data_hash.each do |key, info|
arrange_data = []
info.each do |pwd|
arrange_data.push(pwd.split(" "))
end
arrange_data.flatten!
data_hash[key] = arrange_data
end
# convert the inner data to hash datatype
data_hash.each do |key, info|
pwd_hash = {}
info.each do |pwd|
item, value = pwd.split(":")
pwd_hash[item] = value
end
data_hash[key] = pwd_hash
end
expected_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
valid_passports = 0
data_hash.each_value do |value|
missing = expected_fields - value.keys
valid_passports += 1 if missing.empty? || missing == ["cid"]
end
p valid_passports