Created
December 5, 2020 16:57
-
-
Save dougdroper/216d874fcba5a8e3355bc32f374c93ee to your computer and use it in GitHub Desktop.
day 4 p2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry'.freeze | |
values = File.read('input2.txt') | |
def b_rule(v) | |
return true if (1920..2002).include?(v.to_i) | |
end | |
def i_rule(v) | |
return true if (2010..2020).include?(v.to_i) | |
end | |
def e_rule(v) | |
(2020..2030).include?(v.to_i) | |
end | |
def h_rule(v) | |
me, t = v.match(/(\d*)(\D*)/).captures | |
return (59..76).include?(me.to_i) if t == 'in' | |
return (150..193).include?(me.to_i) if t == 'cm' | |
false | |
end | |
def hc_rule(v) | |
c, _ = v.match(/\#([0-9a-f]*)/i)&.captures | |
return false unless c | |
c&.length == 6 | |
end | |
def ec_rule(v) | |
%w[amb blu brn gry grn hzl oth].include?(v) | |
end | |
def p_rule(v) | |
m, _ = v.match(/(\d*)/).captures | |
return true if m&.length == 9 | |
end | |
valid = { | |
byr: ->(v) { b_rule(v) }, | |
iyr: ->(v) { i_rule(v) }, | |
eyr: ->(v) { e_rule(v) }, | |
hgt: ->(v) { h_rule(v) }, | |
hcl: ->(v) { hc_rule(v) }, | |
ecl: ->(v) { ec_rule(v) }, | |
pid: ->(v) { p_rule(v) } | |
} | |
fff = values.split(/\n\n/).map { |line| | |
vi = line.split(/\n/).join(' ').split | |
all = vi.map { |e| e.split(':') }.to_h | |
all_keys = all.keys - ['cid'] | |
all.delete('cid') | |
(valid.keys - all_keys.map(&:to_sym)).empty? && | |
all.all? do |k, v| | |
valid[k.to_sym].call(v) | |
end | |
} | |
puts fff.count(true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment