okay, this is significantly nicer

This commit is contained in:
Colin McMillen 2020-12-04 23:15:13 -05:00
parent 5d822ff397
commit 9dcddd0081

View File

@ -55,10 +55,6 @@ namespace AdventOfCode {
} }
} }
static bool ValidateHairColor(string value) {
return Regex.IsMatch(value, @"^#[0-9a-f]{6}$");
}
static bool IsValidPassport2(string input) { static bool IsValidPassport2(string input) {
string[] fields = input.Split(' '); string[] fields = input.Split(' ');
var fieldsPresent = new HashSet<string>(); var fieldsPresent = new HashSet<string>();
@ -69,35 +65,18 @@ namespace AdventOfCode {
string[] tokens = field.Split(':'); string[] tokens = field.Split(':');
string key = tokens[0]; string key = tokens[0];
string value = tokens[1]; string value = tokens[1];
fieldsPresent.Add(key); bool valid = key switch {
if (key == "byr") { "byr" => ValidateYear(value, 1920, 2002),
if (!ValidateYear(value, 1920, 2002)) { "iyr" => ValidateYear(value, 2010, 2020),
return false; "eyr" => ValidateYear(value, 2020, 2030),
} "hgt" => ValidateHeight(value),
} else if (key == "iyr") { "hcl" => Regex.IsMatch(value, @"^#[0-9a-f]{6}$"),
if (!ValidateYear(value, 2010, 2020)) { "ecl" => validEyeColors.Contains(value),
return false; "pid" => Regex.IsMatch(value, @"^[0-9]{9}$"),
} _ => false
} else if (key == "eyr") { };
if (!ValidateYear(value, 2020, 2030)) { if (valid) {
return false; fieldsPresent.Add(key);
}
} else if (key == "hgt") {
if (!ValidateHeight(value)) {
return false;
}
} else if (key == "hcl") {
if (!ValidateHairColor(value)) {
return false;
}
} else if (key == "ecl") {
if (!validEyeColors.Contains(value)) {
return false;
}
} else if (key == "pid") {
if (!Regex.IsMatch(value, @"^[0-9]{9}$")) {
return false;
}
} }
} }
return fieldsPresent.IsSupersetOf(requiredFields); return fieldsPresent.IsSupersetOf(requiredFields);