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];
bool valid = key switch {
"byr" => ValidateYear(value, 1920, 2002),
"iyr" => ValidateYear(value, 2010, 2020),
"eyr" => ValidateYear(value, 2020, 2030),
"hgt" => ValidateHeight(value),
"hcl" => Regex.IsMatch(value, @"^#[0-9a-f]{6}$"),
"ecl" => validEyeColors.Contains(value),
"pid" => Regex.IsMatch(value, @"^[0-9]{9}$"),
_ => false
};
if (valid) {
fieldsPresent.Add(key); fieldsPresent.Add(key);
if (key == "byr") {
if (!ValidateYear(value, 1920, 2002)) {
return false;
}
} else if (key == "iyr") {
if (!ValidateYear(value, 2010, 2020)) {
return false;
}
} else if (key == "eyr") {
if (!ValidateYear(value, 2020, 2030)) {
return false;
}
} 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);