diff --git a/2020/Day04.cs b/2020/Day04.cs index 5f5a556..2cafc3c 100644 --- a/2020/Day04.cs +++ b/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(); @@ -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);