-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.rb
102 lines (86 loc) · 2.07 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
90
91
92
93
94
95
96
97
98
99
100
101
102
input_arr = File.open('input.txt').read.split("\n\n")
ret = 0
input_arr.delete_if do |item|
flag = true;
['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'].each do |field|
flag = false unless item.include?(field)
end
!flag
end
input_arr.map! { |e| e.split /[\n\s]+/ }
input_arr.delete_if do |entry|
flag = false
entry.each do |field|
if field.include? 'byr'
byr = field.match(/:(.*)/)[1].to_i
unless byr >= 1920 && byr <= 2002
flag = true
break
end
end
if field.include? 'iyr'
iyr = field.match(/:(.*)/)[1].to_i
unless iyr >= 2010 && iyr <= 2020
flag = true
break
end
end
if field.include? 'eyr'
eyr = field.match(/:(.*)/)[1].to_i
unless eyr >= 2020 && eyr <= 2030
flag = true
break
end
end
if field.include? 'hgt'
hgt = field.match(/:(.*)/)[1]
if hgt.include? 'cm'
num = hgt.scan(/\d/).join('').to_i
unless num >= 150 && num <= 193
flag = true
break
end
elsif hgt.include? 'in'
num = hgt.scan(/\d/).join('').to_i
unless num >= 59 && num <= 76
flag = true
break
end
else
flag = true
break
end
end
if field.include? 'hcl'
hcl = field.match(/:(.*)/)[1]
unless hcl[0] =='#' && hcl.length == 7
flag = true
break
end
hcl.slice(1,6).split('').each do |ltr|
array = [*('a'..'f'), *('0'..'9')]
unless array.include? ltr
flag = true
break
end
end
end
if field.include? 'ecl'
ecl = field.match(/:(.*)/)[1]
unless ['amb','blu','brn','gry','grn','hzl','oth'].include? ecl
flag = true
break
end
end
if field.include? 'pid'
pid = field.match(/:(.*)/)[1]
pid_nums = pid.scan(/\d/).join('')
unless pid.length == 9 && pid_nums.length == 9
flag = true
break
end
end
end
flag
end
print input_arr.length