Browse Source

okay, this is significantly nicer

main
Colin McMillen 1 year ago
parent
commit
9dcddd0081
  1. 45
      2020/Day04.cs

45
2020/Day04.cs

@ -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…
Cancel
Save