-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.rb
89 lines (71 loc) · 2.03 KB
/
day04.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
file = File.open("input/day04.txt", "r")
lines = file.readlines
$fieldNames = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"] # + "cid"
class Hash
def hasAllFields
for fn in $fieldNames
if not has_key?(fn)
return false
end
end
return true
end
def isValidPassport
if !hasAllFields
return false
end
checks = 0
checks += validNumber(self["byr"], 1920, 2002)
checks += validNumber(self["iyr"], 2010, 2020)
checks += validNumber(self["eyr"], 2020, 2030)
if self["hgt"].match?(/([0-9]+)in/)
md = self["hgt"].match(/([0-9]+)in/)
checks += validNumber(md[1], 59, 76)
elsif self["hgt"].match?(/([0-9]+)cm/)
md = self["hgt"].match(/([0-9]+)cm/)
checks += validNumber(md[1], 150, 193)
end
checks += validHex(self["hcl"])
for ecl in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
if self["ecl"] == ecl
checks += 1
break
end
end
checks += (self["pid"].match?(/^[0-9]{9}$/) ? 1 : 0)
return checks == 7
end
end
def validNumber(value, min, max)
if (value.to_i < min || value.to_i > max)
return 0
end
return 1
end
def validHex(value)
return value.match?(/#[0-9a-fA-F]{6}$/) ? 1 : 0
end
def processPassport(passport)
# puts passport
fields = {}
passport.split(" ").map{|e| e.split(":")}.each {|e| fields[e[0]] = e[1]}
return fields
# return fields.hasAllFields # part 1
return fields.isValidPassport
end
passports = []
passport = ""
for line in lines
passport += " " + line.rstrip
if line.strip.empty?
passports.push(processPassport(passport))
passport = ""
next
end
end
if !passport.strip.empty?
passports.push(processPassport(passport))
end
puts passports.count # total
puts passports.count {|p| p.hasAllFields} # part 1
puts passports.count {|p| p.isValidPassport} # part 2