m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/04/b.rb
blob: e2cd254530696961bda0f86daa6ae8a7dcdadc75 (plain)
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
file = File.read('input.txt')
records = file.split "\n\n"

fields = [ 'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid' ]

regexes = [
  /^byr:(19[2-9]\d|200[0-2])$/,
  /^iyr:(201\d|2020)$/,
  /^eyr:(202\d|2030)$/,
  /^hgt:((1[5-8]\d|19[0-3])cm|(59|6\d|7[0-6])in)$/,
  /^hcl:#[0-9a-f]{6}$/,
  /^ecl:(amb|blu|brn|gry|grn|hzl|oth)$/,
  /^pid:\d{9}$/
]

count = 0
records.each do |record|
  fields_correct = 0
  record.split.each do |entry|
    if regexes.any? {|r| r.match?(entry)}
      fields_correct += 1
    end
  end

  if fields_correct == 7
    count += 1
  end
end
puts count