Reimplement day 4 using validators.

This commit is contained in:
Danila Fedorin 2020-12-04 19:54:14 -08:00
parent 1a2f18b316
commit b91819c4cf
2 changed files with 70 additions and 22 deletions

View File

@ -15,12 +15,13 @@ end
def part2 def part2
input = INPUT.clone input = INPUT.clone
passports = parse_passports(input) passports = parse_passports(input)
total = passports.count do |passport| valid_passports = [] of Passport
DEFAULT_VALIDATORS.all? do |k, v| passports.each do |p|
passport[k]?.try { |s| v.call(s) } if vp = Passport.from_hash?(p)
valid_passports << vp
end end
end end
puts total puts valid_passports.size
end end
part1 part1

View File

@ -13,27 +13,74 @@ def parse_passports(lines)
end end
end end
def validate_range(range) class Validator(T, R)
->(s : String) { s.to_i32?.try { |i| range.includes? i } } def initialize(@proc : Proc(T, R?))
end end
def validate_regex(regex) def >>(other)
->(s : String) { s.matches?(regex) } Validator.new(->(i : T) { @proc.call(i).try { |j| other.@proc.call(j) } })
end end
def validate_unit_range(unit_ranges) def then(&block : R -> S?) forall S
->(s : String) { Validator(T, S).new(->(i : T) { @proc.call(i).try { |j| block.call(j) } })
return false unless s.match(/^(\d+)([a-zA-Z]+)$/)
unit_ranges[$~[2]]?.try { |rng| validate_range(rng).call($~[1]) }
}
end end
DEFAULT_VALIDATORS = { def run(input)
"byr" => validate_range(1920..2002), @proc.call(input)
"iyr" => validate_range(2010..2020), end
"eyr" => validate_range(2020..2030), end
"hgt" => validate_unit_range({"cm" => (150..193), "in" => (59..76)}),
"hcl" => validate_regex(/^#[0-9a-f]{6}$/), class Hash(K,V)
"ecl" => ->(s : String) { "amb blu brn gry grn hzl oth".split(" ").includes? s }, def validate(k, vl)
"pid" => ->(s : String) { s =~ /^[0-9]{9}$/ }, self[k]?.try { |v| vl.run(v) }
} end
end
def range(range) : Validator(Int32, Int32)
Validator.new ->(i : Int32) { (range.includes? i) ? i : nil }
end
def int : Validator(String, Int32)
Validator.new ->(s : String) { s.to_i32? }
end
def regex(regex) : Validator(String, Regex::MatchData)
Validator(String, Regex::MatchData).new ->(s : String) { s.match(regex) }
end
def oneof(strs : Array(A)) : Validator(A, A) forall A
Validator(String, String).new ->(s : String) { (strs.includes? s) ? s : nil }
end
def length(len) : Validator(String, String)
Validator(String, String).new ->(s : String) { (s.size == len) ? s : nil }
end
def year(rng) : Validator(String, Int32)
length(4) >> int >> range(rng)
end
def unit_range(map) : Validator(String, {String, Int32})
regex(/(\d+)([a-zA-Z]+)/).then do |md|
map[md[2]]?.try { |r| (int >> range(r)).run(md[1]).try { |i| {md[2], i} } }
end
end
class Passport
def initialize(
@byr : Int32, @iyr : Int32,
@eyr : Int32, @hgt : {String, Int32},
@hcl : String, @ecl : String, @pid : Int32)
end
def self.from_hash?(hash)
return nil unless byr = hash.validate("byr", year(1920..2002))
return nil unless iyr = hash.validate("iyr", year(2010..2020))
return nil unless eyr = hash.validate("eyr", year(2020..2030))
return nil unless hgt = hash.validate("hgt", unit_range({"cm" => (150..193), "in" => (59..76)}))
return nil unless hcl = hash.validate("hcl", regex(/^#([0-9a-f]{6})$/).then { |d| d[1] })
return nil unless ecl = hash.validate("ecl", oneof(["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]))
return nil unless pid = hash.validate("pid", length(9) >> int)
Passport.new(byr, iyr, eyr, hgt, hcl, ecl, pid)
end
end