From eea91b1ed5b6bebf4f7e02c11b90e9bb595350e7 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Thu, 3 Dec 2020 21:23:40 -0800 Subject: [PATCH] [2020][ruby][4.x] --- 2020/ruby/day_04.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2020/ruby/day_04.rb diff --git a/2020/ruby/day_04.rb b/2020/ruby/day_04.rb new file mode 100644 index 0000000..b0e99ec --- /dev/null +++ b/2020/ruby/day_04.rb @@ -0,0 +1,25 @@ +FIELDS = { + byr: ->(s) { s =~ /^\d{4}$/ && (1920..2002).cover?(s.to_i) }, + iyr: ->(s) { s =~ /^\d{4}$/ && (2010..2020).cover?(s.to_i) }, + eyr: ->(s) { s =~ /^\d{4}$/ && (2020..2030).cover?(s.to_i) }, + hgt: ->(s) { + s =~ /^(\d+)(cm|in)$/ + case $2 + when "cm" + (150..193).cover?($1.to_i) + when "in" + (59..76).cover?($1.to_i) + else + false + end + }, + hcl: ->(s) { s =~ /^#[0-9a-f]{6}$/ }, + ecl: ->(s) { s =~ /^(amb|blu|brn|gry|grn|hzl|oth)$/ }, + pid: ->(s) { s =~ /^\d{9}$/ }, + cid: ->(s) { true }, +} + +puts ARGF.read.split("\n\n").map {|line| line.scan(/(\w+):(\S+)/).to_h } + .count {|passport| + FIELDS.map {|k,v| v[passport.fetch(k.to_s, "")] }.all? + }