okay, this is significantly nicer
This commit is contained in:
parent
5d822ff397
commit
9dcddd0081
@ -55,10 +55,6 @@ namespace AdventOfCode {
|
||||
}
|
||||
}
|
||||
|
||||
static bool ValidateHairColor(string value) {
|
||||
return Regex.IsMatch(value, @"^#[0-9a-f]{6}$");
|
||||
}
|
||||
|
||||
static bool IsValidPassport2(string input) {
|
||||
string[] fields = input.Split(' ');
|
||||
var fieldsPresent = new HashSet<string>();
|
||||
@ -69,35 +65,18 @@ namespace AdventOfCode {
|
||||
string[] tokens = field.Split(':');
|
||||
string key = tokens[0];
|
||||
string value = tokens[1];
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
return fieldsPresent.IsSupersetOf(requiredFields);
|
||||
|
Loading…
Reference in New Issue
Block a user